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

2019-05-31  本文已影响0人  爱睡觉的小章鱼

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

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

<project xmlns="http://maven.apache.org/POM/4.0.0"

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">

<modelVersion>4.0.0</modelVersion>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<groupId>com.wx.demo</groupId>

<artifactId>maven-web-demo</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>maven-web-demo Maven Webapp</name>

<url>http://maven.apache.org</url>

<dependencies>

<!-- junit依赖管理 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<!-- testNG依赖管理 -->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>3.2.1</version>

</dependency>

<dependency>

<groupId>com.google.inject</groupId>

<artifactId>guice</artifactId>

<version>4.2.2</version>

</dependency>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.14.3</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.uncommons</groupId>

<artifactId>reportng</artifactId>

<version>1.1.4</version>

</dependency>

<!--mysql数据库连接依赖-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.16</version>

</dependency>

</dependencies>

<build>

<finalName>maven-web-demo</finalName>

</build>

</project>

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

<?xml version="1.0" encoding="UTF-8"?>

<suite name="test-parameter">

<test name="example1">

<!-- 配置dbconfig, poolsize的值-->

<parameter name="dbconfig" value="db.properties" />

<parameter name="poolsize" value="10" />

<!--这个测试类可以在编写完测试文件后再在testNG.xml文件中配置进来-->

<classes>

<class name="com.wx.parameter.TestParameterXML" />

</classes>

</test>

</suite>

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下面,不然读取配置文件的时候会报错

上一篇下一篇

猜你喜欢

热点阅读