pytest基础

分類 devops, pytest

1. 概述

pytest 是一个功能强大的 Python 测试框架,具有以下特点:

  • 简单灵活,文档丰富
  • 支持参数化测试
  • 可用于单元测试、功能测试、自动化测试(如 Selenium/Appium)和接口测试
  • 丰富的插件生态系统
  • 支持测试跳过(skip)和预期失败(xfail)
  • 良好的 CI 工具集成(如 Jenkins)

主要插件

插件名称 功能描述
pytest-selenium Selenium 集成
pytest-html 生成 HTML 测试报告
pytest-rerunfailures 失败用例重试
pytest-xdist 多 CPU 并行测试

2. 使用介绍

2.1 安装

1
pip install pytest

2.2 编写规则

  • 测试文件:test_*.py*_test.py
  • 测试类:以 Test 开头,不能有 __init__ 方法
  • 测试函数:以 test_ 开头
  • 断言:使用 Python 原生 assert

2.3 示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# pytest1.py
import pytest

@pytest.fixture(scope='function')
def setup_function(request):
def teardown_function():
print("teardown_function called.")
request.addfinalizer(teardown_function)
print('setup_function called.')

@pytest.fixture(scope='module')
def setup_module(request):
def teardown_module():
print("teardown_module called.")
request.addfinalizer(teardown_module)
print('setup_module called.')

@pytest.mark.website
def test_1(setup_function):
print('Test_1 called.')

def test_2(setup_module):
print('Test_2 called.')

def test_3(setup_module):
print('Test_3 called.')

2.4 fixture 的 scope 参数

参数值 作用范围
function 每个测试函数运行一次(默认)
class 每个测试类运行一次
module 每个模块运行一次
session 每次会话运行一次

2.5 setup 和 teardown

  • setup:测试前的准备工作
  • teardown:测试后的清理工作
  • 替代方案:使用 yield 实现 setup/teardown

3. 测试执行

3.1 基本执行命令

命令 功能
pytest 运行当前目录下所有测试
pytest test_mod.py 运行指定模块的测试
pytest somepath/ 运行指定路径下的测试
pytest -k "stringexpr" 运行名称匹配表达式的测试
pytest test_mod.py::test_func 运行指定模块的特定测试函数

3.2 标记执行

1
2
3
4
5
# 执行标记为 website 的测试
pytest -v -m "website" pytest1.py

# 执行未标记为 website 的测试
pytest -v -m "not website" pytest1.py

3.3 常用命令行参数

参数 功能
-v 显示详细测试结果
-q 只显示简要结果
-s 显示 print 输出
-x 遇到第一个失败立即停止
--exitfirst -x
-h 显示帮助信息

4. 扩展插件

4.1 测试覆盖率 (pytest-cov)

1
2
3
4
pip install pytest-cov

# 生成 HTML 覆盖率报告
pytest --cov-report=html --cov=./ test_code_target_dir

覆盖率参数

参数 说明
--cov=[path] 指定计算覆盖率的路径
--cov-report=type 报告类型:term, html, xml 等
--cov-fail-under=MIN 设置覆盖率最低阈值

4.2 其他实用插件

插件 功能 安装命令
pytest-randomly 随机执行测试 pip install pytest-randomly
pytest-xdist 分布式测试 pip install pytest-xdist
pytest-instafail 立即显示失败 pip install pytest-instafail

5. 示例输出

5.1 详细模式 (-v)

1
2
3
4
5
6
7
8
$ pytest -v pytest1.py
==============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0
collected 3 items

pytest1.py::test_1 PASSED
pytest1.py::test_3 PASSED
pytest1.py::test_2 PASSED

5.2 显示输出 (-s)

1
2
3
4
5
6
7
8
9
$ pytest -s pytest1.py
==============================================================================
setup_function called.
Test_1 called.
.teardown_function called.
setup_module called.
Test_2 called.
.Test_3 called.
.teardown_module called.

5.3 覆盖率报告

1
2
3
4
---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ----------------------------------------------------------------
Name Stmts Miss Cover
--------------------------------
pytest1.py 18 0 100%

6. 参考资源

  1. Python 测试工具大全
  2. 主流测试工具比较
  3. pytest 官方播客
  4. pytest 简介

留言與分享

  • 第 1 頁 共 1 頁
作者的圖片

Kein Chan

這是獨立全棧工程師Kein Chan的技術博客
分享一些技術教程,命令備忘(cheat-sheet)等


全棧工程師
資深技術顧問
數據科學家
Hit廣島觀光大使


Tokyo/Macau