Skip to main content

A collection of test helpers, additional assertions, and the like.

Project description

This package collects various helpers for writing tests.

assertEllipsis

An assertion which is very helpful when using Testbrowser with unittest.TestCase (instead of doctests).

Some examples:

self.assertEllipsis('...bar...', 'foo bar qux')
# -> nothing happens

self.assertEllipsis('foo', 'bar')
# -> AssertionError: Differences (ndiff with -expected +actual):
     - foo
     + bar

self.assertNotEllipsis('foo', 'foo')
# -> AssertionError: "Value unexpectedly matches expression 'foo'."

To use, inherit from gocept.testing.assertion.Ellipsis in addition to unittest.TestCase.

assertNothingRaised

The opposite of assertRaises(), this is an assertion that makes some tests more readable. As assertRaises(), it can be used as as context manager, too:

self.assertNothingRaised(do_something, 1, 2, 3)

with self.assertNothingRaised():
    do_something(1, 2, 3)

mock patch context

gocept.testing.mock.Patches collects mock patches that are valid for the whole TestCase, and resets them all in one go in tearDown (this is pending incluion upstream as mock.patcher(), see issue 30):

class MyTest(unittest.TestCase):

    def setUp(self):
        self.patches = gocept.testing.mock.Patches()

    def tearDown(self):
        self.patches.reset()

    def test_something(self):
        compile = self.patches.add('re.compile')

It offers three methods:

add:

wraps mock.patch()

add_object:

wraps mock.patch.object

add_dict:

wraps mock.patch.dict

Note that gocept.testing does not declare a dependency on mock to be as lightweight as possible, so clients need to do that themselves.

assertCalledWith

This is syntactic sugar around mock.assert_called_with, so you can write:

class MyTest(unittest.TestCase, gocept.testing.mock.Assertions):

    def test_something(self):
        dummy = mock.Mock()
        dummy(True)
        self.assertCalledWith(dummy, True)

instead of:

dummy.assert_called_with(True)

Mocking properties

gocept.testing.mock.Property is syntactic sugar directly lifted from the mock documentation that allows you to patch properties like this:

class Dummy(object):

    @property
    def foo(self):
        return False


with mock.patch('Dummy.foo', gocept.testing.mock.Property()) as foo:
    foo.return_value = 'something else'

Development

The source code is available in the mercurial repository at https://code.gocept.com/hg/public/gocept.testing

Please report any bugs you find at https://projects.gocept.com/projects/projects/gocept-testing/issues

Changelog

1.2 (2011-12-09)

1.1 (2011-11-10)

  • Add assertNothingRaised.

1.0 (2011-11-02)

  • first release: assertEllipsis

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gocept.testing-1.2.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file gocept.testing-1.2.tar.gz.

File metadata

  • Download URL: gocept.testing-1.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gocept.testing-1.2.tar.gz
Algorithm Hash digest
SHA256 dae2279fbcb693aed327101ff6a6bb7b01638a1ca6803f91a946a4671a427fac
MD5 9b3462570fc6d108eefdf11e041b7e3c
BLAKE2b-256 7aa01992d7948ebf3f0a067ff0466f61e5f07e555d3a18969279b3974941a1e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page