Skip to main content

Improved TestCase class

Project description

Improved TestCase Class

License Github pypi Travis CI

dectest.TestCase is a drop-in replacement for unittest.TestCase with a few added features.

Tests, Setup, and Teardown with Decorators

Tests can optionally be marked using the @test decorator, instead of prefixing the method name with test. The following test case class contains two tests:

from dectest import TestCase, test

class MyTest(TestCase):
    def test_foo(self):
        pass

    @test
    def bar(self):
        pass

Setup and teardown methods can be marked using the @before and @after decorators, respectively. A class can have multiple setup and teardown methods:

from dectest import TestCase, before, after

class MyTest(TestCase):
    @before
    def setup_stuff(self):
        pass

    @before
    def setup_more_stuff(self):
        pass

    @after
    def teardown_all_stuff(self):
        pass

While the order of execution inside a class is undefined and should not be relied upon, it is guaranteed that setup methods in super-classes are executed before methods in sub-classes, and teardown methods in sub-classes are executed before teardown method in super-classes:

from dectest import TestCase, before, after

class MySuperTest(TestCase):
    @before
    def super_setup(self):
        print("setup first")

    @after
    def super_teardown(self):
        print("teardown second")

class MySubTest(MySuperTest):
    @before
    def sub_setup(self):
        print("setup second")

    @after
    def sub_teardown(self):
        print("teardown first")

Patch Support

dectest.TestCase has a patch() method to install a mock using unittest.mock.patch(). This patch is removed during test teardown:

from dectest import TestCase, test

class MyPatchTest(TestCase):
    @test
    def foo(self):
        exit = self.patch("sys.exit")  # will be stopped during teardown
        # call implementation
        exit.assert_called_with(1)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dectest-1.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file dectest-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: dectest-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for dectest-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6701c356b6f81a27fea2c089d7b4c50a455f5b8f008b6e2751e7aef801c3d16d
MD5 a9d812cafb88bd1c6450fd8e596f4250
BLAKE2b-256 96c3b09293faa384fc15d949f170f8271066d4c98f899682bd2551c8f2862bfd

See more details on using hashes here.

Supported by

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