Pytest单元测试框架实战之Pytest用例运行规则


1.Pytest测试用例运行规则


 在pytest单元测试框架下面执行用例,需要满足以下几个特点:

  1. 文件名以test_*.py开头或者*_test.py

  2. 测试类、测试函数以test开头

  3. 所有的包必须要有 __init__.py文件

一般在cmd命令行下面执行pytest用例有3种方法。大家可以选择使用,我推荐第一种:

  pytest  文件名

  py.test  文件名

  python -m pytest 文件名

如果运行某个测试类下面的具体函数,可以使用:pytest  文件名::测试函数名

如果在测试过程中,遇到测试停止的方法可以加 -x参数: pytests -x 文件名

E:\untitled1>pytest -x collect.py ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: E:\untitled1 collected 2 items collect.py .F [100%] ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x00000000036A4A58> def test_two(self): x = 'hello'
>       assert hasattr(x, 'check') E AssertionError: assert False E +  where False = hasattr('hello', 'check') collect.py:102: AssertionError ===================== 1 failed, 1 passed in 0.08 seconds ======================

从结果可以看出第二个测试用例没有运行成功并且停止了。当错误数达到某个量级时,测试停止,参数为:maxfail=num 示例如下:

E:\untitled1>pytest --maxfail=1 collect.py ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: E:\untitled1 collected 3 items collect.py .F ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x000000000368FCF8> def test_two(self): x = 'hello'
>       assert hasattr(x, 'check') E AssertionError: assert False E +  where False = hasattr('hello', 'check') collect.py:102: AssertionError ===================== 1 failed, 1 passed in 0.08 seconds ======================

2.在Pycharm中编写测试代码


 在编写测试代码运行之前需要把切换pytest运行环境。file-->setting-->tools-->python intergrated tools--->default test runner 切换为 py.test  然后编写如下测试代码:

import pytest class TestClass: def test_one(self): x = 'hello' assert 'h' in x def test_two(self): x = 'hello' assert hasattr(x, 'check') def test_secound(self): assert 'x' in 'ddd'

if __name__ == '__main__': pytest.main('-q test_class.py')

运行结果如下   (其中:     .表示测试结果是通过的 pass  E:表示errror  脚本中可能存在问题  F表示failed 测试结果不通过)

C:\Python35\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\helpers\pycharm\pytestrunner.py" -p pytest_teamcity E:/untitled1/test_class.py "-k TestClass and test_secound" Testing started at 17:28 ... ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\jre\jre\bin collected 3 items / 2 deselected / 1 selected . F self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self): >       assert 'x' in 'ddd' E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:106: AssertionError ================================== FAILURES =================================== ___________________________ TestClass.test_secound ____________________________ self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self): >       assert 'x' in 'ddd' E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:106: AssertionError =================== 1 failed, 2 deselected in 0.06 seconds ====================


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM