PHPUnit-like @dataprovider decorator for unittest
Project description
Disclaimer:
This is a only a clone of unittest-data-provider (https://pypi.org/project/unittest-data-provider/) with the last version of https://github.com/yourlabs/unittest-data-provider (package maintener didn’t update project on Pypi and I haven’t had any answer from him to my request).
Package for this snippet: http://melp.nl/2011/02/phpunit-style-dataprovider-in-python-unit-test/
Install:
pip install unittest-data-provider
Import:
import unittest from unittest_data_provider import data_provider
You can then use data_provider without having to stick this snippet somewhere …
Thanks drm !
Original blog post by drm pasted here in case the original link goes down
PHPUnit has a handy feature with which you can provide testdata to your tests. This is called a data provider, and is implemented by annotating a test with @dataProvider methodName. Python’s unittest module doesn’t seem to have such a feature.
PHPUnit’s version
The data provider returns a two-dimensional array of test arguments. For example:
class CssParserTest extends PHPUnit_Framework_TestCase { function setUp() { $this->parser = new CssParser(); } /** * @dataProvider cssColors */ function testParseColor($color, $notation) { $this->assertEquals($color, $this->parser->parseColor($notation)); } function cssColors() { return array( array(array(0, 0, 0), '#000'), array(array(0, 0, 0), '#000000'), array(array(0, 0, 0), 'rgb(0, 0, 0)') array(array(0, 0, 0), 'black') ); } }
Running this test would call the testParseColor() test 4 times, with each of the arrays returned by cssColors() as the arguments.
Python: providing test data using a decorator
While writing tests for some Python code, I discovered that Python’s unittest doesn’t seem to have such a feature. So I implemented my own, using a decorator:
def data_provider(fn_data_provider): """Data provider decorator, allows another callable to provide the data for the test""" def test_decorator(fn): def repl(self, *args): for i in fn_data_provider(): try: fn(self, *i) except AssertionError: print "Assertion error caught with data set ", i raise return repl return test_decorator
Example usage:
class CssParserTest: def setUp(self): self.parser = CssColor() @staticmethod def colors(): return ( ( (0, 0, 0), '#000' ), ( (0, 0, 0), '#000000' ), ( (0, 0, 0), 'rgb(0, 0, 0)' ), ( (0, 0, 0), 'black' ) ) @data_provider(colors): def test_parse_color(self, color, notation): self.assertEquals(color, self.parser.parse_color(notation))
Suggestions of alternatives are greatly appreciated, by the way.
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
Hashes for unittest-dataprovider-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 370c2b8578c885dc8b602227926e694856b02de38ef1db8cea6315443f30d9bc |
|
MD5 | 65cbce4e0a43542609daf6755ea4fe7a |
|
BLAKE2b-256 | 5c31a11f52d997f508d2e6f3c1686d4f87dc287e67a3035aafd17fdb03b45dfd |