java接口自动化-Testng框架HttpClient框架

Posted 放牛娃@搞IT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java接口自动化-Testng框架HttpClient框架相关的知识,希望对你有一定的参考价值。

 Testng 引用文件 ==》pom.xml

快捷键配置:Alt键+回车

<dependencies>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
    </dependency>

</dependencies>

Testng 基本注解和执行顺序

@Test              // 标识这是一个测试方法

@BeforeMethod      // 这是在测试方法之前运行
@AfterMethod       // 这是在测试方法之后运行

@BeforeClass       // 这是在类运行之前运行的方法,也就是调用当前类中的第一个方法之前运行测试用例开始执行一次
@AfterClass        // 这是在类运行之后运行的方法,当前类中的所有测试方法运行之后运行,测试用例结束执行一次
import org.testng.annotations.*;

public class BasicAnnotation 

    @Test    // Alt+回车:快捷键配置testng
    public void testCase1()
        System.out.println("Test这是测试用例1111");
    

    @Test    // @Test标识这是一个测试用例方法
    public void testCase2()
        System.out.println("Test这是测试用例2222");
    

    @BeforeMethod
    public void beforeMethod()
        System.out.println("BeforeMethod这是在测试方法之前运行");
    

    @AfterMethod
    public void afterMethod()
        System.out.println("AfterMethod这是在测试方法之后运行");
    

    @BeforeClass                         // 测试用例开始执行一次
    public void beforeClass()
        System.out.println("beforeClass 这是在类运行之前运行的方法,也就是调用当前类中的第一个方法之前运行");
    

    @AfterClass                          // 测试用例结束执行一次
    public void afterClass()
        System.out.println("afterClass 这是在类运行之后运行的方法,当前类中的所有测试方法运行之后运行");
    

    @BeforeSuite
    public void f1()
        System.out.println("@BeforeSuite套件测试,类运行之前运行之前运行");
    

    @AfterSuite
    public void f2()
        System.out.println("@AfterSuite套件测试,类运行之后运行之后运行");
    

 运行结果

@BeforeSuite       // 套件测试,类运行之前运行之前运行,所有测试运行之前运行
@AfterSuite        // 套件测试,类运行之后运行之后运行,所有测试运行之后运行
@BeforeTest        // 运行属于标记内的类的任何测试方法之前运行
@AfterTest         // 运行属于标记内的类的任何测试方法之后运行
import org.testng.annotations.Test;

public class LoginTest 

    @Test
    public void loginTaoBao()
        System.out.println("登陆成功");
    

import org.testng.annotations.Test;

public class Paytest 

    @Test
    public void PaySuccess()
        System.out.println("支付成功");
    

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class suiteConfig 

    @BeforeSuite
    public void beforeSuite()
        System.out.println("Before Suite运行啦");
    

    @AfterSuite
    public void afterSuite()
        System.out.println("After Suite运行啦");
    

    @BeforeTest
    public void BeforeTest()
        System.out.println("Before Test运行啦");
    

    @AfterTest
    public void AfterTest()
        System.out.println("After Test运行啦");
    

配置文件 ==》 testng.xml

<?xml version="1.0" encoding="UTF-8" ?>
<suite name="test">
    <test name="login">
        <classes>
            <class name="testcase_testng.suite.suiteConfig"/>
            <class name="testcase_testng.suite.LoginTest"/>
        </classes>
    </test>

    <test name="Pay">
        <classes>
            <class name="testcase_testng.suite.suiteConfig"/>
            <class name="testcase_testng.suite.Paytest"/>
        </classes>
    </test>

</suite>

运行结果:

Before Suite 运行啦
Before Test 运行啦
登陆成功
After Test 运行啦
Before Test 运行啦
支付成功
After Test 运行啦
After Suite 运行啦

@Test(enabled = false)                // enabled = false 失效不执行、忽略测试
import org.testng.annotations.Test;

public class lgnoreTest 

    @Test
    public void ignore1()
        System.out.println("ignore1 执行!");
    

    @Test(enabled = false)
    public void ignore2()
        System.out.println("ignore2 执行!");
    

    @Test(enabled = true)
    public void ignore3()
        System.out.println("ignore3 执行!");
    



// 执行结果

ignore1 执行!
ignore3 执行!
@Test(description = "自己看的注释")      // 自己看的注释

依赖测试
@Test(dependsOnMethods = "test2")       // dependsOnMethods = "test2" 先运行test2方法
                                        // alwaysRun = true 报错也会运行
import org.testng.annotations.Test;

public class test 

    @Test(dependsOnMethods = "test2")
    public void test1() 
        System.out.println("test1...");
    

    @Test
    public void test2() 
        System.out.println("test2...");
    

    @Test(description = "自己看的注释")
    public void test3() 
        System.out.println("test3...");
    
import org.testng.annotations.Test;

public class DependTest 

    @Test
    public void test1()
        System.out.println("test1 run");
        throw new RuntimeException();

    

    @Test(dependsOnMethods = "test1")
    public void test2()
        System.out.println("test2 run");
    




// test2依赖于test1,当test1测试执行失败后,test2不再执行


方法组测试
@Test(groups = "server")                // 方法组测试
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class GroupsOnMethod 

    @Test(groups = "server")
    public void test1()
        System.out.println("这是服务端组的测试方法1111");
    

    @Test(groups = "server")
    public void test2()
        System.out.println("这是服务端组的测试方法2222");
    

    @Test(groups = "client")
    public void test3()
        System.out.println("这是客户端组的测试方法3333");
    

    @Test(groups = "client")
    public void test4()
        System.out.println("这是客户端组的测试方法4444");
    

    @BeforeGroups("server")
    public void beforeGroupsOnServer()
        System.out.println("这是服务端组运行之前运行的方法!!!");
    

    @AfterGroups("server")
    public void afterGroupsOnServer()
        System.out.println("这是服务端组运行之后运行的方法!!!");
    

    @BeforeGroups("client")
    public void beforeGroupsOnClient()
        System.out.println("这是客户端组运行之前运行的方法!!!");
    

    @AfterGroups("client")
    public void afterGroupsOnClient()
        System.out.println("这是客户端组运行之后运行的方法!!!");
    


 运行结果

这是服务端组运行之前运行的方法
这是服务端组的测试方法11111
这是服务端组的测试方法2222
这是服务端组运行之后运行的方法!!!!!
这是客户端组运行之前运行的方法
这是客户端组的测试方法33333
这是客户端组的测试方法4444
这是客户端组运行之后运行的方法!!!!!

类分组测试 

import org.testng.annotations.Test;

@Test(groups = "stu")
public class GroupsOnClass1 

    public void stu1()
        System.out.println("GroupsOnClass1中的stu1111运行");
    

    public void stu2()
        System.out.println("GroupsOnClass1中的stu2222运行");
    

import org.testng.annotations.Test;

@Test(groups = "stu")
public class GroupsOnClass2 
    public void stu1()
        System.out.println("GroupsOnClass222中的stu1运行");
    

    public void stu2()
        System.out.println("GroupsOnClass222中的stu2运行");
    


import org.testng.annotations.Test;

@Test(groups = "teacher")
public class GroupsOnClass3 

    public void teacher1()
        System.out.println("GroupsOnClass3中的teacher1运行");
    

    public void teacher2()
        System.out.println("GroupsOnClass3中的teacher2运行");
    


配置文件==》groupOnClass.xml

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

<suite name="suitename">
    <test name="runAll">
        <classes>
            <class name="com.course.testng.groups.GroupsOnClass1"/>
            <class name="com.course.testng.groups.GroupsOnClass2"/>
            <class name="com.course.testng.groups.GroupsOnClass3"/>
        </classes>

    </test>

    <test name="onlyRunStu">
        <groups>
            <run>
                <include name="stu"/>
            </run>

        </groups>

        <classes>
            <class name="com.course.testng.groups.GroupsOnClass1"/>
            <class name="com.course.testng.groups.GroupsOnClass2"/>
            <class name="com.course.testng.groups.GroupsOnClass3"/>
        </classes>

    </test>

</suite>

运行结果

GroupsOnClass1中的stu1111运行
GroupsOnClass1中的stu2222运行
GroupsOnClass222中的stu1运行
GroupsOnClass222中的stu2运行
GroupsOnClass3中的teacher1运行
GroupsOnClass3中的teacher2运行


GroupsOnClass1中的stu1111运行
GroupsOnClass1中的stu2222运行
GroupsOnClass222中的stu1运行
GroupsOnClass222中的stu2运行

异常测试 

import org.testng.annotations.Test;

public class ExpectedException 

    /**
     * 什么时候会用到异常测试??
     * 在我们期望结果为某一个异常的时候
     * 比如:我们传入了某些不合法的参数,程序抛出了异常
     * 也就是说我的语气结果就是这个异常。
     */

//    这是一个测试结果会失败的异常测试

    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionFailed()
        System.out.println("这是一个失败的异常测试");
    

//    这是一个成功的异常测试

    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionSuccess()
        System.out.println("这是我的异常测试");
        throw new RuntimeException();
    


参数化测试和xml文件参数化

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParamterTest 

    @Test
    @Parameters("name","age")
    public void paramTest1(String name,int age)
        System.out.println("name = " + name + ";  age = " + age);
    



 Paramter.xml 配置文件

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

<suite name="parameter">
    <test name="param">

        <classes>
            <parameter name="name" value="zhangsan"/>
            <parameter name="age" value="10"/>


            <class name="com.course.testng.paramter.ParamterTest"/>
        </classes>

    </test>

</suite>

对象传递参数化

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class DataProviderTest 

    @Test(dataProvider = "data")
    public void testDataProvider(String name,int age)
        System.out.println("name =" + name +"; age=" + age);
    

    @DataProvider(name="data")
    public Object[][] providerData()
        Object[][] o = new Object[][]
                "zhangsan",10,
                "lisi",20,
                "wangwu",30
        ;

        return o;
    

    @Test(dataProvider = "methodData")
    public void test1(String name,int age)
        System.out.println("test111方法 name="+name+";age="+age);
    
    @Test(dataProvider = "methodData")
    public void test2(String name,int age)
        System.out.println("test222方法 name="+name+";age="+age);
    

    @DataProvider(name="methodData")
    public Object[][] methodDataTest(Method method)
        Object[][] result=null;

        if(method.getName().equals("test1"))
            result = new Object[][]
                    "zhangsan",20,
                    "lisi",25
            ;
        else if(method.getName().equals("test2"))
            result = new Object[][]
                    "wangwu",50,
                    "zhaoliu",60
            ;
        

        return result;
    
    

多线程测试

import org.testng.annotations.Test;

public class MultiThreadOnAnnotion 

    @Test(invocationCount = 10,threadPoolSize = 3)
    public void test()
        System.out.println(1);
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    


import org.testng.annotations.*;

public class BasicAnnotation 

    //最基本的注解,用来把方法标记为测试的一部分
    @Test
    public void testCase1()
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
        System.out.println("Test这是测试用例1");
    

    @Test
    public void testCase2()
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
        System.out.println("Test这是测试用例2");
    

    @BeforeMethod
    public void beforeMethod()
        System.out.println("BeforeMethod这是在测试方法之前运行的");
    

    @AfterMethod
    public void afterMethod()
        System.out.println("AfterMethod这是在测试方法之后运行的");
    

    @BeforeClass
    public void beforeClass()
        System.out.println("beforeClass这是在类运行之前运行的方法");
    

    @AfterClass
    public void afterClass()
        System.out.println("afterClass这是在类运行之后运行的方法");
    

    @BeforeSuite
    public void beforeSuite()
        System.out.println("BeforeSuite测试套件");
    

    @AfterSuite
    public void afterSuite()
        System.out.println("AfterSuite测试套件");
    

import org.testng.annotations.Test;

public class MultiThreadOnXml 

    @Test
    public void test1()
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    

    @Test
    public void test2()
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    

    @Test
    public void test3()
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    


tests级别:不同的test tag下的用例可以在不同的线程下执行
           相同的test tag下的用例只能在同一个线程中去执行

classs级别:相同的class tag 下的用例在同一个线程中执行
            不同的class tag 下的用例可以在不同的线程中执行

methods级别:所有用例都可以在不同的线程下去执行

thread-count:代表了最大并发线程数

xml文件配置这种方式不能指定线程池,只有方法上才可以指定线程池
<?xml version="1.0" encoding="UTF-8" ?>

<suite name="thread" parallel="methods" thread-count="3">

    <test name = "demo1">
        <classes name="d">
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
            <class name="com.course.testng.BasicAnnotation"/>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>
        <classes name="d1">
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
            <class name="com.course.testng.BasicAnnotation"/>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>

    </test>

    <test name = "demo2">
        <classes name="d3">
            <class name="com.course.testng.BasicAnnotation"/>
        </classes>

    </test>

</suite>

 超时测试

import org.testng.annotations.Test;

public class TimeOutTest 

    @Test(timeOut = 3000)//单位为毫秒值
    public void testSuccess() throws InterruptedException 
        Thread.sleep(2000);
    

    @Test(timeOut = 2000)
    public void testFailed() throws InterruptedException 
        Thread.sleep(3000);
    

Testng测试报告

import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class TestMethodsDemo 


    @Test
    public void test1()
        Assert.assertEquals(1,2);
    

    @Test
    public void test2()
        Assert.assertEquals(1,1);
    


    @Test
    public void test3()
        Assert.assertEquals("aaa","aaa");
    


    @Test
    public void logDemo()
        Reporter.log("这是我们自己写的日志");
        throw new RuntimeException("这是我自己的运行时异常");
    



配置文件和日志模板文件

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


<suite name="我自己的接口测试套件">

    <test name="这些是测试模块">
        <classes>
            <class name="com.tester.extend.demo.TestMethodsDemo">
                <methods>
                    <include name="test1"/>
                    <include name="test2"/>
                    <include name="test3"/>
                    <include name="logDemo"/>

                </methods>


            </class>

        </classes>

    </test>

    <listeners>
        <listener class-name="com.tester.extend.demo.ExtentTestNGIReporterListener" />
    </listeners>

</suite>
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtenthtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.*;
import org.testng.xml.XmlSuite;

import java.io.File;
import java.util.*;

public class ExtentTestNGIReporterListener implements IReporter 
    //生成的路径以及文件名
    private static final String OUTPUT_FOLDER = "test-output/";
    private static final String FILE_NAME = "index.html";

    private ExtentReports extent;

    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) 
        init();
        boolean createSuiteNode = false;
        if(suites.size()>1)
            createSuiteNode=true;
        
        for (ISuite suite : suites) 
            Map<String, ISuiteResult> result = suite.getResults();
            //如果suite里面没有任何用例,直接跳过,不在报告里生成
            if(result.size()==0)
                continue;
            
            //统计suite下的成功、失败、跳过的总用例数
            int suiteFailSize=0;
            int suitePassSize=0;
            int suiteSkipSize=0;
            ExtentTest suiteTest=null;
            //存在多个suite的情况下,在报告中将同一个一个suite的测试结果归为一类,创建一级节点。
            if(createSuiteNode)
                suiteTest = extent.createTest(suite.getName()).assignCategory(suite.getName());
            
            boolean createSuiteResultNode = false;
            if(result.size()>1)
                createSuiteResultNode=true;
            
            for (ISuiteResult r : result.values()) 
                ExtentTest resultNode;
                ITestContext context = r.getTestContext();
                if(createSuiteResultNode)
                    //没有创建suite的情况下,将在SuiteResult的创建为一级节点,否则创建为suite的一个子节点。
                    if( null == suiteTest)
                        resultNode = extent.createTest(r.getTestContext().getName());
                    else
                        resultNode = suiteTest.createNode(r.getTestContext().getName());
                    
                else
                    resultNode = suiteTest;
                
                if(resultNode != null)
                    resultNode.getModel().setName(suite.getName()+" : "+r.getTestContext().getName());
                    if(resultNode.getModel().hasCategory())
                        resultNode.assignCategory(r.getTestContext().getName());
                    else
                        resultNode.assignCategory(suite.getName(),r.getTestContext().getName());
                    
                    resultNode.getModel().setStartTime(r.getTestContext().getStartDate());
                    resultNode.getModel().setEndTime(r.getTestContext().getEndDate());
                    //统计SuiteResult下的数据
                    int passSize = r.getTestContext().getPassedTests().size();
                    int failSize = r.getTestContext().getFailedTests().size();
                    int skipSize = r.getTestContext().getSkippedTests().size();
                    suitePassSize += passSize;
                    suiteFailSize += failSize;
                    suiteSkipSize += skipSize;
                    if(failSize>0)
                        resultNode.getModel().setStatus(Status.FAIL);
                    
                    resultNode.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",passSize,failSize,skipSize));
                
                buildTestNodes(resultNode,context.getFailedTests(), Status.FAIL);
                buildTestNodes(resultNode,context.getSkippedTests(), Status.SKIP);
                buildTestNodes(resultNode,context.getPassedTests(), Status.PASS);
            
            if(suiteTest!= null)
                suiteTest.getModel().setDescription(String.format("Pass: %s ; Fail: %s ; Skip: %s ;",suitePassSize,suiteFailSize,suiteSkipSize));
                if(suiteFailSize>0)
                    suiteTest.getModel().setStatus(Status.FAIL);
                
            

        
//        for (String s : Reporter.getOutput()) 
//            extent.setTestRunnerOutput(s);
//        

        extent.flush();
    

    private void init() 
        //文件夹不存在的话进行创建
        File reportDir= new File(OUTPUT_FOLDER);
        if(!reportDir.exists()&& !reportDir .isDirectory())
            reportDir.mkdir();
        
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
        // 设置静态文件的DNS
        //怎么样解决cdn.rawgit.com访问不了的情况
        htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);

        htmlReporter.config().setDocumentTitle("api自动化测试报告");
        htmlReporter.config().setReportName("api自动化测试报告");
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setCSS(".node.level-1  ul display:none; .node.level-1.active uldisplay:block;");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        extent.setReportUsesManualConfiguration(true);
    

    private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status) 
        //存在父节点时,获取父节点的标签
        String[] categories=new String[0];
        if(extenttest != null )
            List<TestAttribute> categoryList = extenttest.getModel().getCategoryContext().getAll();
            categories = new String[categoryList.size()];
            for(int index=0;index<categoryList.size();index++)
                categories[index] = categoryList.get(index).getName();
            
        

        ExtentTest test;

        if (tests.size() > 0) 
            //调整用例排序,按时间排序
            Set<ITestResult> treeSet = new TreeSet<ITestResult>(new Comparator<ITestResult>() 
                @Override
                public int compare(ITestResult o1, ITestResult o2) 
                    return o1.getStartMillis()<o2.getStartMillis()?-1:1;
                
            );
            treeSet.addAll(tests.getAllResults());
            for (ITestResult result : treeSet) 
                Object[] parameters = result.getParameters();
                String name="";
                //如果有参数,则使用参数的toString组合代替报告中的name
                for(Object param:parameters)
                    name+=param.toString();
                
                if(name.length()>0)
                    if(name.length()>50)
                        name= name.substring(0,49)+"...";
                    
                else
                    name = result.getMethod().getMethodName();
                
                if(extenttest==null)
                    test = extent.createTest(name);
                else
                    //作为子节点进行创建时,设置同父节点的标签一致,便于报告检索。
                    test = extenttest.createNode(name).assignCategory(categories);
                
                //test.getModel().setDescription(description.toString());
                //test = extent.createTest(result.getMethod().getMethodName());
                for (String group : result.getMethod().getGroups())
                    test.assignCategory(group);

                List<String> outputList = Reporter.getOutput(result);
                for(String output:outputList)
                    //将用例的log输出报告中
                    test.debug(output);
                
                if (result.getThrowable() != null) 
                    test.log(status, result.getThrowable());
                
                else 
                    test.log(status, "Test " + status.toString().toLowerCase() + "ed");
                

                test.getModel().setStartTime(getTime(result.getStartMillis()));
                test.getModel().setEndTime(getTime(result.getEndMillis()));
            
        
    

    private Date getTime(long millis) 
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    

HttpClient 框架

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Http_Client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>

    </dependencies>


</project>

get请求无参

public class MyHttpClient 

    @Test
    public void test1() throws IOException 
        // 用来请求结果
        String result;
        String url = "http://www.tpshop.com/index.php?m=Home&c=User&a=verify";
        HttpGet get = new HttpGet(url);

        // 这个是用来执行那个get方法
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);

        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

    

实际工作是基础URl等基本信息放在配置文件中 ==》application.properties

test.url = http://www.tpshop.com

test_url= http://www.litemall360.com:8080

getCookie.url = /index.php?m=Home&c=User&a=verify


login.url = /index.php?m=Home&c=User&a=do_login

login_url = /wx/auth/login
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;

public class MyCookiesForGet 

    public String url;
    private ResourceBundle bundle;
    // 存储cookies信息
    private CookieStore store;

    @BeforeTest
    public void beforeTest()
        bundle = ResourceBundle.getBundle("application");
        url = bundle.getString("test.url");
    

    @Test
    public void testGetCookies() throws IOException 
        String result;
        // 从配置文件中拼接url
        String uri = bundle.getString("getCookie.url");
        String testurl = this.url + uri;
        HttpGet get_cookies_url = new HttpGet(testurl);
        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(get_cookies_url);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

        // 获取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = store.getCookies();

        for (Cookie cookie : cookieList)
            String name = cookie.getName();
            String value =cookie.getValue();
            System.out.println(name + value);
        
    

post请求、表单格式参数、携带cookies

public class MyCookiesForGet 

    public String url;
    private ResourceBundle bundle;
    // 存储cookies信息
    private CookieStore store;

    @BeforeTest
    public void beforeTest()
        bundle = ResourceBundle.getBundle("application", Locale.CANADA);
        url = bundle.getString("test.url");
    

    @Test
    private void testPostMethod() throws IOException 
        String uri = bundle.getString("login.url");
        // 地址拼接
        String testUrl = this.url + uri;

        // 声明一个post方法
        HttpPost httpPost = new HttpPost(testUrl);

        // 添加参数
        JSONObject param = new JSONObject();
        param.put("username","15788888888");
        param.put("password","123456");
        param.put("verify_code",888);

        // 设置请求头信息、设置headers
        httpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

        // 将参数添加到方法中
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        httpPost.setEntity(entity);

        // 声明一个对象来进行响应结果的存储用来进行方法的执行,并设置cookies信息
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();


        //执行post的方法并得到响应结果
        CloseableHttpResponse response3 = httpclient.execute(httpPost);

        //就是判断返回结果是否符合预期
        int statusCode = response3.getStatusLine().getStatusCode();
        System.out.println("statusCode = "+ statusCode);

        String result = EntityUtils.toString(response3.getEntity(),"utf-8");

        if (statusCode == 200)
            System.out.println(result);
        else 
            System.out.println("登陆失败!");
        

        // 处理结果,断言是否符合预期
        // 将返回的响应结果字符串转化成json对象
        JSONObject resultJson = new JSONObject(result);

        // 具体的判断返回结果的值
        // 获取到结果值
        System.out.println(resultJson);
//        String success = (String) resultJson.get("errmsg");
//        System.out.println(success);
//        Assert.assertEquals("成功",success);

    

post请求、json格式参数、携带cookies

public class MyCookiesForPost 
    public String url;
    private ResourceBundle bundle;
    // 存储cookies信息
    private CookieStore store;

    @BeforeTest
    public void beforeTest()
        bundle = ResourceBundle.getBundle("application", Locale.CANADA);
        url = bundle.getString("test_url");
    

    @Test
    private void testPostMethod() throws IOException 
        String uri = bundle.getString("login_url");
        // 地址拼接
        String testUrl = this.url + uri;

        // 声明一个post方法
        HttpPost httpPost = new HttpPost(testUrl);

        // 添加参数
        JSONObject param = new JSONObject();
//        param.put("username","15708460952");
//        param.put("password","123456");
//        param.put("verify_code",888);

        param.put("username","user123");
        param.put("password","user123");

        // 设置请求头信息、设置headers
        httpPost.setHeader("Content-Type","application/json;charset=UTF-8");

        // 将参数添加到方法中
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        httpPost.setEntity(entity);

        // 声明一个对象来进行响应结果的存储用来进行方法的执行,并设置cookies信息
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();


        //执行post的方法并得到响应结果
        CloseableHttpResponse response3 = httpclient.execute(httpPost);

        //就是判断返回结果是否符合预期
        int statusCode = response3.getStatusLine().getStatusCode();
        System.out.println("statusCode = "+ statusCode);

        String result = EntityUtils.toString(response3.getEntity(),"utf-8");

        if (statusCode == 200)
            System.out.println(result);
        else 
            System.out.println("登陆失败!");
        

        // 处理结果,断言是否符合预期
        // 将返回的响应结果字符串转化成json对象
        JSONObject resultJson = new JSONObject(result);

        // 具体的判断返回结果的值
        // 获取到结果值
//        System.out.println(resultJson);
        String success = (String) resultJson.get("errmsg");
        System.out.println(success);
        Assert.assertEquals("成功",success);

    

处理get请求获取cookies,关联接口post请求携带cookies

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.List;
import java.util.ResourceBundle;

public class get_cookies_HttpClient 
    public String url;
    private ResourceBundle bundle;
    // 存储cookies信息
    private CookieStore store;

    @BeforeTest
    public void beforetest()
        bundle = ResourceBundle.getBundle("application");
        url = bundle.getString("test.url");
    

    @Test
    private void testGetCookies1() throws Exception 
        String result;
        String uri = bundle.getString("getCookie.url");
        String testurl = this.url + uri;

        HttpGet get_cookies_url = new HttpGet(testurl);
        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse response = client.execute(get_cookies_url);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

        // 获取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = store.getCookies();

        for (Cookie cookie : cookieList)
            String name = cookie.getName();
            String value =cookie.getValue();
            System.out.println(name + value);
        

    

    @Test
    private void testPostMethod2() throws Exception 
        String uri2 = bundle.getString("login.url");
        // 地址拼接
        String testUrl = this.url + uri2;

        // 声明一个post方法
        HttpPost httpPost = new HttpPost(testUrl);

        // 添加参数
        JSONObject param = new JSONObject();
        param.put("username","15708460952");
        param.put("password","123456");
        param.put("verify_code",8888);

        // 设置请求头信息、设置headers
        httpPost.setHeader("Content-Type","application/json;charset=UTF-8");

        // 将参数添加到方法中
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        httpPost.setEntity(entity);

        // 声明一个对象来进行响应结果的存储用来进行方法的执行,并设置cookies信息
        System.out.println(this.store);
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();


        //执行post的方法并得到响应结果
        CloseableHttpResponse response3 = httpclient.execute(httpPost);

        //就是判断返回结果是否符合预期
        int statusCode = response3.getStatusLine().getStatusCode();
        System.out.println("statusCode = "+ statusCode);

        String result = EntityUtils.toString(response3.getEntity(),"utf-8");

        if (statusCode == 200)
            System.out.println(result);
        else 
            System.out.println("登陆失败!");
        

        // 处理结果,断言是否符合预期
        // 将返回的响应结果字符串转化成json对象
        JSONObject resultJson = new JSONObject(result);

        // 具体的判断返回结果的值
        // 获取到结果值
        System.out.println(resultJson);
        String success = (String) resultJson.get("msg");
        System.out.println(success);
        Assert.assertEquals("登陆成功",success);
    


接口自动化测试框架搭建 – Java+TestNG 测试Restful service

接口自动化测试 – Java+TestNG 测试 Restful Web Service

关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, 数据分离,Java+Maven+TestNG

 

本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高。所用到的工具或类库有 TestNG, Apache POI, Jayway rest-assured,Skyscreamer - JSONassert

 

简介:

思想是数据驱动测试,用Excel来管理数据,‘Input’ Sheet中存放输入数据,读取数据后拼成request 调用service, 拿到response后写入 ‘Output’ Sheet 即实际结果, ‘Baseline’为基线(期望结果)用来和实际结果对比的,‘Comparison’ Sheet里存放的是对比结果不一致的记录,‘Result’ Sheet 是一个简单的结果报告。

 

Maven工程目录结构:

 

 

详细介绍

核心就一个测试类HTTPReqGenTest.java 由四部分组成

@BeforeTest  读取Excel (WorkBook) 的 ‘Input’ 和 ‘Baseline’ sheet

 

 

 

并且新建‘Output’, ‘Comparison’, ‘Result’ 三个空sheet

 

读取http_request_template.txt 内容转成string

 

 

@DataProvider (name = "WorkBookData")

TestNG的DataProvider, 首先用DataReader构造函数,读取Excel中Input的数据,放入HashMap<String, RecordHandler>, Map的key值就是test case的ID,value是RecordHandler对象,此对象中一个重要的成员属性就是input sheet里面 column和value 的键值对,遍历Map将test case ID 与 test case的value 即input sheet前两列的值放入List<Object[]> ,最后返回List的迭代器iterator (为了循环调用@Test方法)

 

@Test (dataProvider = "WorkBookData", description = "ReqGenTest")

测试方法,由DataProvider提供数据,首先根据ID去取myInputData里的RecordHandler, 由它和template 去生成request, 然后执行request 返回response,这些工作都是由HTTPReqGen.java这个类完成的,借助com.jayway.restassured, 返回的response是一个JSON体,通过org.skyscreamer.jsonassert.JSONCompare 与Baseline中事先填好的期望结果(同样也是JSON格式)进行比较, 根据结果是Pass还是Fail, 都会相应的往Excel里的相应Sheet写结果。

 

@AfterTest

写入统计的一些数据

关闭文件流

 

实现代码:

 HTTPReqGenTest.java

复制代码
package com.demo.qa.rest_api_test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.testng.Assert;
import org.testng.ITest;
import org.testng.ITestContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.demo.qa.utils.DataReader;
import com.demo.qa.utils.DataWriter;
import com.demo.qa.utils.HTTPReqGen;
import com.demo.qa.utils.RecordHandler;
import com.demo.qa.utils.SheetUtils;
import com.demo.qa.utils.Utils;
import com.jayway.restassured.response.Response;

public class HTTPReqGenTest implements ITest {

    private Response response;
    private DataReader myInputData;
    private DataReader myBaselineData;
    private String template;

    public String getTestName() {
        return "API Test";
    }
    
    String filePath = "";
    
    XSSFWorkbook wb = null;
    XSSFSheet inputSheet = null;
    XSSFSheet baselineSheet = null;
    XSSFSheet outputSheet = null;
    XSSFSheet comparsionSheet = null;
    XSSFSheet resultSheet = null;
    
    private double totalcase = 0;
    private double failedcase = 0;
    private String startTime = "";
    private String endTime = "";

    
    @BeforeTest
    @Parameters("workBook")
    public void setup(String path) {
        filePath = path;
        try {
            wb = new XSSFWorkbook(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        inputSheet = wb.getSheet("Input");
        baselineSheet = wb.getSheet("Baseline");

        SheetUtils.removeSheetByName(wb, "Output");
        SheetUtils.removeSheetByName(wb, "Comparison");
        SheetUtils.removeSheetByName(wb, "Result");
        outputSheet = wb.createSheet("Output");
        comparsionSheet = wb.createSheet("Comparison");
        resultSheet = wb.createSheet("Result");

        try {
            InputStream is = HTTPReqGenTest.class.getClassLoader().getResourceAsStream("http_request_template.txt");
            template = IOUtils.toString(is, Charset.defaultCharset());
        } catch (Exception e) {
            Assert.fail("Problem fetching data from input file:" + e.getMessage());
        }
        
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = sf.format(new Date());
    }

    @DataProvider(name = "WorkBookData")
    protected Iterator<Object[]> testProvider(ITestContext context) {

        List<Object[]> test_IDs = new ArrayList<Object[]>();

            myInputData = new DataReader(inputSheet, true, true, 0);
            Map<String, RecordHandler> myInput = myInputData.get_map();

            // sort map in order so that test cases ran in a fixed order
            Map<String, RecordHandler> sortmap = Utils.sortmap(myInput);
            
            for (Map.Entry<String, RecordHandler> entry : sortmap.entrySet()) {
                String test_ID = entry.getKey();
                String test_case = entry.getValue().get("TestCase");
                if (!test_ID.equals("") && !test_case.equals("")) {
                    test_IDs.add(new Object[] { test_ID, test_case });
                }
                totalcase++;
            }
            
            myBaselineData = new DataReader(baselineSheet, true, true, 0);

        return test_IDs.iterator();
    }

    @Test(dataProvider = "WorkBookData", description = "ReqGenTest")
    public void api_test(String ID, String test_case) {

        HTTPReqGen myReqGen = new HTTPReqGen();

        try {
            myReqGen.generate_request(template, myInputData.get_record(ID));
            response = myReqGen.perform_request();
        } catch (Exception e) {
            Assert.fail("Problem using HTTPRequestGenerator to generate response: " + e.getMessage());
        }
        
        String baseline_message = myBaselineData.get_record(ID).get("Response");

        if (response.statusCode() == 200)
            try {
                DataWriter.writeData(outputSheet, response.asString(), ID, test_case);
                
                JSONCompareResult result = JSONCompare.compareJSON(baseline_message, response.asString(), JSONCompareMode.NON_EXTENSIBLE);
                if (!result.passed()) {
                    DataWriter.writeData(comparsionSheet, result, ID, test_case);
                    DataWriter.writeData(resultSheet, "false", ID, test_case, 0);
                    DataWriter.writeData(outputSheet);
                    failedcase++;
                } else {
                    DataWriter.writeData(resultSheet, "true", ID, test_case, 0);
                }
            } catch (JSONException e) {
                DataWriter.writeData(comparsionSheet, "", "Problem to assert Response and baseline messages: "+e.getMessage(), ID, test_case);
                DataWriter.writeData(resultSheet, "error", ID, test_case, 0);
                failedcase++;
                Assert.fail("Problem to assert Response and baseline messages: " + e.getMessage());
            }
        else {
            DataWriter.writeData(outputSheet, response.statusLine(), ID, test_case);

            if (baseline_message.equals(response.statusLine())) {
                DataWriter.writeData(resultSheet, "true", ID, test_case, 0);
            } else {
                DataWriter.writeData(comparsionSheet, baseline_message, response.statusLine(), ID, test_case);
                DataWriter.writeData(resultSheet, "false", ID, test_case, 0);
                DataWriter.writeData(outputSheet);
                failedcase++;
            }
        }
    }

    @AfterTest
    public void teardown() {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        endTime = sf.format(new Date());
        DataWriter.writeData(resultSheet, totalcase, failedcase, startTime, endTime);
        
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

 

DataReader

复制代码
package com.demo.qa.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class that read data from XSSF sheet
 * 
 */
public class DataReader {
  
  protected static final Logger logger = LoggerFactory.getLogger(DataReader.class);

  private HashMap<String, RecordHandler> map = new HashMap<String, RecordHandler>();

  private Boolean byColumnName = false;
  private Boolean byRowKey = false;
  private List<String> headers = new ArrayList<String>();

  private Integer size = 0;

  public DataReader() {
  }

  /**
   * Primary constructor. Uses Apache POI XSSF to pull data from given excel workbook sheet. Data is stored in a
   * structure depending on the options from other parameters.
   * 
   * @param sheet Given excel sheet.
   * @param has_headers Boolean used to specify if the data has a header or not. The headers will be used as field keys.
   * @param has_key_column Boolean used to specify if the data has a column that should be used for record keys.
   * @param key_column Integer used to specify the key column for record keys.
   */
  public DataReader(XSSFSheet sheet, Boolean has_headers, Boolean has_key_column, Integer key_column) {

    XSSFRow myRow = null;
    HashMap<String, String> myList;
    size = 0;

    this.byColumnName = has_headers;
    this.byRowKey = has_key_column;
    
    try {
    
      if(byColumnName) {
        myRow = sheet.getRow(0);
        for(Cell cell: myRow) {
          headers.add(cell.getStringCellValue());
        }
        size = 1;
      }
  
      for(; (myRow = sheet.getRow(size)) != null; size++ ) {
  
        myList = new HashMap<String, String>();
  
        if(byColumnName) {
          for(int col = 0; col < headers.size(); col++ ) {
            if(col < myRow.getLastCellNum()) {
              myList.put(headers.get(col), getSheetCellValue(myRow.getCell(col))); // myRow.getCell(col).getStringCellValue());
            } else {
              myList.put(headers.get(col), "");
            }
          }
        } else {
          for(int col = 0; col < myRow.getLastCellNum(); col++ ) {
            myList.put(Integer.toString(col), getSheetCellValue(myRow.getCell(col)));
          }
        }
  
        if(byRowKey) {
          if(myList.size() == 2 && key_column == 0) {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(1)));
          } else if(myList.size() == 2 && key_column == 1) {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(0)));
          } else {
            map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList));
          }
        } else {
          map.put(Integer.toString(size), new RecordHandler(myList));
        }
      }
     
    } catch (Exception e) {
      logger.error("Exception while loading data from Excel sheet:"+e.getMessage());
    }
  }

  /**
   * Utility method used for getting an excel cell value. Cell\'s type is switched to String before accessing.
   * 
   * @param cell Given excel cell.
   */
  private String getSheetCellValue(XSSFCell cell) {

    String value = "";

    try {
      cell.setCellType(Cell.CELL_TYPE_STRING);
      value = cell.getStringCellValue();
    } catch(NullPointerException npe) {
      return "";
    }

    return value;
  }

  /**
   * Returns entire HashMap containing this class\'s data.
   * 
   * @return HashMap<String, RecordHandler>, map of ID-Record data.
   */
  public HashMap<String, RecordHandler> get_map() {

    return map;
  }


  /**
   * Gets an entire record.
   * 
   * @param record String key value for record to be returned.
   * @return HashMap of key-value pairs representing the specified record.
   */
  public RecordHandler get_record(String record) {

    RecordHandler result = new RecordHandler();

    if(map.containsKey(record)) {
      result = map.get(record);
    }

    return result;
  }

}
复制代码

 

HTTPReqGen

复制代码
package com.demo.qa.utils;

import static com.jayway.restassured.RestAssured.given;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;

/**
 * Wrapper for RestAssured. Uses an HTTP request template and a single record housed in a RecordHandler object to
 * generate and perform an HTTP requests.
 * 
 */
public class HTTPReqGen {
  
  protected static final Logger logger = LoggerFactory.getLogger(HTTPReqGen.class);

  private RequestSpecification reqSpec;

  private String call_host = "";
  private String call_suffix = "";
  private String call_string = "";
  private String call_type = "";
  private String body = "";
  private Map<String, String> headers = new HashMap<String, String>();
  private HashMap<String, String> cookie_list = new HashMap<String, String>();

  public Map<String, String> getHeaders() {
    return headers;
  }

  public String getCallString() {
    return call_string;
  }

  /**
   * Constructor. Initializes the RequestSpecification (relaxedHTTPSValidation avoids certificate errors).
   * 
   */
  public HTTPReqGen() {
    reqSpec = given().relaxedHTTPSValidation();
  }

  public HTTPReqGen(String proxy) {
    reqSpec = given().relaxedHTTPSValidation().proxy(proxy);
  }

  /**
   * Pulls HashMap from given RecordHandler and calls primary generate_request method with it.
   * 
   * @param template String, should contain the full template.
   * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template.
   * @return this Reference to this class, primarily to allow request generation and performance in one line.
   * @throws Exception 
   */
  public HTTPReqGen generate_request(String template, RecordHandler record) throws Exception {

    return generate_request(template, (HashMap<String, String>) record.get_map());
  }

  /**
   * Generates request data, using input record to fill in the template and then parse out relevant data. To fill in the
   * template, identifies tags surrounded by << and >> and uses the text from the corresponding fields in the
   * RecordHandler to replace them. The replacement is recursive, so tags may also exist in the fields of the
   * RecordHandler so long as they are also represented by the RecordHandler and do not form an endless loop.
   * After filling in the template, parses the resulting string in preparation for performing the HTTP request. Expects the
   * the string to be in the following format:
   *
   * <<call_type>> <<call_suffix>>
   * Host: <<root_host_name>>
   * <<header1_name>>:<<header1_value>>
   * ...
   * <<headerN_name>>: <<headerN_value>>
   *
   * <<body_text>>
   *
   * <<call_type>> must be GET, PUT, POST, or DELETE. <<call_suffix>> must be a string with no spaces. It is appended to
   * <<root_host_name>> to form the complete call string. After a single blank line is encountered, the rest of the file
   * is used as the body of text for PUT and POST calls. This function also expects the Record Handler to include a field
   * named "VPID" containing a unique record identifier for debugging purposes.
   * 
   * @param template String, should contain the full template.
   * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template.
   * @return this Reference to this class, primarily to allow request generation and performance in one line.
   * @throws Exception 
   */
  public HTTPReqGen generate_request(String template, HashMap<String, String> record) throws Exception {

    String filled_template = "";
    Boolean found_replacement = true;
    headers.clear();
    
    try {
      
      // Splits template into tokens, separating out the replacement strings
      // like <<id>>
      String[] tokens = tokenize_template(template);

      // Repeatedly perform replacements with data from record until no
      // replacements are found
      // If a replacement\'s result is an empty string, it will not throw an
      // error (but will throw one if there is no column for that result)
      while(found_replacement) {
        found_replacement = false;
        filled_template = "";
  
        for(String item: tokens) {
  
          if(item.startsWith("<<") && item.endsWith(">>")) {
            found_replacement = true;
            item = item.substring(2, item.length() - 2);
            
            if( !record.containsKey(item)) {
              logger.error("Template contained replacement string whose value did not exist in input record:[" + item + "]");
            }            
            
            item = record.get(item);
          }
  
          filled_template += item;
        }
  
        tokens = tokenize_template(filled_template);
      }
      
    } catch (Exception e) {
      logger.error("Problem performing replacements from template: ", e);
    }

    try {
      
      // Feed filled template into BufferedReader so that we can read it line
      // by line.
      InputStream stream = IOUtils.toInputStream(filled_template, "UTF-8");
      BufferedReader in = new BufferedReader(new InputStreamReader(stream));
      String line = "";
      String[] line_tokens;
      
      // First line should always be call type followed by call suffix
      line = in.readLine();
      line_tokens = line.split(" ");
      call_type = line_tokens[0];
      call_suffix = line_tokens[1];

      // Second line should contain the host as it\'s second token
      line = in.readLine();
      line_tokens = line.split(" ");
      call_host = line_tokens[1];

      // Full call string for RestAssured will be concatenation of call
      // host and call suffix
      call_string = call_host + call_suffix;

      // Remaining lines will contain headers, until the read line is
      // empty
      line = in.readLine();
      while(line != null && !line.equals("")) {

        String lineP1 = line.substring(0, line.indexOf(":")).trim();
        String lineP2 = line.substring(line.indexOf(" "), line.length()).trim();

        headers.put(lineP1, lineP2);

        line = in.readLine();
      }

      // If read line is empty, but next line(s) have data, create body
      // from them
      if(line != null && line.equals("")) {
        body = "";
        while( (line = in.readLine()) != null && !line.equals("")) {
          body += line;
        }
      }

    } catch(Exception e) {
      logger.error("Problem setting request values from template: ", e);
    }

    return this;
  }
  
  /**
   * Performs the request using the stored request data and then returns the response.
   * 
   * @return response Response, will contain entire response (response string and status code).
   */
  public Response perform_request() throws Exception {
    
    Response response = null;
    
    try {

      for(Map.Entry<String, String> entry: headers.entrySet()) {
        reqSpec.header(entry.getKey(), entry.getValue());
      }
  
      for(Map.Entry<String, String> entry: cookie_list.entrySet()) {
        reqSpec.cookie(entry.getKey(), entry.getValue());
      }
  
      switch(call_type) {
  
        case "GET": {
          response = reqSpec.get(call_string);
          break;
        }
        case "POST": {
          response = reqSpec.body(body).post(call_string);
          break;
        }
        case "PUT": {
          response = reqSpec.body(body).put(call_string);
          break;
        }
        case "DELETE": {
          response = reqSpec.delete(call_string);
          break;
        }
  
        default: {
          logger.error("Unknown call type: [" + call_type + "]");
        }
      }
      
    } catch (Exception e) {
      logger.error("Problem performing request: ", e);
    }

    return response;
  }

  /**
   * Splits a template string into tokens, separating out tokens that look like "<<key>>"
   * 
   * @param template String, the template to be tokenized.
   * @return list String[], contains the tokens from the template.
   */
  private String[] tokenize_template(String template) {
    return template.split("(?=[<]{2})|(?<=[>]{2})");
  }

}
复制代码

 

RecordHandler

复制代码
package com.demo.qa.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RecordHandler {

  private enum RecordType {
    VALUE, NAMED_MAP, INDEXED_LIST
  }

  private String single_value = "";
  private HashMap<String, String> named_value_map = new HashMap<String, String>();
  private List<String> indexed_value_list = new ArrayList<String>();
  private RecordType myType;

  public RecordHandler() {
    this("");
  }

  public RecordHandler(String value) {

    this.myType = RecordType.VALUE;
    this.single_value = value;

  }

  public RecordHandler(HashMap<String, String> map) {

    this.myType = RecordType.NAMED_MAP;
    this.named_value_map = map;

  }

  public RecordHandler(List<String> list) {

    this.myType = RecordType.INDEXED_LIST;
    this.indexed_value_list = list;

  }

  public HashMap<String, String> get_map() {
    return named_value_map;
  }

  public int size() {
    int result = 0;

    if(myType.equals(RecordType.VALUE)) {
      result = 1;
    } else if(myType.equals(RecordType.NAMED_MAP)) {
      result = named_value_map.size();
    } else if(myType.equals(RecordType.INDEXED_LIST)) {
      result = indexed_value_list.size();
    }

    return result;
  }

  public String get() {
    String result = "";

    if(myType.equals(RecordType.VALUE)) result = single_value;
    else {
      System.out.println("Called get() on wrong type:" + myType.toString());
    }

    return result;
  }

  public String get(String key) {
    String result = "";

    if(myType.equals(RecordType.NAMED_MAP)) result = named_value_map.get(key);

    return result;
  }

  public String get(Integer index) {
    String result = "";

    if(myType.equals(RecordType.INDEXED_LIST)) result = indexed_value_list.get(index);

    return result;
  }

  public Boolean set(String value) {
    Boolean result = false;

    if(myType.equals(RecordType.VALUE)) {
      this.single_value = value;
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST)) {
      this.indexed_value_list.add(value);
      result = true;
    }

    return result;
  }

  public Boolean set(String key, String value) {
    Boolean result = false;

    if(myType.equals(RecordType.NAMED_MAP)) {
      this.named_value_map.put(key, value);
      result = true;
    }

    return result;
  }

  public Boolean set(Integer index, String value) {
    Boolean result = false;

    if(myType.equals(RecordType.INDEXED_LIST)) {
      if(this.indexed_value_list.size() > index) this.indexed_value_list.set(index, value);

      result = true;
    }

    return result;
  }

  public Boolean has(String value) {
    Boolean result = false;

    if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) {
      result = true;
    } else if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) {
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) {
      result = true;
    }

    return result;
  }

  public Boolean remove(String value) {
    Boolean result = false;

    if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) {
      this.single_value = "";
      result = true;
    }
    if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) {
      this.named_value_map.remove(value);
      result = true;
    } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) {
      this.indexed_value_list.remove(value);
      result = true;
    }

    return result;
  }

  public Boolean remove(Integer index) {
    Boolean result = false;

    if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(index)) {
      this.indexed_value_list.remove(index);
      result = true;
    }

    return result;
  }

}
复制代码

 

其它不重要的类不一一列出来了。

 

pom.xml

复制代码
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>qa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Automation</name>
    <description>Test project for Demo</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.5</version>
                    <executions>
                        <execution>
                            <id>default-jar</id>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                            <configuration>
                                <archive>
                                    <manifest>
                                        <mainClass>com.demo.qa.utils.TestStartup</mainClass>
                                        <addClasspath>true</addClasspath>
                                        <classpathPrefix>lib/</classpathPrefix>
                                        <useUniqueVersions>false</useUniqueVersions>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <skip>true</skip>
                        <suiteXmlFiles>
                            <suiteXmlFile>src\\test\\resources\\HTTPReqGenTest.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.8</version>
                    <executions>
                        <execution>
                            <id>default-cli</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>json-path</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-codec</artifactId>
                    <groupId>commons-codec</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
        </dependency>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>xml-apis</artifactId>
                    <groupId>xml-apis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.6</version>
        </dependency>
    </dependencies>
</project>
复制代码

 

运行是通过TestNG的xml文件来执行的, 里面配置了Parameter “workBook” 的路径

 

 

 

TestNG的运行结果都是Pass, 但事实上里面有case是Fail的,我只是借助TestNG来运行,我并没有在@Test方法里加断言Assert, 所以这里不会Fail, 我的目的是完全用Excel来管理维护测试数据以及测试结果,做到数据脚本完全分离。

 

 

Output sheet

 

Comparison sheet

 

Result sheet

 

 

当然 你也可以把maven工程打成一个可执行jar来运行,不过需要增加一个main函数作为入口,xml测试文件通过参数传递进去,另外还需要在pom里配置一些插件,像maven-jar-plugin。

如果你还需要做back-end DB check,你可以在Input里再增加几列,你要查询的表,字段,Baseline里也相应的加上期望结果,这里就不再赘述了。

 

http://www.cnblogs.com/wade-xu/p/4229805.html 

注:转载需注明出处及作者名。

以上是关于java接口自动化-Testng框架HttpClient框架的主要内容,如果未能解决你的问题,请参考以下文章

接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告

基于Java+HttpClient+TestNG的接口自动化测试框架------ 小结与展望

基于Java+HttpClient+TestNG的接口自动化测试框架------ 小结与展望

基于Java+HttpClient+TestNG的接口自动化测试框架-------参数存取处理

Eclipse+TestNG搭建接口自动化测试框架

基于Java+HttpClient+TestNG的接口自动化测试框架-------随机函数的处理