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

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容