这是一个测试用例
ch1/test_one.py def test_passing(): assert (1, 2, 3) == (1, 2, 3)
执行
cd /ch1
pytest test_one.py
结果
(venv) C:\Users\admin\Desktop\ch1>pytest test_one.py ======================================= test session starts ==================== platform win32 -- Python 3.6.8, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\admin\Desktop\ch1 collected 1 item test_one.py . [100%] ========================================= 1 passed in 0.01s ====================
这是第二个测试用例
ch1/test_two.py def test_passing(): assert (1, 2, 3) == (3, 2, 1)
运行后结果
(venv) C:\Users\admin\Desktop\ch1>pytest test_two.py ================================================ test session starts ================================ platform win32 -- Python 3.6.8, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\admin\Desktop\ch1 collected 1 item test_two.py F [100%] =========================== FAILURES ================================= ________________________________________ test_passing ____________________ def test_passing(): > assert (1, 2, 3) == (3, 2, 1) E assert (1, 2, 3) == (3, 2, 1) E At index 0 diff: 1 != 3 E Use -v to get the full diff test_two.py:2: AssertionError =================================== short test summary info =================================== FAILED test_two.py::test_passing - assert (1, 2, 3) == (3, 2, 1) ==================================== 1 failed in 0.03s ========================================
pytest的官方文档地址
https://docs.pytest.org
pytest通过PyPI(Python官方包管理索引)分发托管:
https://pypi.python.org/pypi/pytest
建议使用vritualenv来使用
pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
在没有其他参数的情况下,pytest会递归遍历每个目录及其子目录。
举一个例子,我们创建一个tasks子目录,并且创建以下测试文件:
ch1/tasks/test_three.py """Test the Task data type.""" from collections import namedtuple Task = namedtuple(‘Task‘, [‘summary‘, ‘owner‘, ‘done‘, ‘id‘]) Task.__new__.__defaults__ = (None, None, False, None) # 指定默认值 def test_defaults(): """Using no parameters should invoke defaults""" t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2 def test_member_access(): """Check .field functionality of namedtuple.""" t = Task(‘buy milk‘, ‘brian‘) assert t.summary == ‘buy milk‘ assert t.owner == ‘brian‘ assert (t.done, t.id) == (False, None)
下面演示下_asdict() 函数和 _replace() 函数的功能:
# ch1/tasks/test_four.py """Type the Task data type""" from collections import namedtuple Task = namedtuple(‘Task‘, [‘summary‘, ‘owner‘, ‘done‘, ‘id‘]) Task.__new__.__defaults__ = (None, None, False, None) def test_asdict(): """_asdict() should return a dictionary""" t_task = Task(‘do something‘, ‘okken‘, True, 21) t_dict = t_task._asdict() expected = {‘summary‘: ‘do something‘, ‘owner‘: ‘okken‘, ‘done‘: True, ‘id‘: 21} assert t_dict == expected def test_replace(): """replace() should change passed in fields""" t_before = Task(‘finish book‘, ‘brian‘, False) t_after = t_before._replace(id=10, done=True) t_expected = Task(‘finish book‘, ‘brian‘, True, 10) assert t_after == t_expected
运行时
cd ch1
pytest
如果不指定,pytest会搜索当前目录及子目录中以test_开头或者以_test结尾的测试函数
我们把 pytest 搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵守命名规则,就能自动搜索。以下是几条主要的命名规则
原文:https://www.cnblogs.com/dongye95/p/13488235.html