preface
The parameterization of pytest can realize the purpose of generating different test cases only by maintaining test data. You can add ids parameters to the parameterization to explain the usage scenarios for each use case.
Finally, we hope to show the title description of each use case in detail on the allure report, so that we can know more intuitively what each use case does.
Parameterize
First, let's see a simple pytest parametric case demonstration test_a.py
# test_a.py import pytest import allure # Author: Shanghai youyou QQ communication group: 779429633 def login(username, password): '''Sign in''' print("Enter account number:%s" % username) print("Input password:%s" % password) # return return {"code": 0, "msg": "success!"} # test data test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure.story("Login case") @pytest.mark.parametrize("test_input,expected", test_datas ) def test_login(test_input, expected): '''Test login case''' # Get function return result result = login(test_input["username"], test_input["password"]) # Assertion assert result["msg"] == expected
cmd command line run case
> pytest --alluredir ./report test_a.py > allure serve ./report
Generate report
The report generated in this way can't show the execution scenario of each use case in the use case list, only knowing which use case reported an error.
So we need to add a description and an ids parameter to each use case
ids parameter usage
Add an ids parameter in the code of the above use case part to describe the operation scenario of each use case.
# Author: Shanghai youyou QQ communication group: 779429633 @allure.story("Login case") @pytest.mark.parametrize("test_input,expected", test_datas, ids=[ "Enter the correct account and password, and the login is successful", "Input wrong account, password, login failed", "Enter the correct account and password, and the login is successful", ] ) def test_login(test_input, expected): '''Test login case''' # Get function return result result = login(test_input["username"], test_input["password"]) # Assertion assert result["msg"] == expected
cmd command line run case
> pytest --alluredir ./report test_a.py > allure serve ./report
Generate report
allure.title Describe use cases
The above problem is solved by adding ids parameters to parameterize. Next, we will use allure.title("use case description") to add a use case description.
Use@ allure.title (use case description), you can add the passed in parameter, such as "test"_ Input, expected ", test to be spliced_ The value of the input parameter, which can be written as follows
@allure.title("Use case description,Test input:{test_input}")
In allure_pytest/utils.py The corresponding code can be found in the source code
# allure_pytest/utils.py def allure_name(item, parameters): name = escape_name(item.name) title = allure_title(item) return title.format(**parameters) if title else name
When not added allure.title() the description of use case is item.name Value (that is, the name of the ids case above),
If added allure.title(), then the description of the use case is the added title value, which is one of the two places.
import pytest import allure # Author: Shanghai youyou QQ communication group: 779429633 def login(username, password): '''Sign in''' print("Enter account number:%s" % username) print("Input password:%s" % password) # return return {"code": 0, "msg": "success!"} # test data test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure.story("Login case") @allure.title("Use case description,Test input:{test_input}") @pytest.mark.parametrize("test_input,expected", test_datas, ids=[ "Enter the correct account and password, and the login is successful", "Input wrong account, password, login failed", "Enter the correct account and password, and the login is successful", ] ) def test_login(test_input, expected): '''Test login case''' # Get function return result result = login(test_input["username"], test_input["password"]) # Assertion assert result["msg"] == expected
cmd command line run case
> pytest --alluredir ./report test_a.py > allure serve ./report
Generate report
Optimize use case title
Combined with the above two implementation methods, the use case description is regarded as a test input parameter. After further optimization, it is as follows
It should be noted that the three parameters test in parameterize_ Input, expected, title and test_ login(test_ The three parameters in input, expected, title) are consistent
import pytest import allure # Author: Shanghai youyou QQ communication group: 779429633 def login(username, password): '''Sign in''' print("Enter account number:%s" % username) print("Input password:%s" % password) # return return {"code": 0, "msg": "success!"} # test data test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!", "Enter the correct account and password, and the login is successful"), ({"username": "yoyo2", "password": "123456"}, "failed!", "Input wrong account, password, login failed"), ({"username": "yoyo3", "password": "123456"}, "success!", "Enter the correct account and password, and the login is successful"), ] @allure.story("Login case") @allure.title("{title}") @pytest.mark.parametrize("test_input,expected,title", test_datas ) def test_login(test_input, expected, title): '''Test login case''' # Get function return result result = login(test_input["username"], test_input["password"]) # Assertion assert result["msg"] == expected