2017-04-20 79 views
0

我在pycharm中運行unittests時遇到問題。第一類'KnownValues'運行,但其他類不會被檢查。PyCharm似乎並未運行所有單元測試

import roman 
import unittest 

class KnownValues(unittest.TestCase): 

    def test_too_large(self): 
     '''to_roman should fail with large input''' 
     self.assertRaises(roman.OutOfRangeError, roman.to_roman, 4000) 
    def test_too_small(self): 
     ls = [0,-1,-25,-60] 
     for x in ls: 
      self.assertRaises(roman.OutOfRangeError, roman.to_roman, x) 
    def test_non_int(self): 
     ls = [1.5, -6.5, 6.8,12.9, "hello wold", "nigga123"] 
     for x in ls: 
      self.assertRaises(roman.TypeError, roman.to_roman, x) 

class Test2(unittest.TestCase): 
    def test1(self): 
     assert 1 == 1 


if __name__ == '__main__': 
    unittest.main() 
+0

您是否驗證過,這與您在PyCharm外部運行程序時的行爲不同? – holdenweb

+0

如果將測試方法的名稱從test1更改爲test_1會怎樣? – Charlie

+0

@Charlie no pycharm即使我將其更改爲test_1,它也不運行1 – Bl4ckC4t

回答

3

test開始所有測試功能。很多人使用下劃線來分隔單詞,所以很多人最終都會從test_開始測試,但是test就是所有必需的。

在GUI中遇到問題時,可以從命令行檢查測試的運行方式。

python test.py 

,你可能會遇到
python -m test 

一個問題是,你的類中定義你的測試,並通過圖形用戶界面運行它們時,該GUI已自動發現它們。請確保在測試文件末尾包含行,指導口譯員使用unittest中內置的main函數。

if __name__ == '__main__': 
    unittest.main() 

請記住,您可以選擇在同一時間運行在只有你的類中的一個測試:

python tests.py KnownValues 
python tests.py Test2 

在PyCharm,它會自動發現所有的測試類。您仍然可以一次只運行一個課程。選擇「運行」 - >「編輯配置」以查看當前正在運行的選項。使用命令行參數可以控制運行更少或更多的測試。 enter image description here

如您所見,您可以選擇運行腳本,類或方法。請務必設置運行配置的名稱,使其反映您正在運行的範圍。

1

我只需要運行與測試文件「運行單元測試在測試」,而不是「調試單元測試中KnownValues」工作。(測試是我單元測試文件「test.py」的文件名)

相關問題