import pytest
'''
安装pytest: pip install pytest
查看pytest版本: pytest --version
pytest参数帮助: pytest -h | --help
pytest遵循其Python测试发现约定来发现所有测试
因此它会找到两个带test_前缀的函数
且无需继承任何子类
'''
# import pdb
# '''
# pytest允许您在每次测试开始时通过命令行选项立即进入PDB提示符:
# pytest --trace
# '''
# pdb.set_trace()
# 简单的测试
def func_plus(x):
return x + 1
def test_plus_answer():
assert func_plus(3) == 4
def test_plus_answer1():
assert func_plus(3) == 5
print('hello')
"""
运行多个测试pytest将在当前目录及其子目录中运行所有格式为test _ *.py或* _test.py的文件
更一般而言,它遵循标准的测试发现规则
"""
def f():
# SystemExit解释器请求退出
raise SystemExit(1)
def test_my_test():
with pytest.raises(SystemExit):
f()
# 将多个测试分组到一个类中
class TestClass:
def test_include_h(self):
x = 'hello'
assert 'h' in x
def test_hasattr(self):
x = 'hello'
assert hasattr(x, 'check')
def test_needs_files(tmpdir):
"""
使用以下命令找出存在哪种内置pytest固定装置:
pytest --fixtures # shows builtin and custom fixtures
:
"""
print(tmpdir)
assert 1
# python错误推出代码
"""
python -m pytest, pytest, py.test可以从命令行通过Python解释器调用测试
运行pytest会导致六个不同的退出代码:
退出代码0: 所有测试均已收集并成功通过 .
退出代码1: 测试已收集并运行,但有些测试失败 E
退出代码2: 测试执行被用户中断 E
退出代码3: 执行测试时发生内部错误 E
退出代码4: pytest命令行用法错误 E
退出代码5: 没有收集测试 E
"""
# pytest测试
"""
pytest -x 第一次失败后停止测试过
pytest --maxfail=2 第一N次失败后停止测试过
在模块中运行测试
pytest test_mod.py
在目录中运行测试
pytest testing/
通过关键字表达式运行测试
pytest -k "MyClass and not method"
要在模块中运行特定的测试,请执行以下操作:
pytest test_mod.py::test_func
在命令行中指定测试方法的另一个示例:
pytest test_mod.py::TestClass::test_method
通过标记表达式运行测试
pytest -m slow将运行用@pytest.mark.slow装饰器装饰的所有测试
从包运行测试
pytest --pyargs pkg.testing这将导入pkg.testing并使用其文件系统位置来查找并运行测试
"""
# 详细的总结报告
'''
f -失败
E -错误
s -跳过
x -xfailed
X -xpass
p -通过
P -通过输出
a -除 pP
A -全部
可以使用多个字符,例如,要仅查看失败和跳过的测试,可以执行:
$ pytest -rfs
'''
@pytest.fixture(scope='function')
def error_fixture():
assert 0
pytest学习--基础测试
最新推荐文章于 2020-11-23 15:07:55 发布