TestNG+Spring集成

Posted 测试工程师成长之道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TestNG+Spring集成相关的知识,希望对你有一定的参考价值。


点击箭头处“蓝色字”,关注我哦!!

TestNG+Spring集成

Testng+spring

TestNG+Spring集成

首先来看下创建项目依赖包,我们仍旧使用Maven来管理,其中最重要的是

spring-test.jar

我们来看下pom.xml

<groupId>TestngSpring</groupId> <artifactId>TestngSpring</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.0.6.RELEASE</spring.version> <testng.version>6.8.7</testng.version> </properties> <dependencies>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency>
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.0.1.RELEASE</version> <scope>test</scope> </dependency> </dependencies>


创建一个Spring java接口

public interface EmailGenerator {    public String generate();}

在创建一个java类

import org.springframework.stereotype.Service;
@Servicepublic class RandomEmailGenerator implements EmailGenerator {
    public String generate() { return "zhou@126.com"; }
}

创建一个测试类

@Test@ContextConfiguration(locations = {"classpath*:/spring-test-config.xml"})public class TestSpring extends AbstractTestNGSpringContextTests{ @Autowired EmailGenerator emailGenerator;
@Test() void testEmailGeneration(){ String email=emailGenerator.generate(); System.out.println(email);
Assert.assertNotNull(email);        Assert.assertEquals(email,"zhou@126.com");    }}

在类路径下创建spring-test xml文件

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="TestngSpring"/></beans>

执行结果:

[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestngSpring ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestngSpring ---[INFO] Changes detected - recompiling the module![INFO] Compiling 2 source files to /Users/louiezhou/home/intellijS2/TestngSpring/target/classes[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.925 s[INFO] Finished at: 2019-11-20T13:26:39+08:00[INFO] Final Memory: 16M/304M[INFO] ------------------------------------------------------------------------
Process finished with exit code 0



伟大教育家孔子说过,"知行合一,学以致用", 才是做好学问的终极法宝.



TestNG+Spring集成

方便拉你进入

TestNG+Spring集成

TestNG+Spring集成



TestNG+Spring集成


TestNG+Spring集成



扫码关注

                                                         


以上是关于TestNG+Spring集成的主要内容,如果未能解决你的问题,请参考以下文章

记Spring5集成TestNG进行单元测试的采坑经历

记Spring5集成TestNG进行单元测试的采坑经历

Spring Boot教程14——集成测试

在 Citrus TestNG 测试期间 Spring Boot 未加载测试特定应用程序属性

使用TestNG实现系统集成测试

TestNG与ExtentReport集成