pytest文檔3-pycharm運行pytest
前言
上一篇pytest文檔2-用例運行規則已經介紹了如何在cmd執行pytest用例,平常我們寫代碼在pycharm比較多
寫完用例之後,需要調試看看,是不是能正常運行,如果每次跑去cmd執行,太麻煩,所以很有必要學習如何在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 Integrated Tools->項目名稱->Default test runner->選擇py.test
備註:pytest是可以兼容unittest框架代碼的
pycharm寫pytest代碼
1.在pycharm裏面寫pytest用例,先導入pytest
# D:/YOYO/test_class.py ** 作者:上海-悠悠 QQ交流群:588402570** import pytest class TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check') def test_three(self): a = "hello" b = "hello world" assert a in b if __name__ == "__main__": pytest.main('-q test_class.py')
運行結果
.F. [100%] ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <YOYO.test_class.TestClass object at 0x00000000039F9080> def test_two(self): x = "hello" > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check') test_class.py:11: AssertionError ============================== warnings summary =============================== <undetermined location> passing a string to pytest.main() is deprecated, pass a list of arguments instead. -- Docs: http://doc.pytest.org/en/latest/warnings.html 1 failed, 2 passed, 1 warnings in 0.06 seconds
2.運行結果“.F. ” 點是代表測試通過,F是Fail的意思,1 warnings是用於pytest.main(‘-q test_class.py‘)裏面參數需要傳list,多個參數放list就不會有警告了
pytest.main([‘-q‘, ‘test_class.py‘])
pycharm設置pytest
1.新建一個工程後,左上角file->Setting->Tools->Python Integrated Tools->項目名稱->Default test runner->選擇py.test
2.改完之後,再重新建個腳本(註意是先改項目運行方式,再寫代碼才能出來),接下來右鍵運行就能出來pytest運行了
3.pytest是可以兼容unittest腳本的,之前寫的unittest用例也能用pytest框架去運行
** 作者:上海-悠悠 QQ交流群:588402570**
pytest文檔3-pycharm運行pytest