源码: https://github.com/pytest-dev/pytest
文档: https://docs.pytest.org/en/8.2.x/
安装: pip install pytest
简单样例
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
命令行执行python
可以看到执行结果
命名规范
- 测试模块: 文件以
test_
开头 - 测试类: 以
Test
开头 - 测试方法: 函数以
test
开头
# 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 -q test_class.py
执行方式
# Run tests in a module
pytest test_mod.py
# Run tests in a directory
pytest testing/
# Run tests by keyword expressions
# 执行 TestMyClass.test_something, 不执行 TestMyClass.test_method_simple
pytest -k 'MyClass and not method'
标签:tests,Run,Python,py,pytest,自动化,test,def
From: https://www.cnblogs.com/rustling/p/18249996