Skip to main content

Improved TestCase class

Project description

# Improved TestCase Class


[![License](https://img.shields.io/pypi/l/dectest.svg)](https://pypi-hypernode.com/pypi/dectest/)
[![Github](https://img.shields.io/github/release/srittau/python-dectest/all.svg)](https://github.com/srittau/python-dectest/releases/)
[![pypi](https://img.shields.io/pypi/v/dectest.svg)](https://pypi-hypernode.com/pypi/dectest/)
[![Travis CI](https://travis-ci.org/srittau/python-dectest.svg?branch=master)](https://travis-ci.org/srittau/python-dectest)

`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:

```python
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:

```python
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:

```python
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 mock is cleaned after during test
teardown:

```python
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-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for dectest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fab5c83d26e657e826b09420c0bd7ddd922218d35c6b689d598bbe4cef3c2538
MD5 e2d222ee59b9128e80e2495fab38c713
BLAKE2b-256 c689b2fb81403a04316d2a3035ce07d792a672a4d11648aa61c9d0ec75f2b3bd

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