天天看點

Junit教程

這裡入門junit 為單元測試做準備,,以前随便寫的 https://junit.org/junit5/ 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

自動化測試架構

示例工程

示例工程在github上 https://github.com/junit-team/junit5-samples/tree/r5.3.0/junit5-jupiter-starter-maven

編寫測試

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));              }              }           

單元測試結構

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));              }              }           

斷言