TestNG 强大的测试框架(2)-数据驱动:springjdbc
上一章我们了解了java读取Excel文件,返回的结果集作为TestNG框架的数据源。本章我们来聊聊SpringJDBC+DAO+TestNG
我们在做接口回归自动化的时候,经常会把接口响应返回的json数据与数据库里的数据做断言。一般的情况下我们只使用JDBC就能实现数据库的增删改查;那么我为什么推荐spring jdbc+DAO+TestNG呢,spring jdbc相比于JDBC而言,可读性高,维护简单,说白了,我就是看中它的健壮性。
DAO其实是一个数据访问层的设计机制,并不一定非要在Spring中,在各种框架的系统中,都可以用DAO来简称数据访问层。这层设计,可以灵活的要你访问各个数据库数据,缓存数据,检索数据,提供了各种方法。程序员几乎只要关注业务模块中间层的设计,数据库这块几乎处于一种托管状态。以后系统移植,升级啊,集成其他系统啊,都很方便。维护成本降低很多。
spring专门为Junit testNG提供了一套测试集成接口类——AbstractSpringContextTests类,对于testNG就是其子类:AbstractTestNGSpringContextTests。Spring和testNG整合后,进行单元测试的时只要test类继承该类,就可以方便的使用spring注入。实现了spring和testNG的无缝整合,我们可以像写普通类那样测试被spring IoC容器所管理的类
mysql驱动下载地址:https://dev.mysql.com/downloads/connector/j/
1.jdbc.properties 保存Mysql的驱动、地址、用户名以及密码
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.10.110:3306/db_group?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=123456
2.applicationContext.xml 配置需要被访问的数据源,声明jdbcteamplate的bean实现对数据库表的增删改查
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<!-- 扫描包以注册注解声明的bean -->
<context:component-scan base-package="com.test"></context:component-scan>
<!-- 配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 声明jdbctemplate bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"></bean>
</beans>
3.DAO模块 创建一个接口
package com.test;
import java.util.List;
import java.util.Map;
public interface impKvDao {
public List<Map<String, Object>> SelectKv();
}
DAO 实现接口,并索引表返回结果集
package com.test.imp;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.test.impKvDao;
@Repository
public class KvDao implements impKvDao{
@Autowired
private JdbcTemplate jdTemplate;
public List<Map<String, Object>> SelectKv(){
String sql = "select * from t_version_kv where kv='1.1.5'";
return jdTemplate.queryForList(sql);
}
}
4.TestNG 注解@ContextConfiguration。默认的从classpath目录下读取applicationContext.xml作为spring的启动配置文件(locations={"classpath:applicationContext1.xml"});该xml文件一般只需要在java项目的src目录下就可以了。如果有多个spring配置,用逗号进行分隔。
package main;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.test.imp.KvDao;
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class NewTest extends AbstractTestNGSpringContextTests{
@Autowired
private KvDao userDao;
@Test(dataProvider="testdata")
public void f(String kv,int ratio,int time,int install_post_baidu,int daily_show) {
System.out.println("kv:"+kv);
System.out.println("ratio:"+ratio);
System.out.println("install_postbaidu:"+install_post_baidu);
System.out.println("daily_show:"+daily_show);
}
@DataProvider
public Object[][] testdata(){
List<Map<String, Object>> list = userDao.SelectKv();
return new Object[][]{
{list.get(0).get("kv"),
list.get(0).get("ratio"),
list.get(0).get("time"),
list.get(0).get("install_post_baidu"),
list.get(0).get("daily_show")},
};
}
}
浙公网安备 33010602011771号