TestNG中使用@Parameters进行参数化传递

1.首先新建一个maven工程,这里就不详细赘述了,下图是我的工程结构,红框圈出来的是这次设计的类以及配置文件的位置,注意:db.properties文件的位置

2.首先配置pom.xml,我主要配置了Junit依赖,MySql依赖,TestNG依赖,代码如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

4.0.0

UTF-8

com.wx.demo

maven-web-demo

war

0.0.1-SNAPSHOT

maven-web-demo Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

com.github.pagehelper

pagehelper

3.2.1

com.google.inject

guice

4.2.2

org.testng

testng

6.14.3

test

org.uncommons

reportng

1.1.4

mysql

mysql-connector-java

8.0.16

maven-web-demo

3.在src/test/resources文件夹下面新建一个普通的file文件,命名db.properties,里面是配置的mysql的连接配置,注意:这是本地数据库,具体如何配置本地数据库请参照:https://www.jianshu.com/writer#/notebooks/34587789/notes/44947394

配置内容如下:

jdbc.driverClassName=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC

jdbc.username=root

jdbc.password=123

4.在com.wx.parameter包下面新建testNG.xml配置文件,在这个文件下面配置参数dbconfig, poolsize的值,在测试类中就会通过dbconfig, poolsize这两个参数传值给测试类,如下图,就是将dbconfig赋值为db.properties,poolsize赋值为10

5.编写测试文件TestParameterXML.java,具体解释见下面注释:

package com.wx.parameter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class TestParameterXML {

Connection con;

@Test

@Parameters({ "dbconfig", "poolsize" })//传递testNG.xml中值

public void createConnection(String dbconfig, int poolsize) throws IOException, ClassNotFoundException {

//打印testNG.xml中的值

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

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

Properties properties = new Properties();

InputStream inputStream = null;

try {

//读取配置文件,注意db.properties的位置

inputStream =TestParameterXML.class.getClassLoader().getResourceAsStream("db.properties");

properties.load(inputStream);

//获取db.properties中的值

String driverClassName = properties.getProperty("jdbc.driverClassName");

String JDBCurl = properties.getProperty("jdbc.url");

String JDBCUsername = properties.getProperty("jdbc.username");

String JDBCPassword = properties.getProperty("jdbc.password");

//分别打印出db.properties中的值

System.out.println("driverClassName="+driverClassName);

System.out.println("url="+JDBCurl );

System.out.println("username="+JDBCUsername );

System.out.println("password="+JDBCPassword );

try {

Class.forName(driverClassName);

con = DriverManager.getConnection(JDBCurl, JDBCUsername, JDBCPassword);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

在TestParameterXML.java文件中右击,Run As --> Run Configurations

选择Suite和对应路径下面的testNG 文件,选择Run

结果如下:

6.在过程中所遇到的问题

(1)时区问题

报错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

在配置文件db.properties中

jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8   我没有设置时区,导致出现报错,修改为jdbc.url=jdbc:mysql://localhost:3306/testcharacterEncoding=utf-8&serverTimezone=UTC

(2)mysql的驱动过旧

报错: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.

因为数据库驱动com.mysql.jdbc.Driver'已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver'

在配置文件db.properties中

jdbc.driverClassName=com.mysql.jdbc.Driver

修改为jdbc.driverClassName=com.mysql.cj.jdbc.Driver

(3)db.properties文件的位置

我的Java文件在src/test/java目录下面,对应的配置文件应该写在src/test/resources下面,不然读取配置文件的时候会报错

你可能感兴趣的:(TestNG中使用@Parameters进行参数化传递)