pytestt执行测试用例、生成测试报告

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生成测试报告

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值