Additional features to extend python unittest and mock.
Project description
unittest_additions
Helpful extras built to make python unittesting easier.
pip install unittest-additions
Features
- Additional Asserts
- Temporary Files
- Runtime Timer
mock_openLine Iteration
Example Uses
Below are some example uses for some of the features.
Temporary Files
def test_using_temp_file(self):
with TestFile(TEST_FILE, TEST_DATA) as tf:
# File TEST_FILE now exists with TEST_DATA inside.
tf.append(MORE_TEST_DATA)
# File TEST_FILE now has TEST_DATA with MORE_TEST_DATA appended.
# File TEST_FILE no longer exists.
RunTimer
A RunTimer is provied to help compare run times of code.
with RunTimer() as t:
t0 = t.split()
t1 = t.split()
t2 = t.overall()
mock_open Line Iteration
When mocking a file using mock_open, "code-under-test" using line iteration (as in the example below) will not work as expected. (do_something will not be called.)
def my_function(fn):
with open(fn, 'r') as f:
for l in f:
do_something(l)
To enable line iteration you can use add_line_iter_to_mock_open.
@patch('builtins.open', new_callable=mock_open, read_data='line 0\nline 1\n')
def test_function(self, open_mock):
add_line_iter_to_mock_open(open_mock)
# Line iteration over the mock_open read_data will now work.
Additional Asserts
To use the additional asserts, add the mixin to your TestCase class.
class MixedTestCase(TestCase, AdditionalAssertsMixin):
def test_a_test:
self.assertIsEmpty([])
self.assertIsNotEmpty(('hello',))
The new asserts are:
| Method | Checks that ... |
|---|---|
assertIsEmpty(c) |
len(c) == 0 |
assertIsNotEmpty(c) |
len(c) > 0 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unittest_additions-0.1.3.tar.gz.
File metadata
- Download URL: unittest_additions-0.1.3.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6a4d10b8bc76224ed6fa57dfd184e820023a49f1b80ae73fd40b238665b7b79
|
|
| MD5 |
89e01949a1e7f5874bdc4a8d6f7bbc47
|
|
| BLAKE2b-256 |
3aadacc7a697bda2eec31527ace75f34eb3d54b10e9af592a3fb6039a651430f
|
File details
Details for the file unittest_additions-0.1.3-py3-none-any.whl.
File metadata
- Download URL: unittest_additions-0.1.3-py3-none-any.whl
- Upload date:
- Size: 3.9 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.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbe6b8f87b22b96529707ba445b183a3a083a4107bb275663068b52ded4a0ec3
|
|
| MD5 |
1dd5710c24d1c5893ba057f95c66976a
|
|
| BLAKE2b-256 |
0fe314595ccd4f3cd02eebc23910ad047e673b632f44b51447f0be6c047e38ff
|