网站建设有关书籍,阿里巴巴网站优化,合肥建设银行网站,汕头网站建设哪里找文章目录 一#xff0c;导入依赖二#xff0c;基本功能一、常用断言二、执行顺序和常用注解1、通过BeforeAll类的注解来保证顺序2、通过order注解来保证执行顺序 三、依赖测试四、参数化测试五、测试套件SelectPackages、IncludePackages、SelectClasses、IncludeTags等注解的… 文章目录 一导入依赖二基本功能一、常用断言二、执行顺序和常用注解1、通过BeforeAll类的注解来保证顺序2、通过order注解来保证执行顺序 三、依赖测试四、参数化测试五、测试套件SelectPackages、IncludePackages、SelectClasses、IncludeTags等注解的使用 六、软断言七、并发测试八、动态测试解决硬编码问题九、Junit5启动类适用于持续集成 Junit5的构成 Junit PlatformJunit JupiterJunit VintageJunit Platform是Junit向测试平台演进提供平台功能的模块通过这个其他的自动化引擎可以接入Junit实现对接和执行Junit Jupiter核心承载Juint4原有功能丰富的新特性Junit Vintage主要功能是对Juint旧版本进行兼容 一导入依赖 propertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjunit-platform.version1.8.1/junit-platform.versionjunit-jupiter.version5.8.1/junit-jupiter.version/propertiesbuildpluginManagementpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-plugin/artifactIdversion3.0.0-M5/versiondependenciesdependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.8.1/version/dependency/dependenciesconfigurationincludesinclude**/*Test.java/include/includes/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.8.1/versionconfigurationsource1.8/sourcetarget1.8/target/configuration/plugin/plugins/pluginManagement/build!--核心依赖--dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.8.1/version/dependency!--套件测试使用--dependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-runner/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.junit.platform/groupIdartifactIdjunit-platform-runner/artifactIdversion1.8.1/version/dependency二基本功能
一、常用断言 二、执行顺序和常用注解
1、通过BeforeAll类的注解来保证顺序
// 执行顺序beforeAll-beforeEach-afterEach-afterAll
DisplayName(常用注解测试)
public class TestCase {/*** BeforeAll和AfterAll 必须静态修饰在所有方法执行前后只执行一次* Test 一个方法* AfterEach和BeforeEach 每次方法执行前都会执行一次* DisplayName() 类似注解的功能* RepeatedTest(5) 重复5次* Disabled 不执行该方法* Tags 打标签*/BeforeAllpublic static void beforeAll() {System.out.println(BeforeAll再每个类中只执行一次且是在开头执行);}BeforeEachpublic void beforeEach() {System.out.println(BeforeEach在每个方法执行前都会执行一次);}// junit5不需要访问修饰符// Disabled表示不执行TestDisabledDisplayName(方法1)void fun1() {System.out.println(---fun1---);}TestDisplayName(方法2)RepeatedTest(5)void fun2() {System.out.println(---fun2---);}TestTag(tag1)void tagTest(){System.out.println(tag1);}AfterEachpublic void afterEach() {System.out.println(AfterEach在每个方法执行前都会执行一次);}AfterAllpublic static void afterAll() {System.out.println(afterAll再每个类中只执行一次且是在结尾执行);}
}
2、通过order注解来保证执行顺序
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;TestMethodOrder(OrderAnnotation.class)
class OrderedTestsDemo {TestOrder(1)void nullValues() {// perform assertions against null values}TestOrder(2)void emptyValues() {// perform assertions against empty values}TestOrder(3)void validValues() {// perform assertions against valid values}
}allure注解
三、依赖测试
/*** Nested* 功能类似于suite测试套件* 从下往上执行*/public class NestedTest {private static HashMapString, Object dataMap new HashMapString, Object();Testvoid login() {dataMap.put(login, 登录成功);}Nestedclass Shopping{Testvoid shopping(){if (null!dataMap.get(buy)){System.out.println(购买成功啦);}else {System.out.println(购买失败);}}}Nestedclass Buy {Testvoid buyTest() {if (dataMap.get(login).equals(登录成功)) {System.out.println(登录成功);dataMap.put(buy, 登录成功快去购物吧);} else {System.out.println(登录失败);}}}
}四、参数化测试
写过一篇点这里
五、测试套件
SelectPackages、IncludePackages、SelectClasses、IncludeTags等注解的使用
/*** SelectPackages 选择需要执行的包*/
RunWith(JUnitPlatform.class)
SelectPackages({com.testCase2})
public class SelectPackagesTest {}/*** IncludePackages需要和SelectPackages搭配使用* IncludePackages是在SelectPackages的基础上再做一层筛选* ps:一定要注意包下的类名一定要Test开头或者结尾否则就不执行了*/
RunWith(JUnitPlatform.class)
SelectPackages({com.testCase2})
// 只执行com.testCase2.demo1
IncludePackages({com.testCase2.demo1})
public class IncludePackagesTest {
}/*** SelectClasses和IncludeTags组合使用在方法里选出对应的标签* 还有ExcludeTag*/
RunWith(JUnitPlatform.class)
SelectClasses({TestCase.class})
//IncludeTags(tag1)
ExcludeTags(tag1)
public class SelectClassesTest {
}
六、软断言
assertAll断言方法会在执行完所有断言后统⼀输出结果⼀次性暴露所有问题提高了测试脚本的健壮性。
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;import java.util.ArrayList;
import java.util.List;import static org.junit.jupiter.api.Assertions.assertAll;public class AssertAllDemo {// 普通方法第一个执行失败以后后面都不执行Testvoid assertTest1() {Assertions.assertTrue(2 1);}void assertTest2() {Assertions.assertTrue(3 1);}void assertTest3() {Assertions.assertTrue(1 1);}// 用了assertAll之后所有方法都执行Testvoid assertAllTest() {assertAll(多次结果校验,() - {Assertions.assertTrue(2 1);},() - {Assertions.assertTrue(3 1);});}Testvoid assertAllTtest02() {ListExecutable assertList new ArrayList();for (int i 0; i 10; i) {int result i;System.out.println(result);assertList.add(() - {Assertions.assertEquals(10, result);});}assertAll(多次结果校验, assertList.stream());}}
七、并发测试
测试环境基本上都是单线程操作而线上存在分布式的并发场景所以并不能暴露所有问题这里就需要用到并发junit测试是在⼀个线程中串行执行的从5.3开始⽀持并行测试。⾸先需要在配置⽂件junit-platform.properties 中配置。
#是否允许并行执行true/false
junit.jupiter.execution.parallel.enabled true
#是否支持方法级别多线程same_thread/concurrent
junit.jupiter.execution.parallel.mode.default concurrent
#是否支持类级别多线程same_thread/concurrent
junit.jupiter.execution.parallel.mode.classes.default concurrent
# the maximum pool size can be configured using a ParallelExecutionConfigurationStrategy
junit.jupiter.execution.parallel.config.strategyfixed
junit.jupiter.execution.parallel.config.fixed.parallelism4import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;/*** 并发测试*/
public class ParallelTest {static int result 0;// 加synchronized可以保证其原子性但是如果是分布式系统是不适合的需要用如redis分布式锁public static int cal(int x) throws InterruptedException {int i result;Thread.sleep(1000);result i x;return result;}public static int add(int x,int y) throws InterruptedException {Thread.sleep(1000);result y x;return result;}RepeatedTest(10)public void testCal() throws InterruptedException {long id Thread.currentThread().getId();System.out.println(线程 id 为你服务 cal(1));}RepeatedTest(10)public void testAdd() throws InterruptedException {long id Thread.currentThread().getId();System.out.println(加法计算线程 id 为你服务 add(1,2));}
}八、动态测试解决硬编码问题
传统自动化测试思路中我们的测试逻辑是在以硬编码的形式组织到代码里的当遇到用例迁移或结果整合时会产生大量的逻辑重写JUnit5提供了动态测试方案让测试人员可以在脚本Runtime时动态的批量生成用例现在我们有一个需求要把yaml文件内容转为测试用例解决思路如下
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;/*** 动态测试demo* 需求把result.yaml脚本变成测试用例* 首先把它通过反序列化变成对象* 然后把它添加到dynamicTestList*/
public class ShellTestResult {TestFactoryCollectionDynamicTest shellTestResult() throws IOException {// 新建一个列表来存储数据不是结果ListDynamicTest dynamicTestList new ArrayList();ObjectMapper mapper new ObjectMapper(new YAMLFactory());// 反序列化方式把yaml文件转换为对象列表ResultList resultList mapper.readValue(new File(D:\\interface_auto\\src\\main\\resources\\result.yaml), ResultList.class);System.out.println(done);// 动态遍历生成测试方法for (Result result : resultList.getResultList()) {// 把数据收集起来dynamicTestList.add(// 动态生成测试方法DynamicTest.dynamicTest(result.getCaseName(), () - {Assertions.assertTrue(result.isResult());}));}return dynamicTestList;}
}
import lombok.Data;Data
public class Result {private String caseName;private boolean result;public boolean isResult() {return result;}public void setResult(boolean result) {this.result result;}
}Data
public class ResultList {private ListResult resultList;
}九、Junit5启动类适用于持续集成
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;/*** junit5启动类适用于持续集成这种点不了执行的*/
public class LauncherDemo {public static void main(String[] args) {LauncherDiscoveryRequest request LauncherDiscoveryRequestBuilder.request().selectors(// 2个过滤条件是or不是andselectPackage(com.learn.junit5),selectClass(TestExecutionOrder.class)).filters(
// includeClassNamePatterns(Test.*)).build();Launcher launcher LauncherFactory.create();TestExecutionListener listener new SummaryGeneratingListener();launcher.registerTestExecutionListeners(listener);launcher.execute(request);}
}