Skip to main content

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-dataprovider

Import:

import unittest
from unittest_dataprovider 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.

Metadata

Release files for unittest-dataprovider 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unittest-dataprovider 1.0.3
File Size Uploaded Provenance Actions
unittest-dataprovider-1.0.3.tar.gz 3.6 kB Without provenance Details for unittest-dataprovider-1.0.3.tar.gz

Release files / unittest-dataprovider-1.0.3.tar.gz

Download URL unittest-dataprovider-1.0.3.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
32a3fc7c36f7b4486a11e32b5d1e089793b8255e35e8531b4ad1ebb2f799270f
BLAKE2b-256 checksum
How to use checksums
77e8b0f4f91438e596fcaad95111943bf1629bb3a28994de2e4e487c2c72ac4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.10

Security for unittest-dataprovider 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.3 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page