在 Python 中使用 pytest 跑测试用例,一般流程如下:
pip install pytest
验证是否安装成功:
pytest --version
pytest 默认会找以下文件:
test_*.py*_test.pytest_ 开头示例 test_sample.py:
def test_add():
assert 1 + 1 == 2
def test_sub():
assert 3 - 1 == 2
pytest
pytest test_sample.py
pytest test_sample.py::test_add
pytest tests/
| 参数 | 说明 |
|---|---|
-v |
详细输出 |
-s |
显示 print 内容 |
-q |
安静模式 |
-x |
第一个失败就停止 |
--maxfail=2 |
最多失败 2 个 |
-k "add" |
只跑名字包含 add 的用例 |
--html=report.html |
生成 HTML 报告(需插件) |
示例:
pytest -v -s
pytest -k "add or sub"
pytest 使用 Python 原生 assert:
def test_demo():
assert "hello" in "hello world"
class TestMath:
def test_add(self):
assert 1 + 1 == 2
运行:
pytest test_sample.py::TestMath
pip install pytest-rerunfailures
pytest --reruns 3
如果你有 具体项目结构 / 想跑接口测试 / UI 测试 / 并行执行,可以告诉我,我可以给你更具体的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。