I recently downloaded an open source interface automation testing framework (YamL + PyTest + Request + Allure) and wanted to understand and use it for myself. My computer is a MAC and I run PyCharm, but I had some problems running it. Here are the specific configuration steps for PyTest + Allure. Later will be sorted out according to the framework requirements module one by one, and finally speak clearly the whole framework. Framework reference documentation: https://www.imooc.com/article/details/id/71219Copy the code
The installation
1. Install PyTest
pip install pytest
Copy the code
Pycharm- “Preferences-” Select Project: Your Project name – “Project Interpreter-” click +- “PyTest click Install
2. Install allure- PyTest
Allure – PyTest is a third party library for Python. Used to connect PyTest and Allure so they can be used together.
Allure – PyTest Generates jSON-formatted results for Allure based on the original execution results of PyTest. This JSON-formatted result can be used later to generate HTML results by applying Allure.
pip install allure-pytest
Copy the code
Note: Some documents are installed pytest-allure-adaptor, which will cause an error! However, this third-party library is outdated and cannot be used with existing PyTest.
3. allure
Premise: Allure relies on JDK environment 1.8+==
3.1 download the allure
Download the latest version of Allure. For example, I downloaded allure-2.13.8.zip. Unzip and save to the desired directory.
3.2 Configuring Environment Variables
- The terminal execution
Vim ~ /. FollowingCopy the code
- Click Insert to insert the following environment variables, then click ESC, enter ‘:wq’ to save and exit
The export PATH = / usr/bin: / bin: / usr/sbin, / sbin, / usr/local/bin (trip without complains: sh allure: Command not found) export PATH=${PATH}:/Users/ XXX /allure-2.13.8/binCopy the code
- Environment variables that take effect configuration (important!)
The source ~ /. FollowingCopy the code
Enter allure –version to check the allure version and the configuration is successful.
- Then I encountered a problem that the configured variables did not take effect after the new window was opened. I had to manually enter “source ~ /.bash_profile” again to take effect. ZSH loads the ~/.zshrc file, and the ‘.zshrc ‘file does not define the task environment variables. Solution: Add source ~/.bash_profile at the end of the ~/.zshrc file and save it.
Run PyTest and Allure to produce reports
Given that all TestCase functions are in the TestCase folder, the run.py production report function looks like this:
if __name__ == '__main__': -s: print the result of the print statement. -q: simplify the output information. Xml_report_path: The directory where the report is saved, -- allureDir args = ['-s', '-q', 'TestCase', '--alluredir', Xml_report_path] Pytest. main(args) # xml_report_path: Report the directory where the JSON file is saved Generate test report save directory --clean: Generate a new OS. system('allure generate %s - O %s --clean' % (xml_report_path, html_report_path))Copy the code
- The pytest –alluredir report command generates a JSON file based on the test cases captured by PyTest.
Use Allure generate to generate aesthetic reports
Based on the generated result file, use the allure generate command to generate the final aesthetically pleasing test report. But because Allure doesn’t provide the same main method as PyTest, you need CMD. Python has two methods for calling shell commands.
- Method 1: Use os.sytem(CMD).
- Method 2: write CMD command first, then call the standard library to execute CMD.
Instructions for use of Allure
class TestBasic:
@allure.feature('Home')
@allure.severity('blocker')
@allure.story('Basic')
def test_basic_01(self, action):
Copy the code
# Custom reports
- Feature: Mark major public modules
- Story: Mark branch functions under the Feature module
- Severity: Indicates the importance level of the test case
- Step: Important steps for annotating test cases
- Issue: Indicates the Issue case
# level
- Blocker level: Interrupt defect (the client program is not responding and cannot perform the next action)
- Critical level: Critical defects (missing function points)
- Normal level: Common defect (numerical calculation error)
- Minor: Minor defects (interface errors that do not match UI requirements)
- Trivial level: Minor defect (mandatory entry without prompt or non-canonical prompt)
# Add attachments
file = open('.. /test.png', 'rb').read() allure. Attach ('test_img', file, allure. Attach_type. allure.attach('arg1', 'arg2', 'arg3')Copy the code
- Arg1: is the attachment name that is displayed in the report
- Arg2: indicates the contents of the attachment
- Arg3: add type (support: HTML, JPG, PNG, JSON, OTHER, TEXTXML)
Pycharm using OS shell command error resolved
But when I used the OS to call shell commands, I got sh: Allure: Command not found. This is because my PyCharm was not opened by Terminal, but by a shortcut or other method, which did not read the environment variables in BashrC and of course did not find commands that were not in the underlying environment of the system. This can be verified by using the following method: terminal
echo $PATH
Copy the code
Run in the script written by PyCharm:
import os
os.system('echo $PATH')
Copy the code
You can see that the results obtained are different. The solution is to start PyCharm from the terminal.
To see how to quickly open Pycharm -, click On Pycharm – Tool- to Create Command Line Launcher. The default values are /usr/local/bin/charm. Pycharm = /usr/local/bin/charm