1. 程式人生 > 實用技巧 >pytest(三)--pycharm執行pytest

pytest(三)--pycharm執行pytest

上一篇已經介紹瞭如何在cmd執行pytest用例,那麼pycharm下如何執行pytest用例呢?

pycharm執行三種方式

1.以xx.py指令碼方式直接執行,當寫的程式碼裡面沒用到unittest和pytest框架時,並且指令碼名稱不是以test_開頭命名的,此時pycharm會以xx.py指令碼方式執行。

2.當指令碼命名為test_xx.py時,用到unittest框架,此時執行程式碼,pycharm會自動識別到以unittest方式執行

3.以pytest方式執行,需要改該工程設定預設的執行器:file—>Setting—>Tools—>Python—>Python Integrated Tools—>Default test runner—>選擇pytest

備註:pytest是可以相容unittest框架程式碼的;無框架的程式碼,仍然是 Run '檔名'

Pycharm寫pytest程式碼

1.先匯入pytest

#D:\Python0811\2020\test_0728\test_class.py
import pytest
class TestClass:
    def test_onea(self):
        x="this"
        assert 'h' in x
    def test_twob(self):
        x="hello"
        assert hasattr(x,'check')
    def test_threec(self):
        a="hello"
        b="hello world"
        assert a in b
if __name__=="__main__":
    #pytest.main('-q test_class.py')
    pytest.main(['-q', 'test_class.py']) #括號裡傳列表或字串,效果一樣

   

執行結果;".F",F是Fail的意思。