单元测试¶
Pytest¶
pytest 有两个比较重要的配置文件:
- pytest.ini
- setup.cfg
关键特性¶
组参数测试¶
@pytest.mark.parametrize("params_name", ["p1", "p2", "p3"])
def test_func(params_name):
print(params_name)
# 总共三次测试,params_name对应的值依次为 p1,p2和p3
标准输入输出¶
# using global variable capsys
def test_flush_print(capsys):
print("test")
captured = capsys.readouterr()
assert captured.out == "test flush print"
pytest.ini¶
忽略特定的检查项目¶
# pep8
pep8ignore = E402 E201 E231
# flake8, see https://flake8.pycqa.org/en/latest/user/error-codes.html for details
flake8-ignore = E402 F401 F403 F405 F821 F841 W605
重新设置单行长度限制¶
# pep8
pep8maxlinelength = 120
# flake8
flake8-max-line-length = 120