News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Python生成allure测试报告,allure使用详细说明

posted on 2023-05-07 20:53     read(533)     comment(0)     like(25)     collect(0)


The pytest framework comes with a test report, and the content is relatively comprehensive, but the readability is poor. The test report generated by allure is highly modifiable and looks beautiful. The usage process is summarized here.

1. Generate allure test report

1. Download and install the allure-pytest plug-in. I usually install it directly in pycharm: File--Setting--Project--Python Interpreter--"+" on the right side--input "allure-pytest"--select-- Click "Install Package" in the lower left corner. If there is a problem with the environment configuration, you can download it from Baidu.

2. It should be necessary to create a report folder in the root directory of the project. This is not sure. You can try it. Can you generate a report without manually reporting the folder? If not, if this directory is missing, an error will be reported and it will not be found. to the directory.

3. There are several ways to generate an allure test report , cmd executes command generation, pycharm’s Terminal executes commands, these two Baidu commands, and execute the commands. But at the end of our automation, we can’t manually execute a command to generate a report. It’s so inconvenient, so write the command to the main.py file and run the code of the test case to automatically generate the report.

pytest.main(["./test_script",
             "-sv","--alluredir","./report/temp_jsonreport"])
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")

Because the report generated by allure is in json format and needs to be converted into html format, a temp_jsonreport file will be automatically generated, just follow the above, and then execute the main.py file.

specific:

1. "./test_script" is the path of your test case. For example, all my test cases are in this folder, and there are many folders for each use case under this file. If you want to execute all the use cases Just write to the path "./test_script". If you want to execute a certain use case under this file, write to the path of the specific file.

2. "--alluredir" is the path to create allure report.

3. -o is to execute, --clean is to clear the report generated before.

After the main.py is finished, the main file can be executed. After the execution of the use case is completed, there will be two folders under the report folder: html, temp_jsonreport. Under the html folder, find the index.html file. This is the test report we generated. Right-click and open it with a browser to see it.

 2. Optimization of allure test report

On the allure test report page, you can choose to switch between Chinese and English. I personally prefer to use the information in the [Function/Behaviors] menu, because you can see more detailed content here, and it is easier to standardize our test cases. Allure Most of the transformation of the test report is also in this link.

1. Add function module description, test point description and test steps

Method: import allure first, then add the decorator @allure.feature("generate bills") to the class, add the decorator @allure.story("generate bills in batches") to the method, and add steps with allure to the method. step("1. Enter the [Community Management] menu"):

Use and effect diagram:

(feature is equivalent to a function, a large module, classifying cases into a certain feature, which is displayed in behaviore in the report, which is equivalent to testsuite)

(story corresponds to this function or different scenarios under the module, the branch function belongs to the structure under the feature, and the report is displayed in the features, which is equivalent to the testcase)

 

 

 2. Execute assertion, screenshot of failure, screenshot of success

A case can be asserted in the middle step or at the end, depending on the test needs. One result we want is a screenshot of the assertion failure and put it in the allure test report.

  1. with allure.step("5.执行断言"):
  2. #如果断言失败就截图并保存
  3. try:
  4. assert "添加成功" in self.driver.page_source
  5. except:
  6. self.driver.save_screenshot("./screenshot/houseInfoFail.png")
  7. allure.attach.file("./screenshot/houseInfoFail.png", attachment_type=allure.attachment_type.PNG)
  8. #如果断言失败就截图,这里加一个断言失败,方便报告里记录失败用例,
  9. # 不加的话无论失败与否pytest框架都会判断你的用例执行成功了
  10. assert "添加成功" in self.driver.page_source

Now create a screenshot folder under the project to put the intercepted pictures, and then allure will get the pictures again. houseInfoFail.png is the file name of the picture defined by myself.

If the assertion is successful, also capture a picture and put it in the allure report. The complete code is as follows:

  1. with allure.step("5.执行断言"):
  2. #如果断言失败就截图并保存
  3. try:
  4. assert "添加成功" in self.driver.page_source
  5. except:
  6. self.driver.save_screenshot("./screenshot/houseInfoFail.png")
  7. allure.attach.file("./screenshot/houseInfoFail.png", attachment_type=allure.attachment_type.PNG)
  8. #如果断言失败就截图,这里加一个断言失败,方便报告里记录失败用例,
  9. # 不加的话无论失败与否pytest框架都会判断你的用例执行成功了
  10. assert "添加成功" in self.driver.page_source
  11. #截图
  12. with allure.step("6.保存图片"):
  13. self.driver.save_screenshot("./screenshot/houseInfo.png")
  14. allure.attach.file("./screenshot/houseInfo.png", attachment_type=allure.attachment_type.PNG)

 houseInfo.png This is the picture that was successfully intercepted. Please note that it should be distinguished from the file name of the picture that was intercepted when the execution failed.

Effect:

 There are many other functions, you can achieve the desired effect.



Category of website: technical article > Blog

Author:Believesinkinto

link:http://www.pythonblackhole.com/blog/article/332/31c604e51267ea239b2f/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

25 0
collect article
collected

Comment content: (supports up to 255 characters)