![](/img/trans.png)
[英]Running a single test from unittest.TestCase via the command line
[英]Running single test from unittest.TestCase via command line - with ddt
與此問題類似。 但是,當使用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*
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.