1、python脚本中执行测试用例
pytest中使用main()函数执行测试用例:
pytest_demo/
├── test_sample.py
├── test_class.py
└── test_main.py
假设我们已经完成了test_sample.py和test_class.py的测试用例,在test_main.py中调用main()函数执行。
test_main.py代码如下:
import pytest
if __name__ == '__main__':
pytest.main()
运行test_main.py就会执行当前目录下所有test_.py或_test.py中的测试用例。
main() 默认执行了当前文件所在的目录下的所有测试文件。
那么,如果我们只想运行某个测试文件呢?可以向main()中添加参数,就像在cmd命令提示符下面一样:
#coding=utf-8
import pytest
def test_main():
assert 5 != 5
if __name__ == '__main__':
pytest.main("-q test_main.py") # 指定测试文件,-q 为quiet。表示在安静的模式输出报告
那如果我想运行某个目录下的测试用例呢?指定测试目录即可。
#coding=utf-8
import pytest
def test_main():
assert 5 != 5
if __name__ == '__main__':
pytest.main("d:/pyse/pytest/") # 指定测试目录
2、生成测试报告:
其实只需要在上边的main()函数中加上对应的参数即可。
详细信息可参考:pytest生成测试报告