上一篇pytest文檔2-用例運行規則已經介紹瞭如何在cmd執行pytest用例,日常咱們寫代碼在pycharm比較多
寫完用例以後,須要調試看看,是否是能正常運行,若是每次跑去cmd執行,太麻煩,因此頗有必要學習如何在pycharm裏面運行pytest用例html
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框架代碼的this
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')
運行結果code
.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就不會有警告了htm
pytest.main(['-q', 'test_class.py'])blog
1.新建一個工程後,左上角file->Setting->Tools->Python Integrated Tools->項目名稱->Default test runner->選擇py.testthree
2.改完以後,再從新建個腳本(注意是先改項目運行方式,再寫代碼才能出來),接下來右鍵運行就能出來pytest運行了
3.pytest是能夠兼容unittest腳本的,以前寫的unittest用例也能用pytest框架去運行
---------------------------------pytest結合selenium自動化完整版-------------------------
全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
做者:上海-悠悠 QQ交流羣:874033608
也能夠關注下個人我的公衆號:yoyoketang