A quick unittest-compatible framework for repeating a test function over many fixtures
Project description
repeated_test lets you nicely write tests that apply the same function to many sets of parameters.
For instance:
from repeated_test import Fixtures
class my_fixtures(Fixtures):
a = 10, 5, 5
b = 15, 7, 8
c = 42, 1, 1
@my_fixtures.with_test
def sum_test(self, expected, *terms):
self.assertEqual(expected, sum(terms))
The result is unittest-compatible, and provides useful context in the traceback in case of errors:
$ python -m unittest my_tests
..F
======================================================================
FAIL: test_c (my_tests.sum_test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/epsy/code/repeated_test/my_tests.py", line 6, in my_fixtures
c = 42, 1, 1
File "/home/epsy/code/repeated_test/my_tests.py", line 10, in sum_test
self.assertEqual(expected, sum(terms))
AssertionError: 42 != 2
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (failures=1)
You can install it using:
$ pip install --user repeated_test
Help / Issues
You can get help in the gitter.im chatroom.
If you find any issues or have any requests, use GitHub Issues.
Reference
Introduction
Python’s unittest modules helps in performing various forms of automated testing. One writes a class deriving from unittest.TestCase and adds various test_xyz methods, and test runners run these tests, keeping count of succesful tests, failed tests and produces a trace of the causes of these failures.
Sometimes it makes sense to have one test be carried out for a large amount of different inputs. This module aims to provide an efficient way to deal with such situations.
It does so by allowing you to write fixtures (inputs) as plain members of a class, and bind a test function to them. This test function is called for each fixture as you will see below. The produced class is a unittest.TestCase subclass, so it is compatible with unittest and other unittest-compatible test runners.
Building a test case
In order to produce a unittest.TestCase, repeated_test requires you to:
Subclass repeated_test.Fixtures
Write a _test function that takes a few parameters, making use of any unittest.TestCase method it needs
Assign fixtures directly in the class body, which are then unpacked as arguments to the _test method
You can use any unittest.TestCase methods in your test function, such as assertEqual() and so forth.
from repeated_test import Fixtures
class my_fixtures(Fixtures):
def _test(self, arg1, arg2, arg3):
self.assertEqual(..., ...)
Ps = 'p1', 'p2', 'p3'
# _test(*Ps) will be called, ie. _test('p1', p2', 'p3')
Qs = 'q1', 'q2', 'q3'
# _test(*Qs) will be called, ie. _test('q1', q2', 'q3')
Make sure that your fixture tuples provide the correct amount of arguments for your _test method, unless it has an *args parameter.
Naming and escaping
You may name your test tuples however you like, though they may not start with test_ or _. They are copied to the resulting unittest.TestCase class, and test methods are created for them. Their name is that of the tuple, prefixed with test_.
Members starting with test_ or _ are directly copied over to the resulting unittest.TestCase class, without being treated as fixtures. You can use this to insert regular tests amongst your fixtures, or constants that you do not wish to be treated as tests:
from repeated_test import Fixtures
class my_fixtures(Fixtures):
def _test(self, arg1, arg2, arg3):
self.assertEqual(..., ...)
def test_other(self):
self.assertEqual(3, 1+2)
_spam = 'spam, bacon and eggs'
# _spam won't be treated as a fixture, so _test(*_spam) won't be called
ham = _spam, _spam, _spam
You may even call the test function using self._test(...) if necessary.
Separating tests and fixtures
You can apply a fixtures class to a different test function using its with_test method:
class my_fixtures(Fixtures):
...
@my_fixtures.with_test
def other_test(self, arg1, arg2, arg3):
self.assertEqual(..., ...)
While the function appears out of any class, it will be used as a method of the resulting unittest.TestCase class, so keep in mind that it takes a self parameter.
You can reuse a fixture class however many times you like.
If you specify a test function this way, you can omit the _test method from your fixtures definition. However, it will not be discovered by unittest, so regular test methods won’t be run.
Working with functions as fixtures
It can be fairly impractical to use functions in your fixture tuples in this scheme. If your fixture tuple is meant to have one function in it, you can use the tup decorator:
from repeated_test import Fixtures, tup
class my_tests(Fixtures):
def _test(self, func, arg1, arg2):
self.assertEqual(..., ...)
@tup('arg1', 'arg2')
def ham():
pass
# equivalent to
def _ham():
pass
ham = _ham, 'arg1', 'arg2'
Replacing unittest.TestCase with another class
You can replace unittest.TestCase with another class using WithTestClass(cls).
For instance, if you wish to use unittest2:
import unittest2
from repeated_test import WithTestClass
class my_tests(WithTestClass(unittest2.TestCase)):
...
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
Hashes for repeated_test-0.1a2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3df6809fe96d8c7758f5442545c8856530ca36971a838f132b1120eb401c3c6e |
|
MD5 | fcc9880017b5b83cc6381e06da1315b8 |
|
BLAKE2b-256 | 119053a5b24c431773dbfb5f07c862cca207d819f1f077b45be0f7f29cc7f8c9 |