Pytest单元测试框架-测试用例运行规则

1.Pytest测试用例运行规则


 在pytest单元测试框架下面执行用例,须要知足如下几个特色:python

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

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

  3. 全部的包必需要有 __init__.py文件函数

通常在cmd命令行下面执行pytest用例有3种方法。你们能够选择使用,我推荐第一种:单元测试

  pytest  文件名测试

  py.test  文件名spa

  python -m pytest 文件名命令行

若是运行某个测试类下面的具体函数,可使用:pytest  文件名::测试函数名code

若是在测试过程当中,遇到测试中止的方法能够加 -x参数: pytests -x 文件名orm

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 ====================