2021-10-25 pytest run配置(含allure pytest-cov)

pytest run配置

关于–html=./report.html生成测试报告问题:
  pycharm 在运行测试用例的时候 默认是以pytest 框架来运行的,所以不能生成测试报告
  1、步骤:手动注释掉pytest代码,运行(此时就不是以pytest框架运行了)
  2、再去掉注释运行

  • Run_Report示例
    • allure 测试报告
    • cov 覆盖率
    #!/usr/bin/env python3
    # -*- coding:utf8 -*-
    
    import subprocess
    import sys
    import time
    
    day = time.strftime("%Y%m%d_%H%M", time.localtime(time.time()))
    
    reportDir = "../reports" + "/" + day
    
    if __name__ == '__main__':
        if len(sys.argv) == 1:
            subprocess.call("pytest -v -s --alluredir=%s/Allrue --cov=./ --cov-report=html:%s/Cov" % (reportDir, reportDir), shell=True
                            )
            subprocess.call("allure serve %s/Allrue" % reportDir, shell=True)
        elif len(sys.argv) == 2:
            caseType = sys.argv[1]
            caseDir = "test_" + caseType + "*"
            subprocess.call("pytest -v -s %s --alluredir=%s" % (caseDir, reportDir), shell=True)
            subprocess.call("allure serve %s" % reportDir, shell=True)
    
    

Allure 配置

默认模式
修改后

1. 添加 Environment

创建 environment.properties 文件key=value 的格式。可以通过编写相关函数动态获取每次执行时的真实值,然后写入 environment.properties 文件
然后,把文件 environment.properties 拷贝到你在执行测试用例时设置的 allure 报告目录下

Browser=Chrome
Browser.Version=86.0.4240
Environment=QA

在运行 pytest 生成 allure 报告的时候,有时候需要加 --clean 参数,删除之前的报告记录,这样会之前清空 report 目录,environment.properties文件也会被删除
为了不让 environment.properties 文件删除掉,可以把 environment.properties 文件放项目根目录,在运行报告的时候,先 copy 到 report 目录

2. 添加 categories.json

  • 产品缺陷 failed
  • 测试缺陷 broken
[
  {
    "name": "Ignored tests", 
    "matchedStatuses": ["skipped"] 
  },
  {
    "name": "Infrastructure problems",
    "matchedStatuses": ["broken", "failed"],
    "messageRegex": ".*bye-bye.*" 
  },
  {
    "name": "Outdated tests",
    "matchedStatuses": ["broken"],
    "traceRegex": ".*FileNotFoundException.*" 
  },
  {
    "name": "Product defects",
    "matchedStatuses": ["failed"]
  },
  {
    "name": "Test defects",
    "matchedStatuses": ["broken"]
  }
]

参数含义

  • name:必填项,分类的名字
  • matchedStatuses:可选项,测试用例的运行状态,默认是[“failed”, “broken”, “passed”, “skipped”, “unknown”]
  • messageRegex:可选项,测试用例运行的错误信息,使用正则表达式匹配。默认是".*"
  • traceRegex:可选项,测试用例运行的堆栈信息,使用正则表达式匹配。默认是".*"

3. 显示历次运行的 trends

  1. 执行完测试后,不要执行 allure serve 命令,转而执行 allure generate
    会生成一个新的文件夹,名为 allure-report。
  2. 拷贝 allure-report 文件夹下的 history 文件夹,及其子文件夹到 allure_results 这个目录中
  3. 新的一次测试执行后执行 allure serve,即可把历史记录带到 Allure 报告

4. 添加执行人

Executor 通常是由 Builder 自动生成的,比如通过 Jenkins pluginAllure Jenkins Plugin 来生成
手动配置 创建名称为executor.json 的文件,然后拷贝到allure_results

{
  "name": "iTesting",
  "type": "jenkins",
  "url": "http://helloqa.com",
  "buildOrder": 3,
  "buildName": "allure-report_deploy#1",
  "buildUrl": "http://helloqa.com#1",
  "reportUrl": "http://helloqa.com#1/AllureReport",
  "reportName": "iTesting Allure Report"
}

浏览器打开 allure 报告的两种方式

  • allure serve标准写法
    # 执行 pytest,指定 allure 结果目录
    pytest -sq --alluredir=./allure
    
    # 打开 allure 报告
    allure serve ./allure
    
  • allure generate + allure open标准写法
    # 执行 pytest,指定 allure 结果目录
    pytest -sq --alluredir=./allure
    
    # 生成 allure 的 html 报告
    allure generate -c -o ./allure-report ./allure
    
    # 打开 allure 报告
    allure open ./allure-report # 当然不写 -o 也可以
    

pytest_cov 覆盖率配置

  • 方法一 :case目录下新建.coveragerc
    [run]
    branch = True
    omit =
        # 计算覆盖率时排除某些文件
        */test_*.py
        */*_test.py
     
    [report]
    # 设置报告精度
    precision = 2
     
    # 设置报告排除的行
    exclude_lines =
        # Don't complain about missing debug-only code:
        if __name__ == .__main__.:
     
    [html]
    # 设置html报告的文件夹
    directory = coverage_html_report
    
  • 方法二
    '''输出,测试文件目录,报告样式html xml, 配置筛选文件路径'''
    pytest.main(['-sq', '--cov=./', '--cov-report=html:./reports/coverage', '--cov-report=xml:reports/coverage.xml', '--cov-config=./cov/coveragerc'])
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值