2016-12-02 42 views
0

我想基于单元测试的名称创建多个测试套件,这有可能吗?如何将所有名称与模式匹配的测试添加到单元测试套件中?

上下文是我有大量的单元测试文件与两种类型的测试:一些检查返回的值是纠正,其他人检查值是在合理的时间内计算。

现在我想运行我的机器上的计时测试,但不是在我的持续集成构建上只测试第一个测试。

希望我所有的时间测试具有相同的名称(但在不同的文件):test_problem_one_minute

我的测试是在一个unittest.TestCase类。

显然我不想在我的测试套件中手动添加所有的测试,其中有太多。有没有办法做到这一点?或者我应该在调用python时在命令行中使用该模式?

回答

0

在本Q/A说,一种解决方案是使用TestLoader并修改其testMethodPrefix值:

test_runner.py

# Loaders 
loader_foo = unittest.TestLoader() 
loader_bar = unittest.TestLoader() 
loader_timing = unittest.TestLoader() 

# Prefixes 
loader_foo.testMethodPrefix = "test_problem_foo" 
loader_bar.testMethodPrefix = "test_problem_bar" 
loader_timing.testMethodPrefix = "test_problem_one_minute" 

# Suites 
suite_foo = loader_foo.discover('.', pattern="test_*.py") 
suite_bar = loader_bar.discover('.', pattern="test_*.py") 
suite_timing = loader_timing.discover('.', pattern="test_*.py") 
suite_notiming = unittest.TestSuite((suite_foo, suite_bar)) 
suite_full = unittest.TestSuite((suite_foo, suite_bar, suite_timing)) 

# Running the correct test suite 
if len(sys.argv) > 1: 
    if sys.argv[1] == "notiming": 
     unittest.TextTestRunner().run(suite_no_timing) 
    elif [...] 

并调用test_runner.py这样的:

python test_runner.py notiming