我该如何发现动态生成的测试用例? [英] How do I get nose to discover dynamically-generated testcases?

查看:122
本文介绍了我该如何发现动态生成的测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在上一个问题中,探索了在整个功能系列中实施实质上相同的测试的方法,以确保测试不会在第一个失败的功能处停止.

In the previous question, methods were explored to implement what was essentially the same test over an entire family of functions, ensuring testing did not stop at the first function that failed.

我的首选解决方案使用元类将测试动态地插入到unittest.TestCase中.不幸的是,鼻子没有捡起来,因为鼻子会静态扫描测​​试用例.

My preferred solution used a metaclass to dynamically insert the tests into a unittest.TestCase. Unfortunately, nose does not pick this up because nose statically scans for test cases.

我如何发现并运行这样的TestCase?请在此处有关所讨论的TestCase的示例.

How do I get nose to discover and run such a TestCase? Please refer here for an example of the TestCase in question.

推荐答案

Nose为此类问题提供了测试生成器"功能.您编写了一个生成器函数,该函数生成您希望其运行的每个测试用例"函数及其args.按照您先前的示例,这可以在单独的测试中检查每个功能:

Nose has a "test generator" feature for stuff like this. You write a generator function that yields each "test case" function you want it to run, along with its args. Following your previous example, this could check each of the functions in a separate test:

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)

这篇关于我该如何发现动态生成的测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆