TestNG+Jenkins+Maven参数化测试dubbo接口

1.TestNG参数化测试用例:

package com.tree.autotest.testcase.IUserBankSummaryService;

import com.datatrees.finance.risk.dataclean.service.IUserBankSummaryService;
import com.tree.autotest.BaseTestNGTest;
import com.tree.autotest.commons.CommonUtil_2;
import com.tree.autotest.commons.TestCase;
import com.tree.autotest.demo.ExcelHandle;
import com.tree.autotest.jdbc.DBIdConstant;
import com.tree.autotest.jdbc.ManagedSqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.*;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lyh on 17/3/6.
 */
public class TestGetBankIdsOfNeedMoreBill extends BaseTestNGTest {
    @Autowired
    private IUserBankSummaryService iUserBankSummaryService;

    private boolean handleOk;
    private String insertSql;
    private static String excel;
    private static Map allCases;
    private Integer testuser;
    private TestCase testcase;



    @BeforeMethod
    public void setUp() throws SQLException {
        //获取excel用例列表
        handleOk = true;
        testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");
        testuser = Integer.valueOf(testcase.param1);
        //获取SQL
        insertSql = testcase.stepSql;
        //删除,添加数据
        handleOk &= deleteData(testuser);
        handleOk &= ManagedSqlUtil.insertBySql(insertSql, DBIdConstant.MySql_Basisdata_ID);

        System.out.println("----->> @BeforeClass is finished");
    }

    @DataProvider(name = "testData")
    private static Object[][] dataprovide()throws IOException {
        excel="src/main/resources/case/IUserBankSummaryService/TestGetBankIdsOfNeedMoreBill.xlsx";
        allCases = CommonUtil_2.getAllCases(excel);
        System.out.println("dataprovide方法执行");
        Object[][] testData = ExcelHandle.readXlsx(excel, "用例列表");
        return testData;
    }

    @Test(dataProvider="testData")
    public void test_case_1(HashMap<String, String> map) throws Exception {

        testuser =Integer.valueOf(map.get("userid"));
        //实际值:
        List<Integer> actual_list = iUserBankSummaryService.getBankIdsOfNeedMoreBill(testuser);
        //预期值:
        List<Integer> excepted_list = get_excepted_list(testuser);
        //验证:
        System.out.println("userId值:"+testuser);
        System.out.println("actual_list值:"+actual_list.toString());
        System.out.println("excepted_list值:"+excepted_list.toString());
        System.out.println("size值:"+excepted_list.size());

        if (excepted_list.size()==0){
            Assert.assertEquals(actual_list.toString(), "[]");
        }else {
            Assert.assertEquals(actual_list.toString(), excepted_list.toString());
        }
        System.out.println("----->> @Test is finished");
    }


    @AfterMethod
    public void tearDown() {
        testuser = Integer.valueOf(testcase.param1);
        handleOk &= deleteData(testuser);
        System.out.println("----->> AfterClass is finished");

    }

    //定义数据清理
    public boolean deleteData(Integer userid) {
        String deleteSql = "delete from t_user_bank_summary where userid = "+userid+";";
        handleOk &= ManagedSqlUtil.deleteBySql(deleteSql, DBIdConstant.MySql_Basisdata_ID);
        return handleOk;
    }
    //预期值

    public List<Integer> get_excepted_list(Integer userid)throws SQLException{
        String searchSql = " SELECT a.BankId " +
                "    FROM t_user_bank_summary a, (SELECT MAX(Id) mid " +
                "                             FROM t_user_bank_summary " +
                "                             WHERE UserId ="+userid+" " +
                "                             GROUP BY BankId) t " +
                "    WHERE a.id = t.mid AND EffectiveBillCount12M<11";
        ResultSet rs = ManagedSqlUtil.selectBySql(searchSql,DBIdConstant.MySql_Basisdata_ID);
        List<Integer> list = new ArrayList<Integer>();
        while (rs.next()){
            list.add(rs.getInt(1));
        }
        return list;
    }

}
2.test suite配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" name="Suite" parallel="tests">
    <test name="risk-dataclean-test">
        <packages>
            <package name="com.tree.autotest.testcase.IUserCustomerService"/>
            <package name="com.tree.autotest.testcase.IVariableService"/>
            <package name="com.tree.autotest.testcase.IUserBillService"/>
            <package name="com.tree.autotest.testcase.IUserBankService"/>
            <package name="com.tree.autotest.testcase.IUserBankSummaryService"/>
            <package name="com.tree.autotest.testcase.ICustomerBackService"/>
            <package name="com.tree.autotest.testcase.IInsuranceBillService"/>
        </packages>
        <!--<classes>-->
            <!--<class name="com.tree.autotest.testcase.IUserBankService.TestSelectListByUserId"/>-->
        <!--</classes>-->
    </test> <!-- Test -->
</suite> <!-- Suite -->
3.Pom.xml设置需运行的testng.xml文件路径和依赖:
配置插件:
<!--添加插件 关联testNg.xml-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
            <file>src/main/java/com/tree/autotest/testsuite/testng.xml</file>
        </suiteXmlFiles>
        <!--<workingDirectory>target/</workingDirectory>-->
        <properties>
            <property>
                <name>usedefaultlisteners</name>
                <value>false</value>
            </property>
            <property>
                <name>listener</name>
                <value>org.uncommons.reportng.HTMLReporter,  org.uncommons.reportng.JUnitXMLReporter</value>
            </property>
        </properties>
        <workingDirectory>target/</workingDirectory>
        <forkMode>always</forkMode>
    </configuration>

</plugin>

配置POM.xml testNG相关依赖
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.4</version>
</dependency>

<!-- 依赖reportNg 关联testNg-->
<dependency>
    <groupId>org.uncommons</groupId>
    <artifactId>reportng</artifactId>
    <version>1.1.4</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 依赖Guice -->
<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>3.0</version>
    <scope>test</scope>
</dependency>
4.maven环境变量: vi .bash_profile

export JAVA_HOME=/aliyun/server/java

export JRE_HOME=/aliyun/server/java/jre

export CLASSPATH=/aliyun/server/java/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:.:$CLASSPATH

export PATH=/aliyun/server/mysql/bin:/dashu/software/apache-maven-3.39/bin:/aliyun/server/nginx/sbin:$JAVA_HOME/bin:$PATH

5.maven执行测试:

maven clean test

6.Jenkins配置:

安装TestNG插件

git:http://192.168.5.252/testgroup/risk-dataclean-test.git

Shell:

export JAVA_HOME=/aliyun/server/java
export JRE_HOME=/aliyun/server/java/jre
export CLASSPATH=/aliyun/server/java/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:.:$CLASSPATH
export PATH=/aliyun/server/mysql/bin:/dashu/software/apache-maven-3.39/bin:/aliyun/server/nginx/sbin:$JAVA_HOME/bin:$PATH
mvn clean test

TestNG Report:

**/testng-results.xml

 

7.html报告文件:

Doe 发布 [V1.0.0] 前段时间排查某问题的时候,想要快速知道某些dubbo接口(三无)的响应结果,但不想启动项目(因为这些项目不是你负责的,不会部署而且超级笨重),也不想新建一个dubbo客户端项目(占地方),也不想开telnet客户端连接口(麻烦而且有限制)。所以扣了dubbo的netty模块源码,封装了个收发客户端集成一个工具,可以快速调试dubbo接口。源码地址:https://github.com/VIPJoey/doe 极简模式 普通模式 目录结构 mmc-dubbo-api 接口项目,主要用于测试。 mmc-dubbo-provider dubbo提供者项目,主要用于测试。 mmc-dubbo-doe 主项目,实现dubbo接口调试。 deploy 部署文档 功能特性 极简模式:通过dubbo提供的telnet协议收发数据。 普通模式:通过封装netty客户端收发数据。 用例模式:通过缓存数据,方便下一次操作,依赖普通模式。 增加依赖:通过调用maven命令,下载jar包和热加载到系统,主要用来分析接口方法参数,主要作用在普通模式。 依赖列表:通过分析pom文件,展示已经加载的jar包。 其它特性 springboot 整合 redis,支持spring el 表达式。 springboot 整合 thymeleaf。 springboot 整合 logback。 netty rpc 实现原理。 开发环境 jdk 1.8 maven 3.5.3 dubbo 2.6.1 lombok 1.16.20 idea 2018 windows 7 安装步骤 安装jdk 安装maven,并设置好环境变量,仓库目录。 进入mmc-dubbo-api目录,执行mvn clean install命令,省api的jar包。 进入mmc-dubbo-doe目录,执行mvn clean install 命令,在target目录生成dubbo-doe-1.0.0-RELEASE.jar 在F盘(可以任意盘)创建目录F:\app\doe 把dubbo-doe-1.0.0-RELEASE.jar拷贝到F:\app\doe 把deploy目录中的所有文件拷贝到F:\app\doe 如果您电脑安装了git bash,可以在bash窗口运行 ./deploy.sh start,否则如果没有安装git bash,只能打开cmd切换到F:\app\doe目录,然后执行java -jar dubbo-doe-1.0.0-RELEASE.jar --spring.profiles.active=prd 打开浏览器,访问地址:http://localhost:9876/doe/home/index 全剧终
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值