2015-04-01 65 views
2

我在測試模塊中寫了一組測試用例,例如Test1,Test2。Python單元測試:在鼻子有沒有一種方法可以從nose.run()跳過測試用例?

有沒有辦法跳過Test1或有選擇地使用命令nose.main()在該模塊中執行Test2?

我的模塊包含,

test_module.py,

class Test1: 
    setUp(self): 
     print('setup') 
    tearDown(self): 
     print('teardown') 
    test(self): 
     print('test1') 

class Test2: 
    setUp(self): 
     print('setup') 
    tearDown(self): 
     print('teardown') 
    test(self): 
     print('test2') 

我使用不同的Python文件運行它,

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

可能重複http://stackoverflow.com/questions/21936292/conditional- skip-testcase-decorator-in-nosetests) – salparadise 2015-04-01 18:28:33

+0

但是我不想在這裏使用裝飾器,因爲我不想一旦寫入就觸摸測試模塊代碼。我想用主模塊的選項跳過它們。我在這裏錯過了一些東西嗎? – 2015-04-01 18:34:57

回答

2

跳繩測試,而不是運行測試的概念在鼻子環境中是不同的:在測試結果結束時,跳過的測試將被報告爲跳過。如果你想跳過測試,你將不得不用修飾器來修補你的測試模塊或者做一些其他的黑暗魔法。

但是,如果您只想不運行測試,您可以按照您在命令行中執行測試的方式執行測試:使用--exclude選項。它需要你不想運行的測試的正則表達式。事情是這樣的:

import sys 
import nose 

def test_number_one(): 
    pass 

def test_number_two(): 
    pass 

if __name__ == '__main__': 
    module_name = sys.modules[__name__].__file__ 

    nose.main(argv=[sys.argv[0], 
        module_name, 
        '--exclude=two', 
        '-v' 
        ]) 

運行測試會給你:

$ python stackoverflow.py 
stackoverflow.test_number_one ... ok 

---------------------------------------------------------------------- 
Ran 1 test in 0.002s 

OK 
的[條件跳過nosetests TestCase的裝飾(
相關問題