文章目录
本系列总结是基于pytest7.2产品文档的总结,要求python版本是3.7及以上,查看 pytest产品文档:
1.1 Get Start
1.1.1 安装及版本检查
pip install -U pytest
pytest --version
1.1.2 创建第一个测试
def func(x):
return x + 1
def test_answer():
assert fun(3) == 5
C:\Users\mc\Desktop\python基础>pytest
========================= test session starts ==========================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1
rootdir: C:\Users\mc\Desktop\python基础
collected 1 item
test_sample.py F [100%]
=============================== FAILURES ===============================
_____________________________ test_answer ______________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:5: AssertionError
======================= short test summary info ========================
FAILED test_sample.py::test_answer - assert 4 == 5
========================== 1 failed in 0.12s ===========================
[100%]是指运行所有测试用例的总体进度。在它完成后,pytest将显示一个失败报告,因为func(3)不返回5。
1.1.3 运行多个测试
Pytest将在当前目录及其子目录中运行以test_*.py
或*_test.py
为形式的所有文件。更一般地说,它遵循标准的测试发现规则。
1.1.4 声明引发的异常
使用引发辅助器来断言某些代码会引发异常:
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
-q == --quit,表示安静、简短的运行结果报告输出:
C:\Users\mc\Desktop\python基础>pytest -q test_sysexit.py
. [100%]
1 passed in 0.01s
1.1.5 对一个类中的多个测试进行分组
一旦开发了多个测试,您可能希望将它们分组到一个类中。pytest使创建一个包含多个测试的类很容易:
# content of test_class.py
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
Pytest按照其Python测试发现的约定发现所有测试,因此它找到两个test_前缀函数。不需要对任何东西进行子类,但是请确保在类的前缀添加Test,否则类将被跳过。我们可以通过传递模块的文件名来运行模块:
C:\Users\mc\Desktop\python基础>pytest -q test_class.py
.F [100%]
=============================== FAILURES ===============================
__________________________ TestClass.test_two __________________________
self = <test_class.TestClass object at 0x000001D68779BA30>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:9: AssertionError
======================= short test summary info ========================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
1 failed, 1 passed in 0.11s
当在类中对测试进行分组时,需要注意的是,每个测试都有一个类的唯一实例。让每个测试共享相同的类实例将对测试隔离非常有害,并将导致糟糕的测试实践。如下所示:
class TestClassDemoInstance:
value = 0
def test_one(self):
self.value = 1
assert self.value == 1
def test_two(self):
assert self.value == 1
C:\Users\mc\Desktop\python基础>pytest -k TestClassDemoInstance -q
.F [100%]
=============================== FAILURES ===============================
____________________ TestClassDemoInstance.test_two ____________________
self = <test_class.TestClassDemoInstance object at 0x000001C54B6700A0>
def test_two(self):
> assert self.value == 1
E assert 0 == 1
E + where 0 = <test_class.TestClassDemoInstance object at 0x00000
1C54B6700A0>.value
test_class.py:20: AssertionError
======================= short test summary info ========================
FAILED test_class.py::TestClassDemoInstance::test_two - assert 0 == 1
1 failed, 1 passed, 2 deselected in 0.26s
1.1.6 为功能测试请求一个唯一的临时目录
Pytest提供了内置固定装置/函数参数来请求任意的资源,比如一个唯一的临时目录:
def test_needsfiles(tmp_path):
print(tmp_path)
assert 0
在测试函数签名中列出名称tmp_path,pytest将在执行测试函数调用之前查找并调用一个设备工厂来创建资源。在测试运行之前,pytest创建一个唯一的每个测试调用临时目录:
C:\Users\mc\Desktop\python基础>pytest -q test_tmp_path.py
F [100%]
=============================== FAILURES ===============================
___________________________ test_needsfiles ____________________________
tmp_path = WindowsPath('C:/Users/mc/AppData/Local/Temp/pytest-of-mc/pytes
t-0/test_needsfiles0')
def test_needsfiles(tmp_path):
print(tmp_path)
> assert 0
E assert 0
test_tmp_path.py:3: AssertionError
------------------------- Captured stdout call -------------------------
C:\Users\mc\AppData\Local\Temp\pytest-of-mc\pytest-0\test_needsfiles0
======================= short test summary info ========================
FAILED test_tmp_path.py::test_needsfiles - assert 0
1 failed in 0.11s