pip install pytest
pytest [options] [file_or_dir] [file_or_dir] [...]
如果不提供任何参数,pytest会在当前目录以及子目录下寻找测试文件,然后运行测试用例,如果提供了文件名或目录名,pytest会诸葛查找并运行所有的测试用例。
目录结构
.
├── __init__,py
├── test_001.py
└── test_002.py
test_001.py
#!/usr/bin/python3
#-*- conding:utf-8 -*-
def test_one():
print(‘test_one‘)
def test_two():
print(‘test_two‘)
class Test_Class():
def test_three(self):
print(‘test_three‘)
test_2.py
#!/usr/bin/python3
#-*- conding:utf-8 -*-
def test_1():
print(‘test_1‘)
执行当前目录下所有的测试用例
pytest
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 4 items
test_001.py ... [ 75%]
test_002.py . [100%]
=================================================== 4 passed in 0.02s ====================================================
执行指定模块中的所有测试用例
pytest test_one.py
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items
test_001.py ... [100%]
=================================================== 3 passed in 0.02s ====================================================
执行指定模块中的指定测试用例
指定类
pytest test_001.py::Test_Class
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item
test_001.py . [100%]
=================================================== 1 passed in 0.01s ====================================================
指定类中的指定方法
pytest test_001.py::Test_Class::test_three
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item
test_001.py . [100%]
=================================================== 1 passed in 0.01s ====================================================
指定函数
pytest test_001.py::test_two
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item
test_001.py . [100%]
=================================================== 1 passed in 0.01s ====================================================
原文:https://www.cnblogs.com/jingxindeyi/p/13258137.html