跳過用例skip
阿新 • • 發佈:2019-03-15
other from pre requires mark 導入 字符 required all
1、裝飾器,放在函數前面,跳過用例 @pytest.mark.skip(reason="no way of currently testing this")
import pytest def test1(): print(‘操作1‘) print("-----------------------------------------------") @pytest.mark.skip(reason="no way of currently testing this") def test12(): print(‘操作2‘) print("-----------------------------------------------") def test3(): print(‘操作3‘) print("-----------------------------------------------") if __name__ == ‘__main__‘: pytest.main([‘-s‘, "text.fix2.py"]
2、放在函數裏面,只控制某條用例
import pytest def test_123(): pytest.skip("Not implemented") assert 1 == 0 def test_234(): assert 1 == 1
3、跳過某個模塊
@pytest.importskip("模塊名")
4、根據版本去控制跳過某個模塊
@pytest.importskip("模塊名",minversion="version_num")
5、如果您希望有條件地跳過某些內容,則可以使用skipif代替
import sys @pytest.mark.skipif(sys.version_info < (3,6), reason="requires python3.6 or higher") def test_function(): ...
如果條件在收集期間評估為True,則將跳過測試函數,具有指定的原因使用-rs時出現在摘要中。
您可以在模塊之間共享skipif標記。參考以下案例
# content of test_mymodule.py import mymodule minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1), reason="at least mymodule-1.1 required") @minversion def test_function(): ...
您可以導入標記並在另一個測試模塊中重復使用它:
# test_myothermodule.py from test_mymodule import minversion @minversion def test_anotherfunction(): ...
對於較大的測試套件,通常最好有一個文件來定義標記,然後一致適用於整個測試套件。
或者,您可以使用條件字符串而不是布爾值,但它們之間不能輕易共享它們支持它們主要是出於向後兼容的原因
6、skip類或模塊
@pytest.mark.skipif(sys.platform == ‘win32‘, reason="does not run on windows") class TestPosixCalls(object): def test_function(self): "will not be setup or run under ‘win32‘ platform"
警告:強烈建議不要在使用繼承的類上使用skipif。 pytest中的一個已知錯誤標記可能會導致超類中的意外行為。
跳過用例skip