Learn these common features of pytest-Allure, allure.attach, allure.step, fixture, environment, categories. Tested somewhere: Big brother teaches little brother

allure.attach

allure.attachUsed to add attachments to the test report to supplement the test results. The attachment format can be txt, jpg, etc., and the attachment content is usually test data, screenshots, etc.

allure.attach provides two methods: allure.attach(), allure.attach.file()

allure.attach()

Function: Generate attachments with specified content, name, and type in the test report

grammar:allure.attach(body, name=None, attachment_type=None, extension=None)

Parameter Description:

  1. body, the content that needs to be displayed can also be understood as the content written in the attachment.
  2. name,accessory name
  3. attachment_type, attachment types, such as csv, jpg, html, etc., provided byallure.attachment_type
  4. extension: Attachment extension, not commonly used

allure.attach.file()

Function: Upload attachments to test cases

grammar:allure.attach.file(source, name=None, attachment_type=None, extension=None)

Parameter description:source is the file path, and other parameters are consistent with the allure.attach() parameter.

In UI automation testing, this method is often used to upload screenshots of use case execution.

Example

test_login.py

import allure
import pytest
import requests
import json

data = [("lilei", "123456"), ("hanmeimei", "888888"), ("xiaoming", "111111")]
ids = ["username:{}-password:{}".format(username, password) for username, password in data]

@allure.epic("xx在线购物平台接口测试")
@allure.feature("登录模块")
class TestLogin:

    @allure.story("用户登录")
    @allure.title("登录")
    @pytest.mark.parametrize("username, password", data, ids=ids)
    def test_login(self, username, password):
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/login"
        _data = {
            "username": username,
            "password": password
        }
        allure.attach(
            body="用户名-{},密码-{}".format(username, password),
            name="登录参数",
            attachment_type=allure.attachment_type.TEXT
        )
        res = requests.post(url=url, headers=headers, json=_data).text
        res = json.loads(res)
        assert res['code'] == 1000
        
    @allure.story("用户退出登录")
    @allure.title("退出登录")
    def test_logout(self):
        '''这条测试用例仅仅只是为了举例说明allure.attach.file的使用'''
        print("退出登录,并截图")
        # 截图路径
        testcase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        source_path = testcase_path + "/screenshot/logout.jpg"
        allure.attach.file(
            source=source_path,
            name="退出登录后截图",
            attachment_type=allure.attachment_type.JPG
        )
        assert True

The above code uses @pytest.mark.parametrize(), and Allure can support @pytest.mark.parametrize() parameterization very well.

run.py

if __name__ == '__main__':
    pytest.main(['testcase/test_login.py', '-s', '-q', '--alluredir', './result'])
    os.system('allure generate ./result -o ./report --clean')

Runrun.py, the test report results are shown as follows:

allure.attach()result:

allure.attach.file()result:

It can be seen from the results that both methods display the attachment content in the corresponding test cases in the report.

It can also be seen from theallure.attach() results that Allure can support @pytest.mark.parametrize() parameterization a>.

with allure.step

In the previous article, we used the decorator@allure.step() to mark the function to become a test step. In the test function/method, we can also passwith allure.step() mark the test steps.

The difference between them is that @allure.step() is used to mark general functions. When the marked function is called, step instructions will be inserted and displayed in AllureReporting.

Andwith allure.step() marks ordinary code as a test step. When this code is executed, the step instructions will be displayed in theAllure report.

Based on the above code, we addwith allure.step(). The example is as follows:

import allure
import pytest
import requests
import json


data = [("lilei", "123456"), ("hanmeimei", "888888"), ("xiaoming", "111111")]
ids = ["username:{}-password:{}".format(username, password) for username, password in data]

@allure.epic("xx在线购物平台接口测试")
@allure.feature("登录模块")
class TestLogin:

    @allure.story("用户登录")
    @allure.title("登录")
    @pytest.mark.parametrize("username, password", data, ids=ids)
    def test_login(self, username, password):
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/login"
        _data = {
            "username": username,
            "password": password
        }
        # 第一步,请求登录接口
        with allure.step("请求登录接口"):
            allure.attach(
                body="用户名-{},密码-{}".format(username, password),
                name="登录参数",
                attachment_type=allure.attachment_type.TEXT
            )
            res = requests.post(url=url, headers=headers, json=_data).text
            res = json.loads(res)
        # 第二步,获取返回参数进行断言
        with allure.step("断言"):
            assert res['code'] == 1000

 Recommended tutorials related to automated testing:

The latest automated testing self-study tutorial in 2023 is the most detailed tutorial for newbies to get started in 26 days. Currently, more than 300 people have joined major companies by studying this tutorial! ! _bilibili_bilibili

2023 latest collection of Python automated test development framework [full stack/practical/tutorial] collection essence, annual salary after learning 40W+_bilibili_bilibili

Recommended tutorials related to test development

The best in the entire network in 2023, the Byte test and development boss will give you on-site teaching and teach you to become a test and development engineer with an annual salary of one million from scratch_bilibili_bilibili

postman/jmeter/fiddler test tool tutorial recommendation

The most detailed collection of practical tutorials on JMeter interface testing/interface automated testing projects. A set of tutorials for learning jmeter interface testing is enough! ! _bilibili_bilibili

To teach yourself how to capture packets with fiddler in 2023, please be sure to watch the most detailed video tutorial on the Internet [How to Learn to Capture Packets with Fiddler in 1 Day]! ! _bilibili_bilibili

In 2023, the whole network will be honored. The most detailed practical teaching of Postman interface testing at Station B can be learned by novices_bilibili_bilibili

The test report results are shown below:

The test steps inserted in the code are marked as marked in the image above.

fixture

The fixture function of pytest can realize the setup and teardown functions, and Allure will track the calling status of each fixture, showing in detail which fixtures and parameters are called and the calling order.

We add thefixture function based on the above code. The example is as follows:

import allure
import pytest
import requests
import json

@pytest.fixture(scope="class", autouse=True)
def fixture_demo(request):
    print("连接数据库")
    def finalizer():
        print("关闭数据库")
    request.addfinalizer(finalizer)


data = [("lilei", "123456"), ("hanmeimei", "888888"), ("xiaoming", "111111")]
ids = ["username:{}-password:{}".format(username, password) for username, password in data]

@allure.epic("xx在线购物平台接口测试")
@allure.feature("登录模块")
class TestLogin:

    @allure.story("用户登录")
    @allure.title("登录")
    @pytest.mark.parametrize("username, password", data, ids=ids)
    def test_login(self, username, password):
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/login"
        _data = {
            "username": username,
            "password": password
        }
        # 第一步,请求登录接口
        with allure.step("请求登录接口"):
            allure.attach(
                body="用户名-{},密码-{}".format(username, password),
                name="登录参数",
                attachment_type=allure.attachment_type.TEXT
            )
            res = requests.post(url=url, headers=headers, json=_data).text
            res = json.loads(res)
        # 第二步,获取返回参数进行断言
        with allure.step("断言"):
            assert res['code'] == 1000

The test report results are shown below:

As can be seen from the results, the Allure test report shows the fixture function information called by the use case. The display of multiple fixture functions being called and their execution order will not be explained here.

environment

On the home page of the Allure report, the environment information of the test execution (such as test environment, tester, version number of the system under test, system configuration environment, etc.) can be displayed. This needs to be created throughenvironment.properties or environment.xml configure and place the file in the folder specified by --alluredir (the blogger here is the result folder ).

environment.propertiesExamples are as follows:

system=win
python=3.7.7
version=1.0.1
host=127.0.0.1

orenvironment.xml

<environment>
    <parameter>
        <key>system</key>
        <value>win</value>
    </parameter>
    <parameter>
        <key>python</key>
        <value>3.7.7</value>
    </parameter>
    <parameter>
        <key>version</key>
        <value>1.0.1</value>
    </parameter>
    <parameter>
        <key>host</key>
        <value>127.0.0.1</value>
    </parameter>
</environment>

After generating the report, the homepage will be displayedENVIRONMENTThe information is as follows:

categories

There is a column in the Allure report calledCategories, which is classification, used to display test results. By default, only two types of defect results are displayed:

  1. Product defects Product defects (test result: failed)
  2. Test defects Test defects (test result: error/broken)

As shown below

If we want the defect classification display to be richer, we can customize the defect classification by creating categories.json files and place the files in the --alluredir specified folder (that is, placed in the same directory asenvironment.properties).

categories.jsonExamples are as follows:

[
  {
    "name": "Passed tests",
    "matchedStatuses": ["passed"]
  },
  {
    "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"]
  }
]

Parameter Description:

  1. name: Category name
  2. matchedStatuses: The running status of the test case, default ["failed", "broken", "passed", "skipped", "unknown"]< /span>
  3. messageRegex: The error message of the test case running, the default is .*, matched by regular expression
  4. traceRegex: Error stack information when the test case is run, the default is .*, it is also matched through regular expressions

will be displayed in the reportCategories after execution. The homepageCATERORIES column will be displayed as follows:

Categories page display:

Summarize

These are the commonly used features in Allure. The above are just simple examples. You can choose to use them according to the needs of your own projects. Of course, Allure provides more features than these. If you are interested, you can check out Allure official documentation to learn more.

  Summarize:

 Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.

If it is helpful to you, please like and save it to give the author an encouragement. It also makes it easier for you to search quickly next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and improve with like-minded testers.

At the appropriate age, choose the appropriate position and try to give full play to your own advantages.

My path to automated test development is inseparable from plans at each stage, because I like planning and summarizing.

Test development video tutorials and study notes collection portal! !

Guess you like

Origin blog.csdn.net/m0_70618214/article/details/134599149