pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以上的插件,同时兼容 unittest 框架。
1、安装
pip install pytest2、安装后检查
pip show pytest3、简单测试
# -*- coding: utf-8 -*-
# @Time : 2020/2/29 13:53
# @Author : lc
import pytest
def add(x, y):
return (x + y)
def test1():
assert 3 == add(1, 1)
def test2():
assert 1 != add(1, 1)
if __name__ =="__main__":
pytest.main()执行结果
C:\python3\python.exe D:/mypython/py_test/test_1.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: D:\mypython\py_test
collected 2 items
test_1.py F. [100%]
================================== FAILURES ===================================
____________________________________ test1 ____________________________________
def test1():
> assert 3 == add(1, 1)
E assert 3 == 2
E + where 2 = add(1, 1)
test_1.py:10: AssertionError
========================= 1 failed, 1 passed in 0.07s =========================
Process finished with exit code 04、使用 pytest 执行测试需要遵行的规则
- .py 测试文件必须以test_开头(或者以_test结尾)
- 测试类必须以Test开头,并且不能有 init 方法
- 测试方法必须以test_开头
- 断言必须使用 assert
5、pytest 框架也有类似于 setup 和 teardown 的用法
用例运行级别
模块级(setup_module/teardown_module)开始于模块始末,属于全局的;
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)类在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后setup_function/teardown_function 每个用例开始和结束调用一次
# -*- coding: utf-8 -*-
# @Time : 2020/2/29 13:53
# @Author : lc
import pytest
def setup_function():
print('...............setup')
def teardown_function():
print('..............teardowm')
def add(x, y):
return (x + y)
def test1():
assert 2 == add(1, 1)
def test2():
assert 1 != add(1, 1)
if __name__ =="__main__":
pytest.main(['test_1.py','-s','-v'])执行结果
C:\python3\python.exe D:/mypython/py_test/test_1.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- C:\python3\python.exe
cachedir: .pytest_cache
rootdir: D:\mypython\py_test
collecting ... collected 2 items
test_1.py::test1 .............setup
PASSED............teardowm
test_1.py::test2 .............setup
PASSED............teardowm
============================== 2 passed in 0.02s ==============================
Process finished with exit code 06、fixture
fixture 相对于 setup和 teardown 来说有以下几点优势:
- 命名方式灵活,不局限于 setup 和 teardown 这几个命名
- conftest.py 配置里可以实现数据共享,不需要 import 就能自动找到配置
- scope="module" 可以实现多个.py 跨文件共享前置
- scope="session" 以实现多个.py 跨文件使用一个 session 来完成多个用例
fixture 函数的定义
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""
:arg scope: 可选四组参数:function(默认)、calss、module、package/session
:arg params: 一个可选的参数列表,它将导致多个参数调用fixture函数和所有测试使用它。
:arg autouse: 如果为True,则fixture func将为所有测试激活可以看到它。如果为False(默认值),则需要显式激活fixture。
:arg ids: 每个参数对应的字符串id列表,因此它们是测试id的一部分。如果没有提供id,它们将从参数中自动生成。
:arg name: fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名 “fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')”。
"""重点说下 scope 四组参数的意义:
- function:每个方法(函数)都会执行一次。
- class:每个类都会执行一次。类中有多个方法调用,只在第一个方法调用时执行。
- module:一个 .py 文件执行一次。一个.py 文件可能包含多个类和方法。
- package/session:多个文件调用一次,可以跨 .py 文件
# -*- coding: utf-8 -*-
# @Time : 2020/2/29 15:14
# @Author : lc
import pytest
@pytest.fixture
def login():
print("登录")
def test_1():
print('测试用例1')
def test_2(login):
print('测试用例2')
if __name__ =="__main__":
pytest.main(['test_2.py','-s'])7、执行指定用例并详细显示执行信息
pytest -vv py_test/test1.py8、执行测试并计算每个测试用例使用的时间
pytest -vv --durations=0 py_test/test_1.py
但行好事,莫问前程
















