Junit单元测试框架

Junit

  • 引入依赖
  • 注解
  • 参数化
  • 运行顺序
  • 断言
  • 测试套件

Junit单元测试框架_第1张图片

引入依赖

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.9.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


注解

Junit单元测试框架_第2张图片
Junit单元测试框架_第3张图片



    @Test
    void Test01() {
        System.out.println("这是JunitTest里面的Test01");
    }

    @Test
    void Test02() {
        System.out.println("这是JunitTest里面的Test02");
    }

    @Disabled
    //我不跑 , 你们跑
    void Test03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)"));
    }

      @BeforeAll
      static void SetUp() {
          System.out.println("在所有用例运行前跑");
      }
  
      @AfterAll
      static void TearDown() {
          System.out.println("我最后跑");
      }

    @BeforeEach
    void BeforeEachTest() {
        System.out.println("每个之前我跑");
    }

    @AfterEach
    void AfterEachTest() {
        System.out.println("每个之后我跑");
    }


参数化

    @ParameterizedTest
    //传参数
    @ValueSource(ints = {1, 2, 3})
    void Test04(int num) {
        System.out.println(num);
    }

    @ParameterizedTest
    @ValueSource(strings = {"1", "2", "3"})
    void Test05(String number) {
        System.out.println(number);
    }

    @ParameterizedTest
    //  多参数
    @CsvFileSource(resources = "test01.csv")
    void Test06(String name) {
        System.out.println(name);
    }


public class JunitTest {
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(1, "张三"),
                Arguments.arguments(2, "李四"),
                Arguments.arguments(3, "王五")
        );
    }
    @ParameterizedTest
    //多参数
    @MethodSource("Generator")
    void Test07(int num, String name) {
        System.out.println(num + ":" + name);
    }

运行顺序

//指定顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest01 {
    @Order(2)
    @Test
    void B() {
        System.out.println("B测试用例");
    }

//随机
@TestMethodOrder(MethodOrderer.Random.class)

断言

 @ParameterizedTest
    @ValueSource(ints = {1})
    void Test01(int num) {
        System.out.println(num);
        //预期相等
        Assertions.assertEquals(1, num);
        
		// 预期不相等
       Assertions.assertNotEquals(1, num);
       
		//预期为空       
		 String str = "null1";
        Assertions.assertNull(str);
    }

Junit单元测试框架_第4张图片

测试套件

@Suite
// 通过class测试用例运行
@SelectClasses({JunitTest03.class, JunitTest.class, JunitTest01.class})
public class RunSuite {
}

//通过包运行测试用例
@SelectPackages(value = {"Test09", "Test08"})

你可能感兴趣的:(junit,单元测试,1024程序员节)