测试平台

Springboot Autotest

2021-07-21  本文已影响0人  ADADACHAN

Springboot Autotest

1.建立一个java项目

plugins{

    id'org.springframework.boot' version'2.5.2'

    id'io.spring.dependency-management' version'1.0.11.RELEASE'

    id'java'

    id'jacoco'

}

group ='com.kenychen'

version ='0.0.1-SNAPSHOT'

sourceCompatibility ='11'

repositories{

    mavenLocal()

maven{ url'https://maven.aliyun.com/nexus/content/groups/public/' }

    maven{ url'https://maven.aliyun.com/nexus/content/repositories/jcenter' }

    mavenCentral()

}

dependencies{

    implementation'org.springframework.boot:spring-boot-starter'

    implementation'org.springframework.boot:spring-boot-starter-web'

    testImplementation('org.springframework.boot:spring-boot-starter-test'){

        excludegroup:'org.junit.vintage',module:'junit-vintage-engine'

    }

    implementation"org.jacoco:org.jacoco.core:0.8.2"

}

test{

    useJUnitPlatform()

finalizedBy jacocoTestCoverageVerification

}

jacocoTestCoverageVerification{

    violationRules{

        rule{

            limit{

                counter ='LINE'

                value ='COVEREDRATIO'

                minimum =0.6

            }

}

}

    dependsOn jacocoTestReport

}

jacocoTestReport{

    dependsOn test

}

配置文件并且最好配置阿里云的mvn的地址

2.编写一个业务代码,假如为HomeController

package com.kenychen.autotest;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

@RequestMapping("/")

public Stringindex() {

return "Autotest with Spring Boot!";

    }

@RequestMapping("/hello")

public Stringhello() {

return "Hello in Autotest!";

    }

}

3.编写测试代码

package com.kenychen.autotest;

import static org.hamcrest.Matchers.equalTo;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.http.MediaType;

import org.springframework.test.web.servlet.MockMvc;

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest

@AutoConfigureMockMvc

public class HelloControllerTest {

@Autowired

    private MockMvcmvc;

@Test

public void getIndex()throws Exception {

mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(content().string(equalTo("Autotest with Spring Boot!")));

}

    @Test

    public void getHello()throws Exception {

mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())

.andExpect(content().string(equalTo("Autotest with Spring Boot!")));

    }

}

4.运行

本地运行测试,将会在 build/reports/jacoco/test/html/ 生成 HTML 格式的覆盖率报告。

如果行覆盖率不达标,将会报错退出。

界面运行

或者命令运行

./gradlew clean test

没有达到100%,还需要重新调试修改一下。

覆盖率设置的高了,实际是0.6,稍微修改一下jococo的标准为0.6 就通过了执行顺序为先要执行

AutotestApplication,再执行

HelloControllerTest

jococo报告

5.集成测试

pipeline{

agentany

stages{

stage('checkout'){

steps{

checkout([

$class:'GitSCM',

branches:[[name: '*']],

userRemoteConfigs:[[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]

])

}

}

stage('integrationtest'){

agent{

docker{

image'adoptopenjdk:11-jdk-hotspot'

args'-v /root/.gradle/:/root/.gradle/ -v /root/.m2/:/root/.m2/'

reuseNodetrue

}

}

steps{

sh'./gradlew test'

}

post{

//不管成功失败,都收集简易测试结果

always{

junit'build/test-results/**/*.xml'

}

//成功时,才收集测试覆盖率报告

success{

codingHtmlReport(name:'测试覆盖率报告', tag: 'test', path: 'build/reports/jacoco/test/html', entryFile: 'index.html')

}

}

}

}

}

上一篇 下一篇

猜你喜欢

热点阅读