美文网首页
48. Allure报告

48. Allure报告

作者: 薛东弗斯 | 来源:发表于2024-03-23 06:36 被阅读0次

    知乎-最新最全 Allure快速入门 自动化测试报告 - 知乎 (zhihu.com)

    python3.7
    jdk-11.0.12_windows-x64_bin
    allure-2.18.1  
           -<解压到C:\Program Files>
           - 添加环境变量: 将“C:\Program Files\allure-2.18.1\bin” 添加到path
           - windows的cmd目录下通过 allure --version  判断是否生效
    pip install allure-pytest
    
    # pytest.ini
    [pytest]
    testpaths = ./
    # ./ 代表执行命令的当前文件夹
    markers=
        pro:pro
        test:test
        p1:p1
    
    addopts: -vs --alluredir ./report --clean-alluredir
    # --alluredir :报告生成地址
    # --clean-alluredir: 清除之前的记录
    
    bejson.com
    

    生成报告

    执行测试用例后,自动生成report目录。
    当前执行路径,指定report 目录,allure serve report就会自动生成allure测试报告,报告放到临时目录
    
    # 放到固定木
    allure generate report     # 生成allure-report
    allure generate report    -o ABC # 生成重新命名的allure-report,名称叫ABC
    allure open allure-report  # 打开allure-report
    
    # test_one.py
    import allure
    import pytest
    @allure.epic("TestOne_Automation")
    @allure.feature("test_one,feature")
    class TestOne:
        @allure.story("test_one,story")
        @allure.title("test_one,title")
        @allure.testcase("www.baidu.com","testcase_link")
        @allure.issue("www.baidu.com","bug_tracker")
        @allure.description("This is a test_one description")
        @allure.severity("blocker")
        @allure.link("www.baidu.com",name="link")
        def test_one(self):
            with allure.step("step1"):
                pass
            with allure.step("step2"):
                pass
            assert 1 == 1
    
        @allure.story("test_two,story")
        @allure.title("test_two,title")
        def test_two(self):
            assert 1 == 1
    
        def test_three(self):
            assert 1 == 2
    
        @pytest.mark.skip
        def test_four(self):
            assert 1 == 1
    
    pytest -k test_one
    allure serve report   # 自动打开报告
    

    钩子函数完成失败用例截图

    project > conftest.py
    project > pytest.ini
    project > test_allure.py
    project > data > data.yaml
    project > utils > read.py
    project > utils > get_filepath.py
    
    # conftest.py
    import allure
    import pytest
    from selenium import webdriver
    
    from utils.get_filepath import get_screen_shot_path
    
    
    @pytest.fixture(scope="session")
    def driver():
        global driver
        driver = webdriver.Chrome()
        driver.maximize_window()
        print("打开浏览器")
        yield driver
        print("关闭浏览器")
        driver.close()
        driver.quit()
    
    
    # 钩子函数,结果
    @pytest.hookimpl(hookwrapper=True, tryfirst=True)
    def pytest_runtest_makereport(item, call):
        """
    
        :param item: 代表测试函数或方法,包含测试相关的信心,比如名称,位置,标记
        :param call: 包含测试函数执行的详细信息,比如结果,执行时间
        :return:
        when = setup:前置
        when = call:执行测试用例
        when = teardown:后置
        """
        print("=========================")
        # 获取钩子函数的结果
        out = yield
        # 获取测试报告
        report = out.get_result()
    
        # print(f"测试报告:{report}")
        # print(f"步骤:{report.when}")
        # print(f"nodeid:{report.nodeid}")
        # print(f"运行结果:{report.outcome}")
        # 失败测试用例截图
        if report.when == 'call' and report.failed:
            # 保存到本地
            driver.save_screenshot(get_screen_shot_path())
            # 截图,get_screenshot_as_png二进制数据
            # 使用allure.attach将二进制数据附加到allure报告中
            allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
    
    
    # pytest.ini
    [pytest]
    testpaths = ./testcases
    markers=
        pro:pro
        test:test
        p1:p1
    
    addopts: -vs --alluredir ./report --clean-alluredir
    
    # test_allure.py
    from time import sleep
    
    import allure
    import pytest
    from selenium.webdriver.common.by import By
    
    from utils.read import read_yaml
    
    
    class TestLogin:
    
        @pytest.mark.parametrize("username,password", read_yaml()['userinfo_list'])
        def test_login(self, driver, username, password):
            allure.dynamic.title("登录功能,账号为:" + username)
            driver.get("http://sellshop.5istudy.online/sell/user/login_page")
            driver.find_element(By.ID, 'username').send_keys(username)
            driver.find_element(By.ID, 'password').send_keys(password)
            driver.find_element(By.CSS_SELECTOR, '#login > form > p.login.button > input[type=submit]').click()
            if username == 'admin':
                text = driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div > strong").text
                assert text == "用户不存在1"
            sleep(2)
    
    # project > utils > read.py
    import configparser
    
    import yaml
    
    from utils.get_filepath import get_yaml_path, get_ini_path
    
    path = get_yaml_path()
    ini_path = get_ini_path()
    
    
    def read_yaml():
        with open(path, encoding="utf8") as f:
            data = yaml.safe_load(f)
            return data
    
    
    def read_ini():
        config = configparser.ConfigParser()
        config.read(ini_path, encoding='utf8')
        return config
    
    
    if __name__ == '__main__':
        print(read_yaml())
        # print(read_ini()['mysql']['HOST'])
    
    # project > utils > get_filepath.py
    import os
    import time
    
    
    def get_report_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "allure-report/export",
                            'prometheusData.txt')
        return path
    
    
    def get_screen_shot_path():
        file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", file_name)
        return path
    
    
    def get_logo_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", "logo.jpg")
        return path
    
    
    def download_file_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file")
        return path
    
    
    def get_yaml_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")
        return path
    
    
    def get_ini_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
        return path
    
    
    def get_log_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log")
        return path
    
    
    if __name__ == '__main__':
        print(get_report_path())
    
    
    # project > data > data.yaml
    userinfo:
      username: admin
      password: 123456
    
    skill:
      - 接口自动化
      - UI自动化
      - 性能测试
    
    userinfos:
      - username: admin
        password: 123456
      - username: test01
        password: 123456
    
    userinfo_list:
      - [ admin,123456 ]
      - [ test01,123456 ]
    
    skills:
      - - 接口自动化
        - UI自动化
        - 性能测试
    
    skills2:
      - [ 接口自动化, UI自动化, 性能测试 ]
    
    
    #项目
    register_ok:
      mobile: 13800001112
      password: 123456
    
    register_failed:
      mobile: 13800001112
      password: 123456
    
    register_exist:
      mobile: 13017744356
    
    user_login:
      - mobile: ''
        password: 123456
      - mobile: 13800001111
        password: ''
      - mobile: 13800001111
        password: 123451
      - mobile: 13800001111
        password: 123456
    
    user_address:
      province: 天津市
      city: 天津城区
      district: 和平区
      username: 老白
      useraddress: 人民广场100
      mobile: 13800001111
    

    allure动态报告内容

    # test_allure.py
    from time import sleep
    import allure
    import pytest
    from selenium.webdriver.common.by import By
    from utils.read import read_yaml
    
    class TestLogin:
        @pytest.mark.parametrize("username,password", read_yaml()['userinfo_list'])
        def test_login(self, driver, username, password):
            allure.dynamic.title("登录功能,账号为:" + username)
            driver.get("http://sellshop.5istudy.online/sell/user/login_page")
            driver.find_element(By.ID, 'username').send_keys(username)
            driver.find_element(By.ID, 'password').send_keys(password)
            driver.find_element(By.CSS_SELECTOR, '#login > form > p.login.button > input[type=submit]').click()
            if username == 'admin':
                text = driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div > strong").text
                assert text == "用户不存在1"
            sleep(2)
    

    环境信息

    pytest文档45-allure添加环境配置(environment)-腾讯云开发者社区-腾讯云 (tencent.com)

    相关文章

      网友评论

          本文标题:48. Allure报告

          本文链接:https://www.haomeiwen.com/subject/nwfozdtx.html