TestNG系列:
TestNG和Junit4的參數(shù)化測試對比
TestNG運行指定測試套件
TestNG整合ReportNG
TestNG參數(shù)化測試實戰(zhàn)
TestNG+Spring/Spring Boot整合
參數(shù)化測試是測試數(shù)據(jù)和測試腳本分離的一種實現(xiàn)方式,我們可以根據(jù)測試目的設(shè)計不同的測試數(shù)據(jù)并將測試數(shù)據(jù)存儲在各種介質(zhì)(內(nèi)存规脸、硬盤)中聚凹,測試方法在執(zhí)行時獲取一組預(yù)設(shè)的測試數(shù)據(jù)執(zhí)行并給出結(jié)果
一、首先對比下TestNG和Junit的框架整合:
- Spring+TestNG+Maven整合:
1.pom.xml中增加testng依賴:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
2.測試類增加1條注解
@ContextConfiguration(locations = "classpath:applicationContext.xml")并繼承AbstractTestNGSpringContextTests褥傍,范例如下
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class BaseTest extends AbstractTestNGSpringContextTests{
@Test
public void testMethods()
{
......
}
}
- Spring+Junit+Maven整合:
1.pom.xml中增加junit依賴:
<!--Junit版本-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
2.測試類增加2條注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml"),如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class BaseTest{
@Test
public void testMethods()
{
......
}
}
二、再對比下二者參數(shù)化測試的實現(xiàn):
Junit4 參數(shù)化測試:
- 步驟如下:
1.通過@Parameters標(biāo)識靜態(tài)參數(shù)構(gòu)造方法
2.通過測試類構(gòu)造方法引入?yún)?shù)
3.測試方法使用參數(shù) - 源碼如下:
@RunWith(Parameterized.class)
public class AuthorizedMemberHSFTest extends AuthorizedServiceTest {
private Long userId;
private String userName;
private String appName;
private String channelId;
private String secret;
private String xcodeAppKey;
private Long tenantId;
private boolean expected;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{3253622341L, "arthur.hw", "ocs", "1703183528", "123456", "123", 905L, true},
{3253622341L, "arthur.hw", "ocs", "1703183528", "123456", "123", 905L, true}});
}
public AuthorizedMemberHSFTest(Long userId,
String userName,
String appName,
String channelId,
String secret,
String xcodeAppKey,
Long tenantId,
boolean expected) {
this.userId = userId;
this.userName = userName;
this.appName = appName;
this.channelId = channelId;
this.secret = secret;
this.xcodeAppKey = xcodeAppKey;
this.tenantId = tenantId;
this.expected = expected;
}
@Test
public void authorizeMember() {
// 01:prepare parameter
Credentials credentials = new Credentials();
credentials.setUserId(userId);
credentials.setUserName(userName);
credentials.setAppName(appName);
credentials.setChannelId(channelId); // channelId cant be null
credentials.setSecret(secret);
credentials.setXcodeAppKey(xcodeAppKey);
credentials.setTenantId(tenantId);
Result<AccessToken> result = new Result<AccessToken>();
// 02 hsf execution
try {
result = authorizeService.authorizeMember(credentials);
} catch (Exception ex) {
// log.error(ex.getMessage());
}
// 03 assert
Assert.assertEquals(result.isSuccess(), expected);
}
}
缺點:
- 1個測試類只能有一個靜態(tài)的參數(shù)構(gòu)造方法data()
- 測試類需要使用@RunWith(Parameterized.class),無法兼容spring-test的runner:@RunWith(SpringJUnit4ClassRunner.class)掘剪,會導(dǎo)致無法通過注解注入待測服務(wù)
- 需要在測試類中添加一個構(gòu)造方法(一種冗余設(shè)計)
TestNG 參數(shù)化測試:
- 步驟如下:
1.通過@dataProvider注解標(biāo)識參數(shù)構(gòu)造方法
2.測試方法在注解@Test中通過dataProvider屬性指定參數(shù)構(gòu)造方法腻格,便可在測試方法中使用參數(shù) - 源碼如下:
public class AuthorizedServiceTest extends BaseTest {
@Resource
protected AuthorizeService authorizeService;
@BeforeClass
public void init() throws Exception {
ServiceUtil.waitServiceReady(authorizeService);
}
}
public class AuthorizedMemberHSFTest extends AuthorizedServiceTest {
@DataProvider
public static Object[][] getParameters(Method method) {
return new Object[][]{
{3253622341L, "arthur.hw", "ocs", "1703183528", "123456", "123", 905L, true},
{1L, "obama", "ocs", "323243242", "123", "123", 3L, true}};
}
@Test(dataProvider = "getParameters")
public void authorizeMember(Long userId,
String userName,
String appName,
String channelId,
String secret,
String xcodeAppKey,
Long tenantId,
boolean expected) {
// 01:prepare parameter
Credentials credentials = new Credentials();
credentials.setUserId(userId);
credentials.setUserName(userName);
credentials.setAppName(appName);
credentials.setChannelId(channelId);
credentials.setSecret(secret);
credentials.setXcodeAppKey(xcodeAppKey);
credentials.setTenantId(tenantId);
Result<AccessToken> result = new Result<AccessToken>();
// 02 hsf execution
try {
result = authorizeService.authorizeMember(credentials);
} catch (Exception ex) {
log.error(ex.getMessage());
}
// 03 assert
Assert.assertEquals(result.isSuccess(), expected);
}
}
執(zhí)行結(jié)果:
![d55b9f5814827446.png](https://private-alipayobjects.alipay.com/alipay-rmsdeploy-image/skylark/png/14678/d55b9f5814827446.png)
除此之外画拾,TestNG還支持通過testng.xml構(gòu)造參數(shù):
1.這次我們使用maven來運行TestNG,可以參考http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
2.在src/test/java/resources下添加testng.xml文件菜职,其中通過<parameter/>構(gòu)造需要使用的參數(shù)和值
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testmain" verbose="1" >
<parameter name="kilo" value="just for test"></parameter>
<test name="authorizeService" >
<classes>
<class name="xxx.yyy.AuthorizedServiceTest" />
</classes>
</test>
</suite>
3.在pom.xml中添加maven-surfire-plugin插件配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
4.測試方法添加參數(shù)引用:
@Parameters({"kilo"})
@Test
public void authorizeServiceTestMethod(String kilo)
{
System.out.println(kilo);
}
5.運行test:
mvn clean test
![717de23629d36961.png](https://private-alipayobjects.alipay.com/alipay-rmsdeploy-image/skylark/png/14678/717de23629d36961.png)
TestNG的參數(shù)化測試還有一些高級特性青抛,具體可以參考:http://testng.org/doc/documentation-main.html#parameters
可以看到,TestNG相比Junit酬核,基本Junit參數(shù)化測試的缺點都解決了:
1蜜另、一個測試類中可以有多個參數(shù)構(gòu)造方法,測試方法和參數(shù)構(gòu)造方法可以通過注解關(guān)聯(lián)起來
2嫡意、可以兼容spring的注解注入
3举瑰、無需添加構(gòu)造方法
同時代碼量較小