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

Running single test from unittest.TestCase via command line - with ddt

类似于this question. However the accepted solution doesn't work for me when using 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 是 "changing" 测试名称的方式。如果您 运行 在详细模式下进行测试,您将看到以下内容:

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