pytest 的使用

本文详细介绍了 pytest 的使用,包括命令行选项、测试筛选、断言、异常处理、缓存管理、报告生成、夹具(fixture)的使用、测试并行执行、插件管理等。还探讨了如何捕获标准输出、创建 JUnit XML 报告、使用 conftest.py 文件共享 fixture,以及如何处理预期失败和临时工作目录。此外,还提到了一些实用的第三方插件,如 pytest-xdist 和 pytest-repeat,用于并行执行和重复运行失败的测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pytest 的使用

之前用到的,笔记记录

# 静默模式,不打印多余信息
-q
# 所有除了通过
-a
# 生成简单报告
-r
-q: 安静模式, 不输出环境信息
-v: 丰富信息模式, 输出更详细的用例执行信息
-s: 显示程序中的print/logging输出
pytest --resultlog=./log.txt 生成log
pytest --junitxml=./log.xml 生成xml报告

测试类以 “Test” 开头

输出字符列表

• f - failed
• E - error
• s - skipped
• x - xfailed
• X - xpassed
• p - passed
• P - passed with output
# for groups
• a - all except pP
• A - all
• N - none, this can be used to display nothing (since fE is the default)

清缓存、显示缓存

$ pytest --cache-clear

pytest --cache-show
# 过滤
pytest --cache-show example/*

临时目录

pytest --fixtures

tmpdir

失败后停止

pytest -x
pytest --maxfail=2

在失败后跳到 PDB 调试器

pytest -x --pdb # drop to PDB on first failure, then end test session
pytest --pdb --maxfail=3 # drop to PDB for first three failures

开始时跳到PDB调试器

pytest --trace
# This will invoke the Python debugger at the start of every test.

设置断点

import pdb
pdb.set_trace()

内置断点函数

# When breakpoint() is called and PYTHONBREAKPOINT is set to the default value, pytest will use the
# custom internal PDB trace UI instead of the system default Pdb

分析测试执行持续时间

# To get a list of the slowest 10 test durations:
pytest --durations=10

faulthandler

# 超时  (不可用   待深究)
faulthandler_timeout=X
  • ### Creating JUnitXML format files

  • ### Calling pytest from Python code

assert a % 2 == 0, "value was odd, should be even"

关于预期异常的断言 p26

import pytest

def test_zero_division():
	with pytest.raises(ZeroDivisionError):
		1 / 0
        
#and if you need to have access to the actual exception info you may use:
def test_recursion_depth():
	with pytest.raises(RuntimeError) as excinfo:
		def f():
			f()
		f()
	assert "maximum recursion" in str(excinfo.value)
    
    
def myfunc():
	raise ValueError("Exception 123 raised")
def test_match():
	with pytest.raises(ValueError, match=r".* 123 .*"):
		myfunc()
pytest.raises(ExpectedException, func, *args, **kwargs)

conftest:共享fixture 函数

1.conftest.py文件名字是固定的,不可以做任何修改

2.文件和用例文件在同一个目录下,那么conftest.py作用于整个目录

3.conftest.py文件所在目录必须存在__init__.py文件

4.conftest.py文件不能被其他文件导入

5.所有同目录测试文件运行前都会执行conftest.py文件

pytest 一次只缓存一个fixture实例。意味着在使用参数化的fixture时,pytest可能在给定范围内多次调用一个fixture

夹具

fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and
tests according to configuration and component options, or to re-use fixtures across function, class, module or
whole test session scopes.

内置夹具

capfd Capture, as text, output to file descriptors 1 and 2.
capfdbinary Capture, as bytes, output to file descriptors 1 and 2.
caplog Control logging and access log entries.
capsys Capture, as text, output to sys.stdout and sys.stderr.
capsysbinary Capture, as bytes, output to sys.stdout and sys.stderr.
cache Store and retrieve values across pytest runs.
doctest_namespace Provide a dict injected into the docstests namespace.
monkeypatch Temporarily modify classes, functions, dictionaries, os.environ, and other objects.
pytestconfig Access to configuration values, pluginmanager and plugin hooks.
record_property Add extra properties to the test.
record_testsuite_property Add extra properties to the test suite.
recwarn Record warnings emitted by test functions.
request Provide information on the executing test function.
testdir Provide a temporary test directory to aid in running, and testing, pytest plugins.
tmp_path Provide a pathlib.Path object to a temporary directory which is unique to each test
function.
tmp_path_factory Make session-scoped temporary directories and return pathlib.Path objects.
tmpdir Provide a py.path.local object to a temporary directory which is unique to each test function;
replaced by tmp_path.
tmpdir_factory Make session-scoped temporary directories and return py.path.local objects;
replaced by tmp_path_factory.

捕捉屏幕标准输出的两种方式

  • 通过StringIO模块
from io import StringIO

s = StringIO()
    with redirect_stdout(s):
        type_verb = TypeVerb()
        type_verb.add_arguments(parser=parser, cli_name=cli_name)
        _main = type_verb.main(args=args)
        expected_output = _generate_expected_output()
        assert _main == 0
        assert expected_output == s.getvalue().splitlines()
    s.close()
  • 通过内置夹具capfd/ capsys/
import os

def test_system_echo(capfd):
	os.system('echo "hello"')
	captured = capfd.readouterr()
	assert captured.out == "hello\n"
    
def test_system_echo(capsys):
	os.system('echo "hello"')
	captured = capfd.readouterr()
	assert captured.out == "hello\n"

指定(选择)测试

  • 可根据模块、目录、关键字来做判断

  • ​ 文件名

  • ​ 函数名

  • ​ 类名

每个收集到的测试,都会分配一个唯一的节点 ID ,该 ID 有模块文件名和后面的说明符组成

# module
pytest test_mod.py::test_func
# method
pytest test_mod.py::TestClass::test_method
# by marker
pytest -m slow
# from packages
pytest --pyargs pkg.testing

修改堆栈打印

pytest --showlocals # show local variables in tracebacks
pytest -l # show local variables (shortcut)
pytest --tb=auto # (default) 'long' tracebacks for the first and last
# entry, but 'short' style for the other entries
pytest --tb=long # exhaustive, informative traceback formatting
pytest --tb=short # shorter traceback format
pytest --tb=line # only one line per failure
pytest --tb=native # Python standard library formatting
pytest --tb=no # no traceback at all
  • pytest-xdist 多CPU分发并行执行用例

pip3 install pytest-xdist

pytest-sugar

彩色输出\进度条\

pytest-rerunfailures

# 失败重跑
pytest -sv test_run.py --reruns 5

pytest-repeat

# 失败重跑
pytest -sv test_run.py --count=3
# 上面的结果是一个执行三次之后再执行下一个用例三次,但有时我们想一个用例执行完一次后执行下一个,在执行第二次。这时候就要加上--repeat-scope参数
pytest -sv test_run.py --count=3 --repeat-scope=session

Factories as fixtures

夹具不直接返回数据,而是返回一个生成数据的函数

@pytest.fixture
def make_customer_record():
	def _make_customer_record(name):
		return {"name": name, "orders": []}
	return _make_customer_record
	
def test_customer_records(make_customer_record):
	customer_1 = make_customer_record("Lisa")
	customer_2 = make_customer_record("Mike")
	customer_3 = make_customer_record("Meredith")
    

# 管理数据
@pytest.fixture
def make_customer_record():
    created_records = []
    def _make_customer_record(name):
        record = models.Customer(name=name, orders=[])
        created_records.append(record)
        return record

    yield _make_customer_record
    for record in created_records:
        record.destroy()
    
def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")

临时工作目录

# content of conftest.py
import os
import shutil
import tempfile
import pytest

@pytest.fixture
def cleandir():
    old_cwd = os.getcwd()
    newpath = tempfile.mkdtemp()
    os.chdir(newpath)
    yield
    os.chdir(old_cwd)
    shutil.rmtree(newpath)
    
    
# content of test_tmpdir.py
import os
def test_create_file(tmpdir):
    p = tmpdir.mkdir("sub").join("hello.txt")
    p.write("content")
    assert p.read() == "content"
    assert len(tmpdir.listdir()) == 1
    assert 0

A xfail means that you expect a test to fail for some reason. A common example is a test for a feature not yet
implemented, or a bug not yet fixed. When a test passes despite being expected to fail (marked with pytest.mark.
xfail), it’s an xpass and will be reported in the test summary.

@pytest.mark.skip(reason="no way of currently testing this")

collect_ignore = ["setup.py"]
collect_ignore_glob = ["*_py2.py"]

pytest --collect-only

只收集用例,不执行

插件

• pytest-pep8: a --pep8 option to enable PEP8 compliance checking.
• pytest-flakes: check source code with pyflakes.
• pytest-timeout: to timeout tests based on function marks or global definitions.
• pytest-cov: coverage reporting, compatible with distributed testing
# If you want to find out which plugins are active in your environment you can type:
# 查看启用了哪些插件以及环境配置
pytest --trace-config

# You can prevent plugins from loading or unregister them:
# 禁用插件
pytest -p no:NAME

[pytest]
addopts = -p no:NAME

p131 日志

同名的测试用例:If you need to have test modules with the same name, you might add init.py files to your tests folder and subfolders, changing them to packages:

setup.py
mypkg/
	...
tests/
	__init__.py
foo/
	__init__.py
	test_view.py
bar/
    __init__.py
    test_view.py
    
# 推荐布局
setup.py
src/
    mypkg/
        __init__.py
        app.py
        view.py
tests/
    __init__.py
    foo/
        __init__.py
        test_view.py
    bar/
        __init__.py
        test_view.py
        
# 测试程序和应用程序一起发布的话
setup.py
mypkg/
    __init__.py
    app.py
    view.py
    test/
        __init__.py
        test_app.py
        test_view.py
        ...

p206

定位当前测试卡在了那个测试用例(需要 sudo 权限)

import psutil

for pid in psutil.pids():
    environ = psutil.Process(pid).environ()
    if "PYTEST_CURRENT_TEST" in environ:
        print(f'pytest process {pid} running: {environ["PYTEST_CURRENT_TEST"]}')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值