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

时间:2016-11-20 11:52:29

标签: python python-unittest data-driven-tests

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

1 个答案:

答案 0 :(得分:2)

这是因为error: no matching function for call to ‘sort(std::vector<A>&, int, std::vector<A>::size_type, <unresolved overloaded function type>, <unresolved overloaded function type>)’ 正在改变&#34;测试名称。如果以详细模式运行测试,您将看到:

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