pytest 是一个成熟的功能丰富的python测试框架,主要特点有以下几点:
简单灵活
支持参数化,可以细粒度的控制要测试的测试用例
支持简单的单元测试和复杂的功能测试,可用作 selenium/appnium 等自动化测试、接口自动化测试
第三方插件丰富,比如pytest-html
安装pytest
pip install pytest
编写规则
- 测试文件以test_*.py 或者 *_test.py
- 测试类以Test开头,并且不能带有 __init__ 方法
- 测试函数以test_开头
- 断言使用基本的assert即可
# test_case.py
import pytest
def func(x):
return x + 1
def test_answer():
assert func(1) == 3
if __name__ == '__main__':
pytest.main(['test_case.py']) #测试模块
Console 参数介绍
- -v 用于显示每个测试函数的执行结果
- -q 只显示整体测试结果
- -s 用于显示测试函数中print()函数输出
- -x, --exitfirst, exit instantly on first error or failed test
- -h 帮助
可以在执行的时候加上参数,这时候输入的信息会更详细
if __name__ == '__main__':
pytest.main(['-s','-v', 'test_sace.py'])
或者 通过命令行执行
pytest -s -v test_case.py
标记函数
Pytest 查找测试策略
默认情况下,pytest 会递归查找当前目录下所有以 test
开始或结尾的 Python 脚本,并执行文件内的所有以 test
开始或结束的函数和方法。
# test_case.py
import pytest
def func(x):
return x + 1
def test_answer():
assert func(1) == 3
# test_file.py
def test_p():
assert 1 == 1
现在我们有以下两个测试文件,然后通过命令行执行 "pytest -v -s" 后输出结果如下: 两个文件的test函数都被执行了,
collected 2 items
test_case.py F (F表示失败) [ 50%]
test_file.py . (.表示通过) [100%]============================================================================================== FAILURES ==============================================================================================
____________________________________________________________________________________________ test_answer _____________________________________________________________________________________________def test_answer():
> assert func(1) == 3
E assert 2 == 3
E + where 2 = func(1)test_case.py:8: AssertionError
====================================================================================== short test summary info =======================================================================================
FAILED test_case.py::test_answer - assert 2 == 3
==================================================================================== 1 failed, 1 passed in 0.09s =====================================================================================
标记测试函数
由于某种原因(如 test_func2
的功能尚未开发完成),我们只想执行指定的测试函数。在 pytest 中有几种方式可以解决:
第一种,显式指定函数名,通过
::
标记。pytest test_no_mark.py::test_func1
第二种,使用模糊匹配,使用
-k
选项标识。pytest -k func1 test_no_mark.py第三种,使用
pytest.mark
在函数上进行标记。
跳过测试
刚提到 pytest 使用标记过滤测试函数,所以对于那些尚未开发完成的测试,最好的处理方式就是略过而不执行测试。
按正向的思路,我们只要通过标记指定要测试的就可以解决这个问题;但有时候的处境是我们能进行反向的操作才是最好的解决途径,即通过标记指定要跳过的测试。
Pytest 使用特定的标记 pytest.mark.skip 、
pytest.mark.skipif 完美的解决了这个问题
import pytest
def func(x):
return x + 1
@pytest.mark.skip(reason='out-of-date api')
def test_answer_2():
assert func(1) == 3
@pytest.mark.skipif(1>2,reason='out-of-date api')
def test_answer_1():
assert func(1) == 2
if __name__ == '__main__':
pytest.main(['-s','-v', 'test_case.py'])
collected 2 items
test_case.py::test_answer_2 SKIPPED (out-of-date api)
test_case.py::test_answer_1 PASSED
参数化
在pytest中,也可以使用参数化测试,即每组参数都独立执行一次测试。
假设我们现在需要对我们的func执行多种条件的测试,最简单的方法就是针对每种情况编写一个测试用例,但是这样会包含大量的重复代码。
在 pytest 中,我们有更好的解决方法,就是参数化测试,即每组参数都独立执行一次测试。使用的工具就是
pytest.mark.parametrize(argnames, argvalues)
import pytest
def func(x):
return x + 1
@pytest.mark.parametrize('num , result', [(1,2),(2,3),(6,7)])
def test_A(num, result):
assert func(num) == result
if __name__ == '__main__':
pytest.main(['-s','-v', 'test_case.py'])
collected 3 items
test_case.py::test_A[1-2] PASSED
test_case.py::test_A[2-3] PASSED
test_case.py::test_A[6-7] PASSED
运行可知执行了三次测试
Fixture(固件)
定义fixture跟定义普通函数差不多,唯一的区别就是在函数上加个装饰器@pytest.fixture()
fixture命名不能以test开头,跟用例区分开。
fixture是有返回值的,没有返回值默认返回None。
import pytest
@pytest.fixture()
def myFix():
return 1
#固件作为测试的第一个参数传入
def test_01(myFix):
assert myFix == 1
print(myFix)
if __name__ == '__main__':
pytest.main(['-s','-v', 'test_case.py'])
预处理和后处理
很多时候需要在测试前进行预处理(如新建数据库连接),并在测试完成进行清理(关闭数据库连接)。
当有大量重复的这类操作,最佳实践是使用固件来自动化所有预处理和后处理。
Pytest 使用
yield
关键词将固件分为两部分,yield
之前的代码属于预处理,会在测试前执行;yield
之后的代码属于后处理,将在测试完成后执行。以下测试模拟数据库查询,使用固件来模
拟数据库的连接关闭:
如果想更细的跟踪固件执行,可以使用 --setup-show
选项
@pytest.fixture()
def db():
print('Connection successful')
yield
print('Connection closed')
def search_user(user_id):
d = {
'001': 'xiaoming'
}
return d[user_id]
def test_search(db):
assert search_user('001') == 'xiaoming'
作用域
在定义固件时,通过
scope
参数声明作用域,可选项有:
function
: 函数级,每个测试函数都会执行一次固件;class
: 类级别,每个测试类执行一次,所有方法都可以使用;module
: 模块级,每个模块执行一次,模块内函数和方法都可使用;session
: 会话级,一次测试只执行一次,所有被找到的函数和方法都可用。
如果我们想让固件自动执行,可以在定义时指定 autouse
参数。
@pytest.fixture(scope='function')
def func_scope():
pass
@pytest.fixture(scope='module')
def mod_scope():
pass
@pytest.fixture(scope='session')
def sess_scope():
pass
@pytest.fixture(scope='class')
def class_scope():
pass
def test_multi_scope(sess_scope, mod_scope, func_scope):
pass
SETUP S sess_scope SETUP M mod_scope SETUP F func_scope tests/fixture/test_scope.py::test_multi_scope (fixtures used: func_scope, mod_scope, sess_scope). TEARDOWN F func_scope TEARDOWN M mod_scope TEARDOWN S sess_scope
测试报告
(一)pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告
安装
pip install pytest-html
生成报告
pytest --html=report.html
(二)allure
安装
1. pip install allure-pytest
2. 下载 allure
https://github.com/allure-framework/allure2/releases
3. 生成
pytest --alluredir ./ ./test_case.py
allure serve .
allure 用例描述
使用方法 | 参数值 | 参数说明 |
@allure.epic() | epic描述 | 敏捷里面的概念,定义史诗,往下是feature |
@allure.feature() | 模块名称 | 功能点的描述,往下是story |
@allure.story() | 用户故事 | 用户故事,往下是title |
@allure.title(用例的标题) | 用例的标题 | 重命名html报告名称 |
@allure.testcase() | 测试用例的链接地址 | 对应功能测试用例系统里面的case |
@allure.issue() | 缺陷 | 对应缺陷管理系统里面的链接 |
@allure.description() | 用例描述 | 测试用例的描述 |
@allure.step() | 操作步骤 | 测试用例的步骤 |
@allure.severity() | 用例等级 | 定义一个链接,在测试报告展现 |
@allure.attachment() | 附件 | 报告添加附件 |