Unittest Support

Datatest can be used together with the unittest package from the Python Standard Library. For a quick introduction, see:

DataTestCase

class datatest.DataTestCase(methodName='runTest')

This optional wrapper class provides an interface that is consistent with established unittest conventions. This class extends unittest.TestCase with methods for asserting validity and accepting differences. In addition, familiar methods and attributes (like setUp(), maxDiff, assertions etc.) are also available.

VALIDATION METHODS

The assertion methods wrap validate() and its methods:

from datatest import DataTestCase

class MyTest(DataTestCase):
    def test_mydata(self):
        data = ...
        requirement = ...
        self.assertValid(data, requirement)
assertValid(data, requirement, msg=None)

Wrapper for validate().

assertValidPredicate(data, requirement, msg=None)

Wrapper for validate.predicate().

assertValidRegex(data, requirement, flags=0, msg=None)

Wrapper for validate.regex().

assertValidApprox(data, requirement, places=None, msg=None, delta=None)

Wrapper for validate.approx().

assertValidFuzzy(data, requirement, cutoff=0.6, msg=None)

Wrapper for validate.fuzzy().

assertValidInterval(data, min=None, max=None, msg=None)

Wrapper for validate.interval().

assertValidSet(data, requirement, msg=None)

Wrapper for validate.set().

assertValidSubset(data, requirement, msg=None)

Wrapper for validate.subset().

assertValidSuperset(data, requirement, msg=None)

Wrapper for validate.superset().

assertValidUnique(data, msg=None)

Wrapper for validate.unique().

assertValidOrder(data, sequence, msg=None)

Wrapper for validate.order().

ACCEPTANCE METHODS

The acceptance methods wrap accepted() and its methods:

from datatest import DataTestCase

class MyTest(DataTestCase):
    def test_mydata(self):
        data = ...
        requirement = ...
        with self.accepted(Missing):
            self.assertValid(data, requirement)
accepted(obj, msg=None, scope=None)

Wrapper for accepted().

acceptedKeys(predicate, msg=None)

Wrapper for accepted.keys().

acceptedArgs(predicate, msg=None)

Wrapper for accepted.args().

acceptedTolerance(tolerance, /, msg=None)
acceptedTolerance(lower, upper, msg=None)

Wrapper for accepted.tolerance().

acceptedPercent(tolerance, /, msg=None)
acceptedPercent(lower, upper, msg=None)

Wrapper for accepted.percent().

acceptedFuzzy(cutoff=0.6, msg=None)

Wrapper for accepted.fuzzy().

acceptedCount(number, msg=None, scope=None)

Wrapper for accepted.count().

Command-Line Interface

The datatest module can be used from the command line just like unittest. To run the program with test discovery use the following command:

python -m datatest

Run tests from specific modules, classes, or individual methods with:

python -m datatest test_module1 test_module2
python -m datatest test_module.TestClass
python -m datatest test_module.TestClass.test_method

The syntax and command-line options (-f, -v, etc.) are the same as unittest—see unittest’s command-line documentation for full details.

Note

Tests are ordered by file name and then by line number (within each file) when running datatest from the command-line.

Test Runner Program

@datatest.mandatory

A decorator to mark whole test cases or individual methods as mandatory. If a mandatory test fails, DataTestRunner will stop immediately (this is similar to the --failfast command line argument behavior):

@datatest.mandatory
class TestFileFormat(datatest.DataTestCase):
    def test_columns(self):
        ...
class datatest.DataTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, ignore=False)

A data test runner (wraps unittest.TextTestRunner) that displays results in textual form.

resultclass

alias of datatest.runner.DataTestResult

run(test)

Run the given tests in order of line number from source file.

class datatest.DataTestProgram(module='__main__', defaultTest=None, argv=None, testRunner=datatest.DataTestRunner, testLoader=unittest.TestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, warnings=None)

datatest.main

alias of datatest.main.DataTestProgram