Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Additional features to extend python unittest and mock.

Project description

unittest_additions

pypi-version

Helpful extras built to make python unittesting easier.

pip install unittest-additions

Features

  • Additional Asserts
  • Temporary Files
  • Runtime Timer
  • mock_open Line 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


Download files

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

Files for unittest-additions, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size unittest_additions-0.1.3-py3-none-any.whl (3.9 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size unittest_additions-0.1.3.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page