Content abstract

This article will show you how to build an integrated test environment for Python projects from zero to one, and it will detail the various issues encountered during deployment.

  • Pytest is responsible for testing Python code
  • Allure is responsible for the test report HTML display
  • Jenkins is in charge of automation

The deployment environment is MacOS. In Windows or Linux, you can deploy and operate in a similar way. In addition, the corresponding reference links are also provided.

The body of the

The first step is to install Jenkins, which depends on the JDK, so you need to install the Java JDK first, which will not be described here, and then install Jenkins, which can be installed on MacOS via HomeBrew.

brew install jenkins
Copy the code

If your brew install is slow, check out the tips at the end of this article [1].

Of course, you can install it directly from Docker, Jenkins provides the official source in Docker Hub.

docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins
Copy the code

I installed using BREW and after installing, run Jenkins with the following command

$ jenkins
Copy the code

At this point, Jenkins will explicitly run on the command line terminal and create the ~/. Jenkins directory as JENKINS_HOME with localhost:8080 as the default URL.

Startup, Jenkins will give the initial password, the password will be in the JENKINS_HOME secrets/initialAdminPassword.

After login, Jenkins will ask you to install some plug-ins, at which time the first pit will appear, and the plug-in installation speed is very slow. You can refer to Skill [2] to solve this problem. After modifying the configuration through skill [2], You can restart Jenkins through skill [3]

Jenkins installation has several key points.

  • 1. Know what version of Jenkins you have installed before you install it, because allure’s plugin in Jenkins has a version requirement for Jenkins
  • 2. Know the location of JENKINS_HOME

Allure is a visual test reporting tool that supports multiple programming languages. The Allure plugin in Jenkins makes it easy for Jenkins to call Allure (with pit), The purpose of Allure’s add-on to PyTest is to allow PyTest to generate test information files that satisfy Allure’s requirements.

The Allure plugin links Jenkins, PyTest, and Allure together. Pytest is responsible for testing, and the reports generated by testing can be used to generate a graphical interface through Allure, while Jenkins automates both processes.

On MacOS, Allure is also installed via Homebrew.

brew install allure
Copy the code

Allure is available on Windows and Linux at github.com/allure-fram…

After Allure is installed, I’m going to install The Allure plugin for PyTest.

PIP install pytest==5.3.5 PIP install allure-pytest==2.8.9 PIP install allure-python-commons==2.8.9Copy the code

After installation, you can use it first.

Find a project that uses Pytest and go directly to Github and pull the Requests library, whose Tests directory is full of test cases built with Pytest.

Run the code in Requests/Tests through PyTest and specify the folder to generate the data through –alluredir, which is allure’s readable data, usually a bunch of JSON files.

pytest requests/tests --alluredir=./allure-results
Copy the code

Once you have the JSON file data, you can use the allure generate command to generate the HTML test report interface.

allure generate allure-results -o allure-report --clean
Copy the code

Allure generates the test report into the allure- Report directory, which is halfway through the process.

Next, install Allure in Jenkins.

Go to the Jenkins Plugin management screen and install the ‘Allure Jenkins Plugin’, and continue to install the ‘HTML Publisher Plugin’ for better display of HTML test reports.

Once the ‘Allure Jenkins Plugin’ is installed, you still need to configure it by going to System Settings -> Global Tool Configuration.

For the following configuration, From Maven Central selected the current version 2.13.1.

After the configuration, reboot Jenkins and let Jenkins load the newly installed plug-in.

After installing the Jenkins plug-in, create Freestyle project in Jenkins. The Description Description can be filled freely. Select Execute Shell at the Build site. Select Execute Windows Batch Command.

Since I’m running on MacOS, select Execute Shell and type the following Shell code

#! /bin/bash
cd /Users/ayuliao/Desktop/workspace/CICD
source venv/bin/activate
pytest requests/tests --alluredir "${WORKSPACE}/target/allure-results"
exit 0
Copy the code

CD to the appropriate directory, and then activate the Python virtual environment using source. I only installed the PyTest library in the Python virtual environment, and then used PyTest to run the requests/ Tests code. But — Alluredir sets the path very carefully.

In simple terms, –alluredir must be “${WORKSPACE}/target/allure-results”, because when Jenkins calls allure genera via the plugin, The path passed in is fixed to ${WORKSPACE}/target/allure- Results.

${WORKSPACE} is a Jenkins built-in variable that represents “the absolute path allocated to the directory to build as a WORKSPACE.” .

If –alluredir is any other value, the Jenkins build project will throw allure-results does not exist, you can do so in the “github.com/jenkinsci/a…

In addition, you need to configure post-build Actions, which is also critical.

Select “Allure Report” and set the Results and Report paths to Target/Allure – Results and Target/Allure – Report respectively. This is fixed, don’t read the comment that says you can configure it any way you want.

The reason why the above configuration is fixed is based on the Console Output in Jenkins Job, and the Jenkins call allure generate command is fixed.

$ /Users/ayuliao/.jenkins/tools/ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstallation/allure/bin/allure generate "/Users/ayuliao/.jenkins/workspace/allures report/target/allure-results" -c -o "/Users/ayuliao/.jenkins/workspace/allures report/target/allure-report"
Copy the code

Problems arise if Results and Report Path are configured to other values.

And we’re done.

skills

  • [1] Homebrew faster downloading software: mirror.tuna.tsinghua.edu.cn/help/homebr… Shockerli.net/post/homebr…
  • [2] Jenkins plug-in installation speed: www.cnblogs.com/hellxz/p/je…
  • [3] by URL to operate Jenkins:www.cnblogs.com/dzblog/p/69…