Allure combines python to generate test report tutorial

This article mainly introduces the tutorial of allure combined with python to generate test reports. It has a good reference value and I hope it will be helpful to everyone.

Baidu search example

1. Code structure

data.yml is the data management file, test_baidudemo.py is the test case file, and the file structure is as follows:

Create the data/data.yml file, the code is as follows

- allure
- pytest
- unittest

Create test_baidudemo.py, the code is as follows

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import allure
import pytest
import yaml
from selenium import webdriver
import time
@allure.testcase("https://www.baidu.com")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data1',yaml.safe_load(open("data/data.yml")))
def test_steps_demo(test_data1):
    with allure.step("打开百度网页"):
        driver = webdriver.Chrome()
        driver.get("https://www.baidu.com")
        driver.maximize_window()
    with allure.step(f"输入搜索词:{test_data1}"):
        driver.find_element_by_id("kw").send_keys(test_data1)
        time.sleep(2)
        driver.find_element_by_id("su").click()
        time.sleep(2)
    with allure.step("保存图片"):
        driver.save_screenshot("./result/b.png")
        allure.attach.file("./result/b.png",attachment_type=allure.attachment_type.PNG)
    with allure.step("关闭浏览器"):
        driver.quit()

2. Operation results

Enter the project directory and use the following statement to run

pytest test_baidudemo.py -s -q --alluredir=./result/    #执行测试用例,并生成测试报告数据在当前文件夹result文件下
allure serve ./result/     #启动allure,并使用result下的测试结果数据生成测试报告

The generated report is shown below:

problem solved

An error is always reported when running. The current chromedriver only supports chrome78. In fact, chromedriver83 has been updated. The reason has not been found and the solution has not been found. Finally, the absolute path of chromedriver was added to the code. Change driver = webdriver.Chrome() to driver = webdriver.Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe').

with allure.step(f"Enter the search term: {test_data1}"):, when running this statement on python3.6.8 version, a syntax error is always reported. Modify it to with allure.step("Enter the search term:"+test_data1): , can run and output normally.

Introduction and use of allure

Introduction to allure

Allure is a lightweight and very flexible open source test reporting framework. It supports most testing frameworks, such as TestNG, Pytest, JUint, etc. It's simple to use and easy to integrate.

How to generate test report in allure 

Adding pytest.main ('–alluredir', 'report/result', 'TestDemo01.py']) when running will create a report folder in the current folder and create result under the report folder.

Generate html test report 

Because the generated test report is in json and does not look good, use this command to generate a good-looking HTML test report.

After running, an HTML folder will be generated. Click on index.html and this will be our test report.

Several common features of allure (shown in the test report)

@allure.feature  (used to describe the requirements of the product being tested)

@allure.story  (used to describe the user scenario of the feature, that is, test requirements)

with allure.step()  (used to describe the test steps, which will be output to the report)

allure.attach  (used to enter some additional information into the test report, usually some test data, screenshots, etc.)

pytest assertion settings and combined with allure to generate test reports

testing report

The above is personal experience, I hope it can give everyone a reference.

Finally: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves [guaranteed 100% free]

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/133347361