内容简介:这里入门junit 为单元测试做准备,,以前随便写的github
这里入门junit 为单元测试做准备,,以前随便写的
github https://github.com/junit-team/junit5/
依照的官方文档为https://junit.org/junit5/docs/current/user-guide/#overview-java-versions
此官方文档有中文版本https://sjyuan.cc/junit5/user-guide-cn/
什么是Junit
自动化测试框架
示例工程
编写测试
package com.ming.servlet; import com.sun.org.glassfish.gmbal.ParameterNames; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { private final CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); @BeforeEach void setUp() { } @AfterEach void tearDown() { } @Test @DisplayName("ming") void testMethod() { assertTrue(coverageSampleMethods.testMethod(1,2,3)); } }
此时的maven工程
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ming</groupId> <artifactId>messageBoard</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>messageBoard Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>tomcat</groupId> <artifactId>servlet</artifactId> <version>4.0.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.0-M1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.5.0-M1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.0-M1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>messageBoard</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
测试覆盖率
行覆盖
行覆盖,为语句覆盖,度量可执行的语句是否执行到
package com.ming.servlet; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { @BeforeEach void setUp() { } @AfterEach void tearDown() { } @Test @DisplayName("testMethod") void testMethod() { CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); Assertions.assertTrue(coverageSampleMethods.testMethod(1,2,0)); } }
此时已经测试到了所有的行
条件判定覆盖
判定中的每个可能的条件都执行一遍
package com.ming.servlet; import com.sun.org.glassfish.gmbal.ParameterNames; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { @BeforeEach void setUp() { } @AfterEach void tearDown() { } @ParameterizedTest @DisplayName("testMethod") @CsvSource({ "0 ,2, 3", "1, 0, 3", }) void testMethod(int a, int b, int c) { CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); Assertions.assertTrue(coverageSampleMethods.testMethod(a,b,c)); } }
可以看到两个测试用例全部通过,此时三种条件全部覆盖掉
分支覆盖
程序中运行的分支都要覆盖掉
条件组合覆盖
条件组合覆盖,判定中的所有条件各种组合都至少出现一次
package com.ming.servlet; import com.sun.org.glassfish.gmbal.ParameterNames; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { @BeforeEach void setUp() { } @AfterEach void tearDown() { } @ParameterizedTest @DisplayName("testMethod") @CsvSource({ "1, 2, 3", "1, 2, 0", "1, 0, 3", "0, 2, 3", "0, 0, 3" }) void testMethod(int a, int b, int c) { CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); Assertions.assertTrue(coverageSampleMethods.testMethod(a,b,c)); } }
路径覆盖
测试到程序中的所有的可能的路径
package com.ming.servlet; import com.sun.org.glassfish.gmbal.ParameterNames; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { @BeforeEach void setUp() { } @AfterEach void tearDown() { } @ParameterizedTest @DisplayName("testMethod") @CsvSource({ "1, 2, 0", "1, 0, 3", "0, 0, 3" }) void testMethod(int a, int b, int c) { CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); Assertions.assertTrue(coverageSampleMethods.testMethod(a,b,c)); } }
|| 第一个条件为true不再计算后面的值
&& 第一个条件为false 不再计算后面的值
单元测试结构
package com.ming.servlet; import com.sun.org.glassfish.gmbal.ParameterNames; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; class CoverageSampleMethodsTest { // 待测试的实例 private CoverageSampleMethods coverageSampleMethods; // 定义整个测试开始的 // 全局资源 外部资源,测试桩的创建 @BeforeAll static void init(){ } // 测试完成后执行的操作 // 全局和外部资源的释放和销毁 @AfterAll static void cleanup(){ } // 每个测试用例开始的时候执行 // 基础数据 测试环境 @BeforeEach void setUp() { } // 完成后 数据清理 @AfterEach void tearDown() { } // 测试用例 @Test void testMethod(int a, int b, int c) { CoverageSampleMethods coverageSampleMethods = new CoverageSampleMethods(); Assertions.assertTrue(coverageSampleMethods.testMethod(a,b,c)); } }
断言
用于自动化测试
这里推荐Assertions
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 立体字母建模教程【C4D教程】
- PS学习教程 PS制作字体发光效果教程
- 【C4D教程】卡通风可爱小乌龟建模教程
- 卡通风仙人掌建模教程【C4D教程】
- 3D立体字体制作教程,C4D建模教程
- 3D小乌龟制作教程,C4D建模教程
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。