持续改进的自动化框架的一点笔记

写在阅读前

  笔记的用途,能让你很快回忆起学习时候的点滴。
初学者更应该记下更多的记笔记,也许有高手点开你的笔记,
会说垃圾文章,抄袭狗,但是对于初学者,你的笔记,
也许刚好就有其他新人也遇到的坑,你刚好可以解决和你水平差不多人的问题。
 一切的工具,都是从实际应用出发!~
但是我们可以构建好自己想要的框架,也许有一天遇到更好的,会有更深刻的记忆

设计的接口框架是这样的:

可以有界面录入,可以维护,修改,展示,执行测试用例。


屏幕快照 2018-07-02 下午11.03.46.png

此类框架,场景化接口,接口加密,需要额外界面支持,数据需要入库,整体技术要求更高,需要多个人维护框架。并且框架本身可能也有bug。前期准备时间较长

2:常用型框架

测试人员维护xml文件,xml文件主要是接口入参,结果校验信息等。
此类框架,编写也比较简单,就是会有大量的xml文件,需要做好管理,优势在于,对于多样的接口,有更自由的支持,对于校验,多样化
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="suite" verbose="1" >
    <test name = "login">
        <classes>
            <class name="testcase.LoginCase"></class>
            <parameter name="logincase" value='{"urlPath":"api/autoLogin","queryParameters":{"mobile":"15821387135"},"method":"GET"}'/>
       </classes>
      </test>
</suite>

3:本次来分享一个基于自然语言格式的测试框架RestAssured

需要用到的依赖jar
      <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.5</version>
        </dependency>

//code部分
 @Test
    public void testDemo() {
        Response response = given
                .contentType("application/x-www-form-urlencoded")
                .params("mobile", "15821387135", "clientNo", 222222)
                .expect()
                // 判断 status 是不是SUCCESS
                .body("status", equalTo("SUCCESS"))
                .when()
                .get("http://events.pingan.com/api/autoLogin");
                 // 打印出 response 的body
                response.print();
    }

设置header,cookie

header和cookie的设置类似param,存在header()、headers()、
 cookie()、cookies()方法,使用也跟param类似:
Response response = given()
                .cookie("cookie","value")
                .cookies("cookiename1", "value1", "cookiename2", "value2")
                .header("Accept-Encoding:", "gzip, deflate")
                .headers("header1","value1","header2","value2")
                .get("url");

解析JSON

Rest Assured 自带支持对JSON、xml的解析
//    @Test()
    public void getHttpTest() {
        Response response = given().get("http://events.pingan.com/api/autoLogin");
               // 打印出 response 的body
                 response.print();
                 int statusCode = response.getStatusCode();
                 System.out.println(statusCode);

               //同一个字段,可以多种格式来接受statusCode
                 String statusCode2 = response.jsonPath().getInt("statusCode");
                 System.out.println(statusCode2);

                 String status = response.jsonPath().getString("statusCode");
                 System.out.println(status);

代理(proxy)配置

given().proxy("localhost", 8888)

JSON Schema Validation

可以自行百度JSON Schema Validation格式
json契约精神,非常强大的规范校验工具,估计很多人已经遗忘

JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze();

// When
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(jsonSchemaFactory));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容