Run automated test projects based on Jenkins service


Computer configuration: Windows 10, Asus Tianxuan, R9-4900H, RTX2060


1. Jenkins installation


1. The Jenkins service depends on the Java environment, so you need to configure Java first, and Java 17.0.4.1 is installed here (please confirm the Java version you installed, because Jenkins is only compatible with limited Java versions).



2. Download the latest stable Jenkins WAR package to any local directory (download address: http://mirrors.jenkins.io/war-stable/latest/jenkins.war).



3. Enter the downloaded directory in CMD and run the following command to start the Jenkins service. The -Dfile.encoding=utf-8 parameter is added so that Chinese can be displayed normally.

java -Dfile.encoding=utf-8 -jar jenkins.war

If something similar to the following figure appears, the Jenkins service starts successfully.



4. Enter http://localhost:8080 and wait for the Unlock Jenkins page to appear. The red area on the page will display the path of the initial password file.



Use the more + initial password file path command in CMD to get the administrator password (such as more C:\Users\87526.jenkins\secrets\initialAdminPassword).



5. Use the password to log in to Jenkins and then install the required plug-ins to complete the installation.



2. Create a new Jenkins project


1. Before using Jenkins, you need to use the startup command to start the Jenkins service and keep the service window running, then enter http://localhost:8080/ and log in with the newly created personal identity or administrator identity (administrator username: admin, password If not changed, it is the same as the initial password).



2. Click New Item on the left side of the Dashboard to create a new Jenkins project.



3. Create a Pipeline named Test.



4. After clicking OK, it will automatically jump to the Configuration page. In fact, this page only needs to configure the pipeline (the syntax of the pipeline can be learned by itself). A simple case is provided below.

Prerequisites:
(1) The Python environment for executing the automated test project is configured locally, and the interpreter location corresponding to the environment is specified in the pipeline.
(2) Save the automated test project maintained by yourself on Github, and specify the user name and warehouse name in the pipeline by yourself (be sure to add your own SSH credentials in Jenkins).

pipeline {
    
    
    agent any
    environment {
    
    
        python_interpreter_path = ''
        git_username = ''
        repository_name = ''
    }
    stages {
    
    
        stage('前置工作') {
    
    
            steps {
    
    
                echo '工作目录:'
                sh 'pwd'
                echo '清除历史项目目录:'
                sh 'rm -rf ${WORKSPACE}/${repository_name}'
                echo '拉取项目...'
                sh 'git clone [email protected]:${git_username}/${repository_name}.git'
            }
        }
        stage('执行测试') {
    
    
            steps {
    
    
                echo '运行入口程序main.py'
                sh '${python_interpreter_path} ${WORKSPACE}/${repository_name}/main/main.py'
            }
        }
        stage('后置工作') {
    
    
            steps {
    
    
                echo '报告目录:'
                sh 'cd ${WORKSPACE}/${repository_name}/reports/'
            }
        }
    }
    post {
    
    
        success {
    
    
            echo '自动化执行成功'
        }
        failure {
    
    
            echo '自动化执行失败'
        }
    }
}


Copy the self-improved pipeline script into it and click Save to complete the creation of the Jenkins project.


3. Run the Jenkins project


1. Enter the Test project, click Build now to run the project (you can also set a scheduled task according to your needs).



2. The Build History in the lower left corner of the above picture can view the history, including the console output at runtime.

The console output shows that all six test cases in the automated test project were executed successfully.



If there are mistakes or improvements, please actively point them out!

Guess you like

Origin blog.csdn.net/embracestar/article/details/127203798