如何发现动态生成的测试用例?

saf*_*fsd 6 python unit-testing nose

这是我前一个问题的后续行动.

在上一个问题中,探索了一些方法来实现对整个函数族基本相同的测试,确保测试不会在第一个失败的函数处停止.

我首选的解决方案是使用元类将测试动态插入unittest.TestCase.不幸的是,鼻子不会选择这个,因为鼻子静态扫描测​​试用例.

如何发现并运行这样的TestCase?有关TestCase的示例,请参阅此处.

Mat*_*ood 7

对于像这样的东西,Nose有一个"测试生成器"功能.您编写了一个生成器函数,该函数生成您希望它运行的每个"测试用例"函数及其args.按照上一个示例,这可以检查单独测试中的每个函数:

import unittest
import numpy

from somewhere import the_functions

def test_matrix_functions():
    for function in the_functions:
        yield check_matrix_function, function

def check_matrix_function(function)
    matrix1 = numpy.ones((5,10))
    matrix2 = numpy.identity(5)
    output = function(matrix1, matrix2)
    assert matrix1.shape == output.shape, \
           "%s produces output of the wrong shape" % str(function)
Run Code Online (Sandbox Code Playgroud)