allure report adds environment information and executor information

        In daily automation, when we run the automated test locally and open the allure report, we find that the environment information and runner information on the homepage are empty, as shown in the following figure:

why? Why is this so?

        The reason is that before we execute the allure generate command, the allure result directory automatically generated by the system lacks these two information files, so we can manually generate these two files and put them in the allure result directory before running the allure generate command. In this way, the html report generated by running the allure generate command will display the information of these two modules. Without further ado, let’s get straight to the code:

def set_report_env_on_results():
    """
    在allure-results报告的目录下生成一个写入了环境信息的文件:environment.properties(注意:不能放置中文,否则会出现乱码)
    @return:
    """
    # 需要写入的环境信息
    allure_env = {
        'OperatingEnvironment': 测试环境,
        'BaseUrl': www.test1,com,
        'PythonVersion': platform.python_version(),
        'Platform': platform.platform(),
        'PytestVersion': pytest.__version__,
    }
    allure_env_file = os.path.join({你的自动生成allure result报告目录}, 'environment.properties')
    with open(allure_env_file, 'w', encoding='utf-8') as f:
        for _k, _v in allure_env.items():
            f.write(f'{_k}={_v}\n')



def set_report_executer_on_results():
    """
    在allure-results报告的目录下生成一个写入了执行人的文件:executor.json
    @return:
    """
    # 需要写入的环境信息
    allure_executor = {
        "name": "张三",
        "type": "jenkins",
        "url": "http://helloqa.com",  # allure报告的地址
        "buildOrder": 3,
        "buildName": "allure-report_deploy#1",
        "buildUrl": "http://helloqa.com#1",
        "reportUrl": "http://helloqa.com#1/AllureReport",
        "reportName": "张三 Allure Report"
    }
    allure_env_file = os.path.join({你的自动生成allure result报告目录}, 'executor.json')
    with open(allure_env_file, 'w', encoding='utf-8') as f:
        f.write(str(json.dumps(allure_executor, ensure_ascii=False, indent=4)))

use:

After running pytest automation and before generating allure report, call these two methods

pytest.main([
        '-vs', 
        'testCase/',  
        '--alluredir', 'allure_results', '--clean-alluredir'
    ])

# 在allure_results目录下创建environment.properties文件
AllureReportBeautiful.set_report_env_on_results()

# 在allure_results目录下创建executor.json文件
AllureReportBeautiful.set_report_executer_on_results()

# 使用allure generate -o 命令将./allure_results目录下的临时报告生成到allure_report目录下变成html报告
os.system(f'allure generate allure_results -o allure_report --clean')

After running, check the report again, and the environment information and executor information can be displayed normally, as shown below:

Guess you like

Origin blog.csdn.net/weixin_65784341/article/details/133361264