编程知识 cdmana.com

Jenkins realizes interface automation and continuous integration (Python + pytest + allure + GIT)

This article was first published in : Walker AI

In use python When doing automated tests , We write the code , Then it needs to be executed to get the test report , Then we can go through Jenkins To further complete the automation work . With the help of Jenkins, We can combine Git/SVN Auto pull code , The script execution is automatically triggered by setting the timing construction , Get the test report , Finally, you can configure to send mail and so on . Let's share today , How to combine Git Auto pull code , Build to automate python Script output test report .

Jenkins It's open source. , Use Java Continuous integration tools written by . Need to install JDK, If already installed JDK You can skip this step .

1. install JDK

Go to the official website to download JDK:https://www.oracle.com/java/technologies/javase-jdk15-downloads.html

Put the downloaded compressed package in Linux On

Then decompress Java Go to the appropriate directory :

Enter the command :tar -zxvf jdk-11.0.8_linux-x64_bin.tar.gz /home/java

If the prompt does not tar command , Input yum install -y tar , Install first tar

Edit environment variables , stay /etc/profile Add environment variables to the file :

Enter the command :vim /etc/profile

Add the following at the end of the file , Then save :

JAVA_HOME=/home/java/jdk-11.0.8
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export JRE_HOME
export PATH
export CLASSPATH

Enter the command source /etc/profile , Refresh the environment variable configuration file to make it take effect immediately

Here we are ,JDK Installation completed . Input java -version Check whether the installation is successful , As shown in the figure below, the installation is successful .

PS: It is suggested to build a Java Hyperlinks for . After creation, by default from /usr/bin/java Path usage Java,yum During installation , This link will be created automatically .

Enter the command ln -s /home/java/jdk-11.0.8/bin/java /usr/bin/java

2. install Jenkins

Official website :https://www.jenkins.io/download/ Choose the appropriate operating system or use yum Way to install , What I'm using here is yum, Because it's more convenient :

yum Of repos By default, there is no Jenkins Of , So we need to Jenkins Repository added to yum repos in .

Click on the official website 【CentOS/Fedora/Red Hat】 System , Then copy and paste the command to Linux In the system .

Then you can see that the system starts to download and install automatically . If prompted whether to download input :y And press enter. . Wait patiently for the installation to complete .

start-up Jenkins, command :service jenkins start , appear ok Express Jenkins Successful launch .

Type in the browser ip:8080 Get into Jenkins The login page , Input password . thus ,Jenkins installation is complete .

3. Job To configure

Create a new free style project

Enter the engineering configuration page , Under source code management , choice Git:

If there is no source management Git, It means that you have not installed Git plug-in unit . Click on 【Manage Jenkins】, choice 【Manage Plugins】, choice 【 Optional plug-ins 】, stay 【 Search box 】 Input in git plugin, Download the plug-in .

The installation is successful, as shown in the figure , And then restart 【Jenkins】:

Build execution shell, Based on your own script pytest command . in other words Jenkins take Git After downloading the warehouse code , How do we execute scripts through commands , This can be filled in according to your own script execution , This is consistent with our manual script execution , Then click save .

Click on 【Build Now[ Build immediately ]】, You can see the build in progress Job

4. Test report

When the use case execution is complete , We want to see how the use cases execute , and pytest Not only support pytest-html plug-in unit , You can also use allure Generate more beautiful test reports .Allure The framework is a flexible lightweight multi language test reporting tool , It's not just about web The way to show the test results of the profile , And it allows everyone involved in the development process to extract the most useful information from the tests they perform on a daily basis . So what we choose here is Allure frame .

from dev/qa From the perspective of ,Allure The report simplifies the statistics of common defects : Failed tests can be divided into bug And interrupted tests , You can also configure logs 、 step 、fixture、 The attachment 、 timing 、 The history of execution and the relationship with TMS and BUG Management system integration , therefore , Through the above configuration , All responsible developers and testers can master the test information as much as possible .

From a manager's point of view ,Allure Provides a clear “ Big picture ”, These include features that have been covered 、 The location of the defect aggregation 、 Perform the appearance of the timeline and many other convenient things .allure The modularity and scalability of ensures that you can always fine tune something , send Allure More suitable for you , So today we will talk about how to make the report more detailed to show the information we need , as well as allure And jenkins Integration of .

4.1 download Allure Plug in and configuration Allure

Click on 【Manage Jenkins】, choice 【Manage Plugins】, choice 【 Optional plug-ins 】, stay 【 Search box 】 Input in Allure, Download the plug-in . Restart after successful download Jenkins.

Go to global tool configuration 【Global Tool Configuration】, Drop down the last item , Will see 【Allure Commandline】 term , Click the configuration below and save it :

Enter the configuration , Click on 【 Post-build operation (Post-build Actions)】, choice Allure Report

Results: Input Path route , there path The path indicates that this folder is generated in the root of the project , Folders are used to save the build html What the report relied on before json,xml,txt Etc .

Report path: Input save html Report path .

Click on 【Build Now[ Build immediately ]】, After execution, you can see the generated Allure The report

4.2 Allure Report display

5. Send E-mail

Jenkins An email notification is provided by default E-mail Notification, But the function has many limitations , inflexible . such as : Unable to define the format of the message 、 Email notification cannot provide detailed email content . Here we use Email Extension Plugin plug-in unit . This plug-in allows you to customize the content of the email sent 、 E-mail format, etc .

5.1 download Email Plug in and configuration Email

Click on 【Manage Jenkins】, choice 【Manage Plugins】, choice 【 Optional plug-ins 】, stay 【 Search box 】 Input in Email Extension Plugin, Download the plug-in . Restart after successful download Jenkins.

Enter system settings 【System Configuration】, find 【Jenkins Location】, Set up Jenkins URL With the sender's email address

find “Extended E-mail Notification”, Set the following :

Detailed parameter Introduction :

Override Global Settings: If you don't choose , The plug-in will use the default E-mail Notification Notification options . conversely , You can override it by specifying a different setting . 
Default Content Type: Specify the type of mail content to send after the build , Yes Text and HTML Two kinds of . 
Use List-ID Email Header: Set up one for all messages List-ID The letterhead of . 
Add 'Precedence: bulk' Email Header: set priority . 
Default Recipients: Customize the default email recipient list . 
Reply To List: Reply list . 
Emergency reroute: If this field is not empty , All emails will be sent separately to this address ( Or address list ). 
Excluded Committers: Prevent email from being considered spam by the email system , The mailing list should not have an extended account name ( Such as :@domain.com), And use commas to separate . 
Default Subject: Customize the default subject name for email notifications . This option can replace some parameters in the subject field of the mail , In this way, you can include the specified output information in the build . 
Maximum Attachment Size: Maximum attachment size of mail . 
Default Content: Customize the default content body for email notifications . This option can replace some parameters in the content of the email , In this way, you can include the specified output information in the build . 
Default Pre-send Script: Default script executed before sending . 
Enable Debug Mode: Enable debug mode for plug-ins . 
nable Security: When enabled , Will disable the ability to send scripts , Go straight into Jenkins example . If the user tries to access Jenkins Manage object instances , A security exception will be thrown . 
Content Token Reference: Variables that can be used in mail , All variables are optional .

Enter the configuration , Click on 【 Post-build operation (Post-build Actions)】, find 【Extended E-mail Notification】, Set the following :

Trigger parameter description

Failure: Instant mail for build failures . If ”Still Failing” Trigger is configured , And the state of the last build was ”Failure”, that ”Still Failing” The trigger will send an email instead of ( it ). 
Unstable: Send instant build unstable email . If ”Still Unstable” Trigger is configured , And the state of the last build was ”Unstable”, that ”Still Unstable” The trigger will send an email instead of ( it ). 
Still Failing: If the state of two or more consecutive builds is ”Failure”, Send the message . 
Success: If the state of the build is ”Successful” Send E-mail . If ”Fixed” The configured , For the state of the last build “Failure” or “Unstable”, that ”Fixed” The trigger will send an email instead of ( it ). 
Fixed: When the build state changes from “Failure” or “Unstable” Turn into ”Successful” Send email when . 
Still Unstable: If the state of two or more consecutive builds is ” Unstable “, Send the message . 
Before Build: Send mail when build starts . 

Click on 【Build Now[ Build immediately ]】, After execution . If it works , Mail will not be sent to recipients ; If execution fails , An email will be sent to the recipient , The email content is customized according to your own situation .

5.2 Mail display

6. summary

This paper mainly describes how to use Git Auto pull code , then Jenkins Build automation python Script output test report , If execution fails , Automatically send the test report to the recipient by email . There are still many optimization areas in the later stage , I hope to discuss with you .


PS: More technical dry goods , Quick attention 【 official account | xingzhe_ai】, Discuss it with the traveler !

版权声明
本文为[Traveler AI]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224214729021o.html

Scroll to Top