gpt4 book ai didi

python - 通过命令行从 unittest.TestCase 运行单个测试 - 使用 ddt

转载 作者:行者123 更新时间:2023-11-28 17:22:34 26 4
gpt4 key购买 nike

类似于this question .但是,在使用 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))

如果我在终端中运行它,它不起作用

python3 testSuite.py TestNumbersToWords.test_should_return_correct_word

最佳答案

这是因为 ddt 正在“更改”测试名称的方式。如果您在详细模式下运行测试,您将看到:

$ 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*

关于python - 通过命令行从 unittest.TestCase 运行单个测试 - 使用 ddt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40703695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com