上一篇已经介绍了如何在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的意思。