pytest 的使用

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
pytest -x --pdb
pytest --pdb --maxfail=3
pytest --trace

import pdb
pdb.set_trace()

pytest --durations=10

faulthandler_timeout=X
  • Creating JUnitXML format files

  • Calling pytest from Python code

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

def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

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)
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 有模块文件名和后面的说明符组成


pytest test_mod.py::test_func

pytest test_mod.py::TestClass::test_method

pytest -m slow

pytest --pyargs pkg.testing
pytest --showlocals
pytest -l
pytest --tb=auto

pytest --tb=long
pytest --tb=short
pytest --tb=line
pytest --tb=native
pytest --tb=no
  • pytest-xdist 多CPU分发并行执行用例

pip3 install pytest-xdist

彩色输出\进度条\

pytest -sv test_run.py --reruns 5

pytest -sv test_run.py --count=3

pytest -sv test_run.py --count=3 --repeat-scope=session

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

@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")

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)

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

pytest --trace-config

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

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"]}')

Original: https://blog.csdn.net/qq_29935433/article/details/121480611
Author: Colin-YYYY
Title: pytest 的使用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774243/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球