Python中Pytest如何使用

  

Python中Pytest如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<强> 2,Pytest安装

<>之前,,,import  Pytest   ,,,class  Test_class:   ,,,,,,,def  test_001(自我):   ,,,,,,,,,,,印刷(& # 39;用例001 & # 39;)   ,,,,,,,,,,,assert  8,==8   ,,,,,,,def  test_002(自我):   ,,,,,,,,,,,印刷(& # 39;用例002 & # 39;)   ,,,,,,,,,,,assert  6,==6   ,,,,,,,def  test_003(自我):   ,,,,,,,,,,,印刷(& # 39;用例003 & # 39;)   ,,,,,,,,,,,assert  3,==2   ,,,if  __name__ ==,“__main__":   ,,,#,里面参数需要传名单,多个参数放列表就不会有警告了   #,pytest.main (& # 39; -q  test_class.py& # 39;)   ,,,,,,,pytest.main ((& # 39; q # 39;,, & # 39; test_class.py& # 39;])

<强> 3,创建一个简单的测试案例

<>之前,,,import  pytest   ,,,class  Test_class:   ,,,,,,,def  test_001(自我):   ,,,,,,,,,,,印刷(& # 39;用例001 & # 39;)   ,,,,,,,,,,,assert  8,==8   ,,,,,,,def  test_002(自我):   ,,,,,,,,,,,印刷(& # 39;用例002 & # 39;)   ,,,,,,,,,,,assert  6,==6   ,,,,,,,def  test_003(自我):   ,,,,,,,,,,,印刷(& # 39;用例003 & # 39;)   ,,,,,,,,,,,assert  3,==2   ,,,if  __name__ ==,“__main__":   ,,,#,里面参数需要传名单,多个参数放列表就不会有警告了   #,pytest.main (& # 39; -q  test_class.py& # 39;)   ,,,,,,,pytest.main ((& # 39; q # 39;,, & # 39; test_class.py& # 39;])

<强>执行结果

pytest中用例的检查点直接用Python的断言断言。

断言后面的表达式结果为真,就是检查点通过,结果为False,就是检查点不通过。

 Python中pytest如何使用“> </p> <p>执行测试的时候,我们只需要在测试文件test_class所在的目录下,运行py . test即可.pytest会在当前的目录下,寻找以测试开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找test_开头的测试函数并执行。</p> <p> <强> 4,Pycharm设置pytest </强> </p> <pre> #文件→设置→工具→Python  Integrated 工具→项目名称→Default  test 运动员→选择py . test
  #右键选择pytest运行或者直接运行。py文件</pre> <p> <强>执行结果</强> </p> <p> <img src=看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

Python中Pytest如何使用