10-23 JUnit5测试框架实战(二)

课堂PPT

PPT地址

pom依赖

<properties>
  <!--对应解析-->
  <jackson.version>2.13.4</jackson.version>
</properties>

<dependencies>
  <!--        yaml文件解析-->
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-yaml</artifactId>
      <version>${jackson.version}</version>
  </dependency>
</dependencies>

yaml解析

datas:
  - a: 1
    b: 1
    result: 2
    message: 有效2个整数【1,1】
  - a: 98
    b: 98
    result: 196
    message: 有效边界值相加【98,98】
  - a: -98
    b: 98
    result: 0
    message: 有效边界值相加【-98,98】
  - a: 98
    b: -98
    result: 0
    message: 有效边界值相加【98,-98】
  - a: -98
    b: -98
    result: -196
    message: 有效边界值相加【-98,-98】
  - a: 100
    b: 0
    result: 0
    message: 无效边界值相加【100,0】
  - a: -100
    b: -1
    result: 0
    message: 无效边界值相加【-100,-1】
  - a: 2
    b: 100
    result: 0
    message: 无效边界值相加【2,100】
  - a: 1
    b: -100
    result: 0
    message: 无效边界值相加【1,-100】

动态测试

shell_test_result.yaml

resultList:
  - caseName : 'case_1_1'
    result : true
  - caseName : 'case_1_2'
    result : false
  - caseName : 'case_2_1'
    result : true
  - caseName : 'case_2_2'
    result : false
  - caseName: 'case_1_1'
    result: true
  - caseName: 'case_1_2'
    result: false
  - caseName: 'case_2_1'
    result: true
  - caseName: 'case_2_2'
    result: false
  - caseName: 'case_1_1'
    result: true
  - caseName: 'case_1_2'
    result: false
  - caseName: 'case_2_1'
    result: true
  - caseName: 'case_2_2'
    result: false

并行测试


junit.jupiter.execution.parallel.enabled = true

junit.jupiter.execution.parallel.mode.default = concurrent

junit.jupiter.execution.parallel.mode.classes.default = same_thread

junit.jupiter.execution.parallel.config.strategy=fixed

junit.jupiter.execution.parallel.config.fixed.parallelism=8

allure-pom依赖


    <properties>
		<!--allure报告-->  
		<allure.version>2.17.2</allure.version>
        <aspectj.version>1.9.5</aspectj.version>
    </properties>




    <dependencies>

		<!--        allure报告-->  
		<dependency>  
		    <groupId>io.qameta.allure</groupId>  
		    <artifactId>allure-junit5</artifactId>  
		    <version>${allure.version}</version>  
		</dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
				<configuration>  
				<!-- <includes>  
						 <include>${test.suite}</include>
					</includes>--> 
				<!--<properties> 
						<property> 
							<name>listener</name> 
							<value>io.qameta.allure.junit5.AllureJunit5</value> 
						</property> 
					</properties> --> 
				<!--<systemProperties> 
						<property> 
							<name>allure.results.directory</name> 
							<value>${project.build.directory}/allure-results</value> 
						</property> 
						<property> 
							<name>junit.jupiter.extensions.autodetection.enabled</name> 

							<value>true</value> 
						</property> 
					</systemProperties> 
					<reportFormat>plain</reportFormat>--> 
				<argLine>  
				        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"  
				    </argLine>
				</configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

allure报告地址

allure.properties

allure.results.directory=target/allure-results

pom

<configuration>
    <argLine>
        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
    </argLine>
    <!--生成allure-result的目录-->
    <systemProperties>
        <property>
            <name>allure.results.directory</name>
            <value>./target/allure-results</value>
        </property>
    </systemProperties>
</configuration>

allure version


<properties>
    <allure.cmd.download.url>
        https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline
    </allure.cmd.download.url>
</properties>



<plugin>
  <groupId>io.qameta.allure</groupId>
  <artifactId>allure-maven</artifactId>
  <version>2.10.0</version>
  <configuration>
    <reportVersion>${allure.version}</reportVersion>
    <allureDownloadUrl>${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip</allureDownloadUrl>
  </configuration>
</plugin>

修改allure报告图片

作业

  • 数据驱动- yaml文件,对应测试方法参数为对象
  • 优化:yaml文件解析,对应测试方法参数使用自定义注解
@Test
    @DisplayName("加法正向/异常的测试用例")
    @Order(7)
    public void sumNumAllTest() throws IOException {
     //   List<Executable> sumAssertAll  = new ArrayList<>();
      //  System.out.println("输入参数:");
//        Scanner scanner = new Scanner(System.in);

        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        objectMapper.findAndRegisterModules();
        TypeReference<List<ShuJu>> listTypeReference = new TypeReference<List<ShuJu>>() {
        };
        List<ShuJu> listOrders = objectMapper.readValue(
                new File("src/test/resources/test.yaml"), listTypeReference);
        listOrders.forEach(listOrder -> {
            assertAll(()->{
                assertEquals(listOrder.getResult(),calculator.sum(listOrder.getA(),listOrder.getB()));
               
            });
        });
    }

@G_Tester 直播的时候动态测试解析yaml文件的时候 ,为啥要添加一个空的构造函数呀 ?默认情况下是有空的构造函数的吧,所以不需要再创建一个的吧
调用的时候没有实例化对象 为啥要弄一个构造函数呀 没理解,
我自己写的时候没有创建构造函数,并没有报错能正常获取呢