获取更多面试真题
面试官问: 测试流程大概是什么?
考察点
-
pytest 的 fixture 是什么
-
pytest 的常用内置 fixture
技术点
-
pytest 的 fixture 的概念
-
pytest 的常用内置 fixture 清单
-
pytest 的常用内置 fixture 的使用方法
fixture的概念
-
是一种使用
@pytest.fixture定义的函数 -
通常在具体的测试函数之前或是之后运行
内置fixture清单
- 查看 fixture :
pytest --fixtures
cache -- ..._pytestcacheprovider.py:510
Return a cache object that can persist state between testing sessions.
capsys -- ..._pytestcapture.py:878
Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``.
capsysbinary -- ..._pytestcapture.py:895
Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``.
......
内置fixture:request
- request
def test_request_fixture(request):
print(f"发现的测试用例: {request.session.testscollected}")
print(f"失败的测试用例: {request.session.testsfailed}")
内置fixture:capsys
- capsys
import sys
def test_capsys_fixture_stdout(capsys):
print("Hogwarts", end="")
captured = capsys.readouterr()
assert "Hogwarts" == captured.out
def test_capsys_fixture_stderr(capsys):
sys.stderr.write("ERROR")
captured = capsys.readouterr()
assert "ERROR" == captured.err
内置fixture:tempdir
- tempdir
def test_tempdir_fixture(tmpdir):
print(f"临时目录: {tmpdir}")
总结
- pytest fixture 是一种使用
@pytest.fixture定义的,通常在具体的测试函数之前或是之后运行的函数 - 可以使用
pytest --fixtures查看 pytest 的所有 fixture 清单 - 常用的内置fixture有:
-
- request:用来获取测试函数请求信息
- capsys:用来访问被捕获的系统输出
- tempdir:用来获取一个临时目录