How to use the ddt.mk_test_name function in ddt

To help you get started, we’ve selected a few ddt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IQuOD / AutoQC / demo / demo.py View on Github external
import re

dir = os.path.dirname(__file__)
datafile = os.path.join(dir, 'data/demo.json')
print datafile

# monkey patch ddt to not suffix data onto test names
def mk_test_name(name, value, index=0):
    try:
        value = str(value)
    except UnicodeEncodeError:
        # fallback for python2
        value = value.encode('ascii', 'backslashreplace')
    test_name = "{0}_{1}".format(name, index + 1)
    return re.sub('\W|^(?=\d)', '_', test_name)
ddt.mk_test_name = mk_test_name

# Find all QC subroutines.
testFiles = glob.glob('tests/[!_]*.py')
print testFiles
testNames = [testFile[6:-3] for testFile in testFiles]
print testNames

# Define a decorator to convert the QC tests to the TestName class.
def include_tests(cls):
    for testName in testNames:
        exec('import tests.' + testName)
        exec('def test_' + testName + '(self, value): self.assertTrue(tests.' + testName + '.test(value))')
        exec('setattr(test_' + testName + ', ddt.FILE_ATTR, datafile)')
        exec('cls.test_' + testName + ' = test_' + testName)
    return cls