testng执行参数_TestNG参数化测试

TestNG中的另一个有趣的功能是参数化测试。 在大多数情况下,您会遇到业务逻辑需要大量测试的场景。 参数化测试允许开发人员使用不同的值一次又一次地运行相同的测试。

TestNG可以通过两种不同的方式将参数直接传递给测试方法:

使用testng.xml

使用数据提供者

在本教程中,我们将向您展示如何通过XML

为了方便演示,这里创建一个名称为:ParameterTest 的 Maven 工程,其结构如下所示 -

1. 使用XML传递参数

在此示例中,filename属性从testng.xml传递,并通过

依懒文件:pom.xml 文件代码如下所示 -

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.yiibai

IgnoreTest

0.0.1-SNAPSHOT

jar

IgnoreTest

http://maven.apache.org

UTF-8

org.testng

testng

6.8.7

test

junit

junit

3.8.1

test

mysql

mysql-connector-java

6.0.2

compile

创建一个名称为:TestParameterXML.java,其代码如下所示 -

package com.yiibai;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.sql.Connection;

import java.sql.DriverManager;

import java.util.Properties;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class TestParameterXML {

Connection con;

@Test

@Parameters({ "dbconfig", "poolsize" })

public void createConnection(String dbconfig, int poolsize) {

System.out.println("dbconfig : " + dbconfig);

System.out.println("poolsize : " + poolsize);

Properties prop = new Properties();

InputStream input = null;

try {

// get properties file from project classpath

String path = System.getProperty("user.dir")+"\\"+dbconfig;

System.out.println("path => "+path);

//input = getClass().getClassLoader().getResourceAsStream(path);

//prop.load(input);

prop.load(new FileInputStream(dbconfig));

String drivers = prop.getProperty("jdbc.driver");

String connectionURL = prop.getProperty("jdbc.url");

String username = prop.getProperty("jdbc.username");

String password = prop.getProperty("jdbc.password");

System.out.println("drivers : " + drivers);

System.out.println("connectionURL : " + connectionURL);

System.out.println("username : " + username);

System.out.println("password : " + password);

Class.forName(drivers);

con = DriverManager.getConnection(connectionURL, username, password);

} catch (Exception e) {

//e.printStackTrace();

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

//e.printStackTrace();

}

}

}

}

}

创建一个名称为:db.properties 的文件, 其代码如下所示 -

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test

jdbc.username=root

jdbc.password=123456

创建一个名称为:testng.xml 的文件, 其代码如下所示 -

执行上面测试类代码,得到以下结果 -

[TestNG] Running:

F:\worksp\testng\ParameterTest\src\main\java\com\yiibai\testng.xml

dbconfig : db.properties

poolsize : 10

path => F:\worksp\testng\ParameterTest\db.properties

drivers : com.mysql.jdbc.Driver

connectionURL : jdbc:mysql://localhost:3306/test

username : root

password : 123456

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Tue May 02 23:11:05 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

===============================================

test-parameter

Total tests run: 1, Failures: 0, Skips: 0

===============================================

2.1. 查看一个简单的int参数。

创建一个名称为:TestParameterDataProvider.java 的文件, 其代码如下所示 -

package com.yiibai;

import org.testng.Assert;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class TestParameterDataProvider {

@Test(dataProvider = "provideNumbers")

public void test(int number, int expected) {

Assert.assertEquals(number + 10, expected);

}

@DataProvider(name = "provideNumbers")

public Object[][] provideData() {

return new Object[][] { { 10, 20 }, { 100, 110 }, { 200, 210 } };

}

}

执行上面测试类代码,得到以下结果 -

[TestNG] Running:

C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1925148879\testng-customsuite.xml

PASSED: test(10, 20)

PASSED: test(100, 110)

PASSED: test(200, 210)

===============================================

Default test

Tests run: 3, Failures: 0, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 3, Failures: 0, Skips: 0

===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 13 ms

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 34 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 7 ms

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms

[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 69 ms

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 4 ms

2.2.

创建一个名称为:TestParameterDataProvider2.java 的文件, 其代码如下所示 -

package com.yiibai;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import org.testng.Assert;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class TestParameterDataProvider2 {

@Test(dataProvider = "dbconfig")

public void testConnection(Map map) {

for (Map.Entry entry : map.entrySet()) {

System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue());

}

}

@DataProvider(name = "dbconfig")

public Object[][] provideDbConfig() {

Map map = readDbConfig();

return new Object[][] { { map } };

}

public Map readDbConfig() {

Properties prop = new Properties();

InputStream input = null;

Map map = new HashMap();

try {

input = getClass().getClassLoader().getResourceAsStream("db.properties");

prop.load(input);

map.put("jdbc.driver", prop.getProperty("jdbc.driver"));

map.put("jdbc.url", prop.getProperty("jdbc.url"));

map.put("jdbc.username", prop.getProperty("jdbc.username"));

map.put("jdbc.password", prop.getProperty("jdbc.password"));

} catch (Exception e) {

e.printStackTrace();

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return map;

}

}

执行上面测试类代码,得到以下结果 -

[TestNG] Running:

F:\worksp\testng\ParameterTest\src\main\java\com\yiibai\testng.xml

dbconfig : db.properties

poolsize : 10

path => F:\worksp\testng\ParameterTest\db.properties

drivers : com.mysql.jdbc.Driver

connectionURL : jdbc:mysql://localhost:3306/test

username : root

password : 123456

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Tue May 02 23:15:52 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

===============================================

test-parameter

Total tests run: 1, Failures: 0, Skips: 0

===============================================

此示例显示如何根据测试方法名称传递不同的参数。

创建一个名称为:TestParameterDataProvider3.java 的文件, 其代码如下所示 -

package com.yiibai;

import java.lang.reflect.Method;

import org.testng.Assert;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class TestParameterDataProvider3 {

@Test(dataProvider = "dataProvider")

public void test1(int number, int expected) {

Assert.assertEquals(number, expected);

}

@Test(dataProvider = "dataProvider")

public void test2(String email, String expected) {

Assert.assertEquals(email, expected);

}

@DataProvider(name = "dataProvider")

public Object[][] provideData(Method method) {

Object[][] result = null;

if (method.getName().equals("test1")) {

result = new Object[][] {

{ 1, 1 }, { 200, 200 }

};

} else if (method.getName().equals("test2")) {

result = new Object[][] {

{ "test@gmail.com", "test@gmail.com" },

{ "test@yahoo.com", "test@yahoo.com" }

};

}

return result;

}

}

执行上面测试类代码,得到以下结果 -

[TestNG] Running:

C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-817554174\testng-customsuite.xml

PASSED: test1(1, 1)

PASSED: test1(200, 200)

PASSED: test2("test@gmail.com", "test@gmail.com")

PASSED: test2("test@yahoo.com", "test@yahoo.com")

===============================================

Default test

Tests run: 4, Failures: 0, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 4, Failures: 0, Skips: 0

===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@1b40d5f0: 17 ms

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6ea6d14e: 51 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4563e9ab: 9 ms

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms

[TestNG] Time taken by org.testng.reporters.jq.Main@2aaf7cc2: 93 ms

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@45c8e616: 5 ms

4. @DataProvider + ITestContext

在TestNG中,我们可以使用org.testng.ITestContext来确定调用当前测试方法的运行时参数。 在最后一个例子中,我们将演示如何根据包含的分组名称传递参数。

创建一个名称为:TestParameterDataProvider4.java 的文件, 其代码如下所示 -

package com.yiibai;

import org.testng.Assert;

import org.testng.ITestContext;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class TestParameterDataProvider4 {

@Test(dataProvider = "dataProvider", groups = {"groupA"})

public void test1(int number) {

Assert.assertEquals(number, 1);

}

@Test(dataProvider = "dataProvider", groups = "groupB")

public void test2(int number) {

Assert.assertEquals(number, 2);

}

@DataProvider(name = "dataProvider")

public Object[][] provideData(ITestContext context) {

Object[][] result = null;

//get test name

//System.out.println(context.getName());

for (String group : context.getIncludedGroups()) {

System.out.println("group : " + group);

if ("groupA".equals(group)) {

result = new Object[][] { { 1 } };

break;

}

}

if (result == null) {

result = new Object[][] { { 2 } };

}

return result;

}

}

创建一个名称为:testng4.xml 的文件, 其代码如下所示 -

执行上面测试类代码,得到以下结果 -

[TestNG] Running:

F:\worksp\testng\ParameterTest\src\main\java\com\yiibai\testng4.xml

group : groupA

===============================================

test-parameter

Total tests run: 1, Failures: 0, Skips: 0

===============================================

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值