初学Junit5

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hogwarts</groupId>
    <artifactId>junit5demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>junit5demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <aspectj.version>1.8.10</aspectj.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.targer>1.8</maven.compiler.targer>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!--junit5-->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--allure2-->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>2.10.0</version>
            <scope>test</scope>
        </dependency>


        <!--allure命令行工具 查看测试报告
         https://mvnrepository.com/artifact/io.qameta.allure/allure-commandline -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-commandline</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--allure2-->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <!--打包遇到错误选择忽略 测试代码时遇到错误,它会停止编译, true 不会中断-->
                    <testFailureIgnore>true</testFailureIgnore>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <systemProperties>
                        <property>
                            <name>junit.jupiter.extensions.autodetection.enabled</name>
                            <value>true</value>
                        </property>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.2.0</version>
                    </dependency>
                    <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>2.10.0</version>
                <configuration>
                    <reportVersion>2.4.1</reportVersion>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

提供的注解:

@Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试

@ParameterizedTest :表示方法是参数化测试

@RepeatedTest :表示方法可重复执行

@DisplayName :为测试类或者测试方法设置展示名称

@DisplayNameGeneration

@BeforeEach :表示在每个单元测试之前执行

@AfterEach :表示在每个单元测试之后执行 

@BeforeAll :表示在所有单元测试之前执行  必须为static方法

@AfterAll :表示在所有单元测试之后执行     必须为static方法

@Nested:表示带注释的类是非静态嵌套测试类。

                  嵌套测试使测试编写者有更多的能力来表达几组测试之间的关系

@BeforeAll和@AfterAll方法不能直接在@Nested测试类中使用,除非使用“每类”测试实例生命周期。这样的注释不会被继承。

@Tag :表示单元测试类别,类似于JUnit4中的@Categories

@Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore

@Timeout :表示测试方法运行如果超过了指定时间将会返回错误

@ExtendWith :为测试类或测试方法提供扩展类引用

@RegisterExtension

@TempDir

@TestFactory

@TestTemplate

@TestMethodOrder

@TestInstance

套件执行

@Runwith(JUnitplatform.class)

 

 

 

----------------------------------------------------------------------------------

@ParameterizedTest :表示方法是参数化测试

@ValueSource(strings = {"xiao","hhha"})

@NullSource  //提供一个null入参

@EmptySource//提供一个空入参

@ParameterizedTest
@ValueSource(strings = {"xiao","hhha"})
@NullSource  
@EmptySource
public void testValueSource(String name) {
    System.out.println(name);
    assertTrue(!StringUtils.isEmpty(name));
}

输出结果:

xiao 

name

null

      //空格

 

    @ParameterizedTest
    @MethodSource()        //不指定方法名 会自动查找同名无参的静态方法
    public void testMethodSource(String name) {
        System.out.println(name);
        assertTrue(!StringUtils.isEmpty(name));
    }
    public static Stream<String> testMethodSource(){  //同名无参的静态方法
        return Stream.of("ss","dfgfdsg");
    }

    @ParameterizedTest
    @MethodSource("testMethodSource")//指定方法名 
    public void testProvider(String name) {
        System.out.println(name);
        assertTrue(!StringUtils.isEmpty(name));
    }
 

    @ParameterizedTest
//    @CsvSource( {
//            "xiaoming,12","xiaohong,20","hahah,30"
//    })
    @CsvSource({ "apple, ''" })
    public void testCsvsoure(String name, String age) {
        System.out.println("name:" + name + ",age:"+age);
    }

    @ParameterizedTest
    @CsvFileSource(resources = "/stu.csv",numLinesToSkip = 1)
    public void testCsvFilesoure(String name, String age) {
        System.out.println("name:" + name + ",age:"+age);

    }

/stu.csv内容/

name,age
xiaoming,10
xiaohong,10
dssd,13

///

 

动态测试  价值:一键生成很多测试用例,不用手动测试用例

场景: 有测试数据和测试结果, 使用动态测试生成伪测试用例,生成测试报告。 怎么理解?不用写测试用例只写assert?

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值