通过命令行从unittest.TestCase运行单个测试 - 使用ddt
问题描述:
类似于this question。但是,当使用ddt时,接受的解决方案对我无效。通过命令行从unittest.TestCase运行单个测试 - 使用ddt
例如:
def numbers_to_words(num):
if(num == 1): return 'One'
if(num == 2): return 'Two'
if(num == 3): return 'Three'
raise Error
@ddt
class TestNumbersToWords(unittest.TestCase):
@unpack
@data((1, 'One'), (2, 'Two'), (3, 'Three'))
def test_should_return_correct_word(self, input, expected):
self.assertEqual(expected, numbers_to_words(input))
如果我跑这在终端不因道路ddt
是“改变”的测试工作,名字
python3 testSuite.py TestNumbersToWords.test_should_return_correct_word
答
这是。如果您在详细模式下运行你的测试,这是你会看到:
$ python testSuite.py -v
test_should_return_correct_word_1__1___One__ (__main__.TestNumbersToWords) ... ok
test_should_return_correct_word_2__2___Two__ (__main__.TestNumbersToWords) ... ok
test_should_return_correct_word_3__3___Three__ (__main__.TestNumbersToWords) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
正如你所看到的,test_should_return_correct_word
这里不存在。但是你可以提供关于正在运行的方法的真实姓名,而且它会工作:
$ python test_a.py TestNumbersToWords.test_should_return_correct_word_1__1___One__
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
但你不会能够运行所有匹配模式的测试,比如TestNumbersToWords.test_should_return_correct_word*
。
我明白了。重命名有一种模式吗?像是第一个测试总是附加'_1 __ {first_arg} __ {second_arg} __? – syclee
该文档指出格式为“original_test_name_ {ordinal} _ {data}'。ordinal是数据参数的位置,以1开头,但不是很清楚。另一方面,也许你可以尝试pytest和它的'parametrize'装饰器,它可以做类似的事情:http://doc.pytest.org/en/latest/parametrize.html – julienc