Jenkins测试流水线搭建 – CI/CD中的自动化测试集成实战

前言

大家好,我是DevOps老兵。做CI/CD实践10年,测试集成是最容易被忽视的环节。今天分享Jenkins测试流水线的搭建经验。

一、测试在CI/CD中的位置

传统CI流程


代码提交 –

编译 -> 部署 -> 测试(手动)

测试往往在部署后手动执行,反馈慢、效率低。

理想CI/CD流程


代码提交 –

单元测试 -> 构建 -> 集成测试 -> 部署 -> 自动化测试 -> 监控

测试应该:

二、Jenkins Pipeline基础

Pipeline概念

Jenkins Pipeline是一系列任务的组合:

Pipeline语法

groovy
pipeline {
agent any

stages {
stage(‘构建’) {
steps {
sh ‘mvn clean package’
}
}

stage(‘测试’) {
steps {
sh ‘mvn test’
}
}

stage(‘部署’) {
steps {
sh ‘deploy.sh’
}
}
}
}

三、测试流水线搭建

单元测试集成

groovy
pipeline {
agent any

stages {
stage(‘单元测试’) {
steps {
sh ”’
# Python单元测试
pytest tests/unit -v –cov=src –cov-report=xml
”’
}
post {
always {
// 发布测试报告
publishHTML([
reportDir: ‘reports’,
reportFiles: ‘coverage.html’,
reportName: ‘单元测试覆盖率’
])
}
}
}
}
}

集成测试集成

groovy
stage(‘集成测试’) {
steps {
sh ”’
# 启动测试环境
docker-compose -f docker-compose.test.yml up -d

# 等待服务就绪
sleep 30

# 执行集成测试
pytest tests/integration -v

# 清理环境
docker-compose -f docker-compose.test.yml down
”’
}
}

API测试集成

groovy
stage(‘API测试’) {
steps {
// 使用Newman运行Postman测试
sh ”’
npm install -g newman
newman run tests/collection.json
-e tests/environment.json
-r cli,html
–reporter-html-export reports/api-report.html
”’
}
post {
always {
publishHTML([
reportDir: ‘reports’,
reportFiles: ‘api-report.html’,
reportName: ‘API测试报告’
])
}
}
}

RunnerGo集成

RunnerGo可以通过API触发测试:

groovy
stage(‘RunnerGo自动化测试’) {
steps {
sh ”’
# 调用RunnerGo API触发测试
curl -X POST
-H “Authorization: Bearer ${RUNNERGO_TOKEN}”
-H “Content-Type: application/json”
-d ‘{“plan_id”: “${PLAN_ID}”, “scene_ids”: []}’
${RUNNERGO_URL}/api/run
”’

// 等待测试完成并获取报告
script {
def reportId = ”
def status = ‘running’

while (status == ‘running’) {
sleep(time: 10, unit: ‘SECONDS’)

def response = sh(
script: ‘curl -s ${RUNNERGO_URL}/api/status’,
returnStdout: true
).trim()

def json = new groovy.json.JsonSlurper().parseText(response)
status = json.status
reportId = json.report_id
}
}
}
}

四、测试环境管理

Docker环境

使用Docker创建隔离测试环境:

yaml

docker-compose.test.yml

version: ‘3’
services:
app:
build: .
ports:
– “8080:8080”
environment:
– DB_HOST=test-db
– DB_PORT=3306

test-db:
image: mysql:8
environment:
– MYSQL_ROOT_PASSWORD=test
– MYSQL_DATABASE=test_db

redis:
image: redis:alpine

环境变量管理

groovy
pipeline {
agent any

environment {
TEST_ENV = ‘staging’
DB_HOST = ‘test-db’
API_URL = ‘http://test-api.example.com’
}

stages {
stage(‘测试’) {
steps {
sh ‘pytest tests’
}
}
}
}

五、测试报告集成

JUnit报告

groovy
stage(‘测试’) {
steps {
sh ‘pytest –junitxml=reports/junit.xml’
}
post {
always {
junit ‘reports/*.xml’
}
}
}

Allure报告

groovy
stage(‘测试’) {
steps {
sh ‘pytest –alluredir=allure-results’
}
post {
always {
allure([
includeProperties: false,
jdk: ”,
results: [[path: ‘allure-results’]],
reportBuildPolicy: ‘ALWAYS’,
report: ‘allure-report’
])
}
}
}

六、质量门禁

测试失败阻断

groovy
stage(‘测试’) {
steps {
script {
def result = sh(script: ‘pytest’, returnStatus: true)

if (result != 0) {
error(‘测试失败,阻断部署’)
}
}
}
}

覆盖率门禁

groovy
stage(‘覆盖率检查’) {
steps {
script {
def coverage = sh(
script: ‘python get_coverage.py’,
returnStdout: true
).trim().toDouble()

if (coverage < 80) {
error("覆盖率 ${coverage}% 不达标(要求80%以上)")
}
}
}
}

七、并行测试

并行执行Stage

groovy
pipeline {
agent any

stages {
stage(‘并行测试’) {
parallel {
stage(‘单元测试’) {
steps {
sh ‘pytest tests/unit’
}
}
stage(‘API测试’) {
steps {
sh ‘newman run api-tests.json’
}
}
stage(‘UI测试’) {
steps {
sh ‘pytest tests/ui’
}
}
}
}
}
}

八、定时测试任务

定时触发

groovy
pipeline {
agent any

triggers {
// 每天早上9点执行
cron(‘0 9 * * *’)
}

stages {
stage(‘定时测试’) {
steps {
sh ‘pytest tests/regression’
}
}
}
}

总结

Jenkins测试流水线要点:

环节 配置
单元测试 pytest + 覆盖率报告
集成测试 Docker环境隔离
API测试 Newman/RunnerGo集成
报告 JUnit/Allure发布
门禁 覆盖率、失败阻断
并行 parallel stage

下期预告:Appium 2.0新架构解析 – 移动端自动化测试进阶指南

分享到:

探索 RunnerGo 全栈测试平台

RunnerGo 是一款面向企业的全栈测试平台,集接口测试、自动化测试、性能测试、UI测试于一体,助力企业提升研发效能。

接口测试
性能测试
自动化测试
免费体验 RunnerGo