栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

pytest report 配置
  • pytest run配置
  • Allure 配置
    • 1. 添加 Environment
    • 2. 添加 categories.json
    • 3. 显示历次运行的 trends
    • 4. 添加执行人
  • 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'])
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号