Pytest+selenium+allure+Jenkins automated test framework construction and use

1. Environment setup
1. Python download and installation
Python can be applied to multiple platforms including windows, Linux and Mac OS X. This article mainly introduces the windows environment. You can enter the "python" command through the terminal window to check whether Python is installed locally and the installed version of Python.
 

 

If python is not installed, it is recommended to download python 3.8.3 or above. This article mainly introduces the installation under windows. Open the WEB browser to visit https://www.python.org/downloads/windows/, and select the corresponding Windows system platform in the download list Install the package, such as the file name python-3.9.5-amd64.exe (corresponding to 64-bit operating system), run the installation directly.

 Run the .exe executable file after downloading, use the default settings, "Next" until it is completed, add the installation path such as: C:\Python38 and C:\Python38\Scripts to the environment variable Path, save it in the cmd command Perform python verification under the line.
Environment variable editing results:

 

 2. JDK installation (the following Tomcat, allure, etc. need to rely on JDK) go to
the official website (https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html) to download the JDK installation package, run The downloaded JDK installation file, such as jdk-8u181-windows-x64.exe, only needs to change the installation location (or not) and remember this installation location.
2.1 Create a new JAVA_HOME variable in the environment variable in the previous step, and the variable value is the JDK installation directory (mine is: c:\Program Files\Java\jdk1.8.0_181).
2.2 Create a new CLASS_PATH variable, the variable value is .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar; There is an English point and semicolon in front of it. Don’t miss it.
2.3 New value %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin in the Path variable; Note that if multiple addresses are placed in one line in the path variable such as windows7 system, English semicolons should be added between and before The variable values ​​are spaced apart.
After the setting is completed, the result is shown in the figure below:

 After saving the configuration, you can perform java -version verification under cmd:

 

3. Editor installation
You can search for the installation and configuration of PyCharm or Visual Studio Code by yourself, this article does not explain in detail
PyCharm download address: https://www.jetbrains.com/zh-cn/pycharm/
Visual Studio Code download address: https: //code.visualstudio.com/

4. Installation of related python packages and plug-ins
The python packages needed for this framework include: pytest, selenium, allure-pytest
4.1 Install pytest
under cmd or under the terminal command line of PyCharm or Visual Studio Code, use the pip command tool to install: 
> pip install -U pytest (-U parameter means upgrade to the latest version if installed)

 When the Pytest command does not specify a directory or file, it will search for test modules in the current directory and all subdirectories, including: files named test_*.py or *_test.py, and search for functions starting with test_ in the test module , to find classes whose names begin with Test. Among them, first filter out the class containing the __init__() function, and then search for the class method starting with test_ in the class, that is, the rules are as follows:
==The test file starts with test_ (it can also end with _test)
==The test class starts with Start with Test, and cannot have an init method
== the test function starts with test_
== assert that you can use the basic assert as
shown in the figure

The Pytest framework provides test cases that can be executed according to priority 

 

4.2 Install selenium
command line input
>pip install selenium    

  
To learn about selenium, please visit: https://blog.csdn.net/qq_44326412/article/details/107825851?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162129756416780357211210%252 2%252C%2522scm%2522%253A%252220140713.130102334. .%2522%257D&request_id=162129756416780357211210&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-107825851.first_rank_v2_pc_rank _v29&utm_term=selenium&spm=1018.2226.3001.4187 4.3 Install Allure 4.3.1 Allure Download the latest
 version
: https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/ After selecting a version to enter, download the zip package

After downloading, extract it to a certain directory, and add the directory where allure is located in the environment variable path, such as adding D:\allure\allure-2.13.9\bin to my environment variable path. 4.3.2 Integration with pytest
Install
pytest- allure plug-in library, >pip install allure-pytest
4.3.3 Jenkins to install the allure plug-in, the following will introduce
4.3.4 allure generation report
description when installing Jenkins: the data generated by pytest execution test cases is saved in the data type of json format, passed Allure generate can be parsed to formally generate html-type test reports
for use with pytest: >pytest --alluredir=D:/pytest/result (specify the directory where the allure report produces results when executing pytest, or use a relative path to specify, such as: pytest --alluredir=../result , which indicates the result directory under the upper directory of the specified current directory)
To view the actual report after the test is completed, you need to use the Allure command line utility to generate a report from the result:
>allure generate ../ result -o ../report --clean (organize the test report from the allure result ../result generated by the previous command to ../report, the --clean parameter indicates that the previous results are cleared) through the allure
annotation Reports can be hierarchically divided in this way, and use cases can be managed uniformly and displayed hierarchically in this way.
Sample code:
 

 The execution results are as follows:

 

5. Tomcat and Jenkins installation
   Note that Jenkins can also run directly under windows, but it is recommended to use the Tomcat container to run Jenkins
5.1 Download Tomcat from the official website, and download the 64-bit zip file of the windows version as needed; the download address is: https://tomcat .apache.org/whichversion.html

5.2 Decompress the downloaded Tomcat to a certain disk, mine is decompressed to D:\Tomcat; 

5.3 Configure the Tomcat environment variable, create a new TOMCAT_HOME, the value is D:\Tomcat\apache-tomcat; create a new CATALINA_HOME, the value is D:\Tomcat\apache-tomcat-9.0.36; set ;%TOMCAT_HOME%\bin;%CATALINA_HOME %\lib into the path.

5.4 Go to D:\Tomcat\apache-tomcat-9.0.36\bin and click the startup.bat file
  

5.5 Then enter localhost:8080 in the browser and a cat page will appear (port 8080 by default). If the port conflict needs to be modified, go to the D:\Tomcat\apache-tomcat-9.0.36\conf directory to modify the server In the .xml file, <Connector port="8088" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> Change 8080 to 8088 or other port numbers you want. The effect picture is as 
follows

5.6 Install Jenkins

5.6.1 Download the Jenkins application package jenkins.war (download address https://www.jenkins.io/zh/download/)

5.6.2 Create a new environment variable, JENKINS_HOME, the directory of jenkins to be installed, I created D:\Jenkins,
or edit the context.xml file under D:\Tomcat\apache-tomcat-9.0.36\conf Add <Context ...> <Environment name="JENKINS_HOME" value="D:/jenkins/" type="java.lang.String"/>
</Context>
otherwise it will be installed by default in .jenkins under the c drive user Under the folder, if the previous files are installed incorrectly, all files must be deleted, otherwise it will affect the subsequent installation. 5.6.3
Put the jenkins.war package under the webapps of tomcat
 
5.6.4 Enter D:\Tomcat\apache-tomcat-bin Run the startup.bat file, and jenkins will be installed at this time. After installation, the jenkins file will appear in wabapp. The key point is that the ports of jenkins and tomcat should not be the same. Tomcat sets port 8088, jenkins port 8080, open http://localcat:8086/jenkins,
the effect is as shown in the figure below, when accessing jenkins, there is an initialization password, which needs to be entered, find the initialization password, and follow the prompts to enter the corresponding directory D:\Program Files \Jenkins\secrets, open initialAdminPassword and copy the password to the input field (initial password under Linux system is in /root/.jenkins/secrets/initialAdminPassword)

5.6.5 Install the allure plugin of Jenkins
Log in to Jenkins with the admin account, under the Manage Jenkins menu on the left side of the homepage, enter the Manage Plugins submenu, select "Optional Plugins" in the plugin management page, search for allure, and you will find the Allure Jenkins Plugin, tick After selecting, click Install without restart or Download now and install after restart at the bottom of the page to install.
5.6.6 Jenkins global configuration
Since jenkins needs jdk environment to run, the global configuration needs to introduce environment variables

The execution of the host execution machine needs to rely on the local allure startup component, and the global configuration needs to introduce the allure environment variable 

Note: For the configuration of the project in Jenkins, please refer to the documentation  
After installation, in the "build" step of the project configuration, when the pytest command is executed, the test results are generated and the storage directory is specified 

Then you can see in the configuration menu of the project that there is an Allure Report item under the post-build operation, as shown in the figure below. In Results: Path, fill in the directory result generated by the test results (that is, the result directory specified in the previous step), and in Report path: Specify the directory where the test report is stored: 

 

2. pytest+selenium framework
1. The directory structure of the framework 

 

Note: the common directory stores public methods, such as driver, the method of reading files, etc., and the __init__.py file is required in the directory; the
config directory stores configuration files such as logs, and the __init__.py file is required in the directory;
the driver directory stores browsing Driver driver, currently using Google Drive chromedriver.exe; 
the log directory stores log files;
the PageObject directory stores page classes, methods, elements, etc., and the __init__.py file is required in the directory; the
report directory stores the test reports generated by allure;
the result directory Store the test result files generated by allure;
the testcase directory is used to store test cases and conftest.py (pytest glue file, if necessary), and the __init__.py file is required in the directory. If you run the test case on the windows command line, you can cd to this directory Execute the pytest command after downloading;
other files __init__.py are empty files, necessary files of the python package, without this file, they cannot be imported in the py file;
README.md is the relevant text description of the project;
requirements.txt file lists The information of the dependent package is provided. In order to facilitate the installation of the project, it can be executed directly on the command line: pip install -r requirements.txt, and the required package and its required version can be installed.

2. The composition of the framework
Simple understanding of the POM model:
Since the following will talk about element-related, so first understand the POM model
Page Object mode has the following advantages.
a. Abstracting the object can minimize the impact of developers modifying the page code on the test, so you only need to adjust the page object without affecting the test; b. A part of the test can be reused in multiple test
cases Code;
c. Test code becomes more readable, flexible, and maintainable
Page Object pattern diagram

 

• basepage——the base class of selenium, which encapsulates the methods of selenium
• pageelements——page elements, extract the page elements separately and put them into a file
• searchpage——page object class, integrate selenium methods and page elements
• testcase ——use pytest to write test cases for the integrated searchpage
From the above figure, we can see that through the POM model idea, we have disassembled:
selenium method
• page element
• page object
• test case The above four code bodies
Points, although it will increase the code when there are few use cases, but it is very meaningful when there are many use cases, and the amount of code will be significantly reduced when the use cases increase. Our maintenance code has become more intuitive and obvious, the readability of the code has become much stronger than that of the factory model, and the code reuse rate has also been greatly improved.

Supongo que te gusta

Origin blog.csdn.net/qq_48811377/article/details/131330627
Recomendado
Clasificación