FavoriteLoading
0

接口测试初学者心得--从 1 往后

之前从0到1也花了一部分时间,现在从1要开始了,我也继续发个帖来总结下。
从0到1,从0到1(1)从0到1(2)

虽然我是一个接口的菜鸟,但我还是有一颗愿意分享给大家的心,大家轻喷。

结构


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.
├── 业务数据驱动.xml
├── 各种环境切换.xml
├── README.md
├── 账号管理.xml
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── monkey
│   │               └── app
│   │                   └── my_rest_test
│   │                       └── App.java
│   └── <span class="nb">test</span>
│       ├── java
│       │   └── com
│       │       └── musical
│       │           ├── 业务模块1
│       │           │   ├── Time
│       │           │   │   ├── 业务模块接口定义.java
│       │           │   │   ├── 业务模块实现.java
│       │           │   │   └── 测试用例.java
│       │           │   └── utils
│       │           │       ├── 加密.java
│       │           │       ├── TestngDataProvider.java
│       │           │       ├── AssertUtils.java
│       │           │       ├── Base64.java
│       │           │       ├── 解密.java
│       │           │       ├── JsonUtils.java
│       │           │       ├── ParameterDataProvider.java
│       │           │       ├── 解析xml.java
│       │           │       ├── RequestsUtil.java
│       └── resources
│           ├── 业务1.JSON
│           ├── 业务2.JSON
│           ├── 业务3.JSON
│           ├── ....JSON
├── target
│   ├── classes
│   └── <span class="nb">test</span>-classes
│       ├── 业务1.JSON
│       ├── 业务2.JSON
│       ├── 业务3.JSON
│       ├── .....JSON
│   ├── passed.png
│   ├── skipped.png
│   ├── testng-failed.xml
│   ├── testng-reports.css
│   ├── testng-reports.js
│   ├── testng-results.xml
│   └── testng.css
└── testNG.xml

基础设计

由于开始就说好是TestAssured,所以我们看下基础的设计


1
2
3
4
5
6
7
8
9
10
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">BaseTest</span> <span class="o">{</span>

    <span class="nd">@BeforeMethod</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="n">setUp</span><span class="o">()</span> <span class="o">{</span>
        <span class="n">RestAssured</span><span class="o">.</span><span class="na">baseURI</span> <span class="o">=</span> <span class="n">ParserConfig</span><span class="o">.</span><span class="na">getInstance</span><span class="o">().</span><span class="na">getBaseURI</span><span class="o">();</span>
        <span class="c1">// RestAssured.port = 8080;</span>
        <span class="n">RestAssured</span><span class="o">.</span><span class="na">basePath</span> <span class="o">=</span> <span class="s">""</span><span class="o">;</span>
        <span class="n">RestAssured</span><span class="o">.</span><span class="na">defaultParser</span> <span class="o">=</span> <span class="n">Parser</span><span class="o">.</span><span class="na">JSON</span><span class="o">;</span>

    <span class="o">}</span>

public static String 接口1(参数1,参数2) {
return url_parm;
}


1
2
3
4
5
6
7
8
9
10
11
请求+断言实现
---
* Post,Get等方法都一样


```java
    public static void AssertJSONStructurePost(String CookieType, String Cookie, int statusCode, String url,
            String JSON_Name) {
        given().log().all().header(CookieType, Cookie).expect().statusCode(statusCode).when().post(url).then()
                .assertThat().body(matchesJsonSchemaInClasspath(JSON_Name));
    }
  • online等方法也一样如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">AssertJSONStructureOnlinePut</span><span class="p">(</span><span class="n">String</span> <span class="n">baseURI</span><span class="o">,</span> <span class="n">String</span> <span class="n">basePath</span><span class="o">,</span> <span class="n">String</span> <span class="n">Cookie_type</span><span class="o">,</span> <span class="n">String</span> <span class="n">Cookie</span><span class="o">,</span>
            <span class="n">String</span> <span class="n">url</span><span class="o">,</span> <span class="n">String</span> <span class="n">JSON_Name</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">String</span> <span class="n">request_url</span> <span class="o">=</span> <span class="n">baseURI</span> <span class="o">+</span> <span class="n">basePath</span><span class="o">;</span>
        <span class="k">try</span> <span class="o">{</span>
            <span class="n">JSONObject</span> <span class="n">encryptJson</span> <span class="o">=</span> <span class="n">EncryptUtils</span><span class="o">.</span><span class="na">createHeader</span><span class="o">(</span><span class="n">Cookie</span><span class="o">,</span> <span class="n">request_url</span><span class="o">,</span> <span class="s">"请求方式"</span><span class="o">);</span>

            <span class="n">given</span><span class="o">().</span><span class="na">log</span><span class="o">().</span><span class="na">all</span><span class="o">().</span><span class="na">header</span><span class="o">(</span><span class="n">Cookie_type</span><span class="o">,</span> <span class="n">Cookie</span><span class="o">)</span>
                    <span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="s">"key1"</span><span class="o">,</span> <span class="n">encryptJson</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"value1"</span><span class="o">))</span>
                    <span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="s">"key2"</span><span class="o">,</span> <span class="n">encryptJson</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"value2"</span><span class="o">))</span>
                    <span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="s">"key3"</span><span class="o">,</span> <span class="n">encryptJson</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"value3"</span><span class="o">))</span>
                    <span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="s">"key4"</span><span class="o">,</span> <span class="n">encryptJson</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"value4"</span><span class="o">)).</span><span class="na">expect</span><span class="o">()</span>
                    <span class="o">.</span><span class="na">statusCode</span><span class="o">(</span><span class="mi">200</span><span class="o">).</span><span class="na">when</span><span class="o">().</span><span class="na">get</span><span class="o">(</span><span class="n">url</span><span class="o">).</span><span class="na">then</span><span class="o">().</span><span class="na">assertThat</span><span class="o">().</span><span class="na">body</span><span class="o">(</span><span class="n">matchesJsonSchemaInClasspath</span><span class="o">(</span><span class="n">JSON_Name</span><span class="o">));</span>
        <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
            <span class="c1">// TODO Auto-generated catch block</span>
            <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
        <span class="o">}</span>
    <span class="o">}</span>

验证

当然作为API第一步先验证的是结构,就如同我之前上面的方法名写的,所以在我架构这个工程的时候会有很多的.JSON文件,这个就是为了来验证json结构的。
比如我们又一个返回


1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="o">{</span>
  <span class="s2">"A"</span>: <span class="nb">true</span>,
  <span class="s2">"B"</span>: <span class="nb">false</span>,
  <span class="s2">"C"</span>: <span class="o">{</span>
    <span class="s2">"D"</span>: <span class="nb">false</span>,
    <span class="s2">"E"</span>: <span class="s2">"testerhome"</span>,
    <span class="s2">"F"</span>: <span class="nb">false</span>,
    <span class="s2">"G"</span>: <span class="nb">false</span>,
    <span class="s2">"H"</span>: <span class="nb">false</span>
  <span class="o">}</span>,
  <span class="s2">"I"</span>: 1463990673992,
  <span class="s2">"J"</span>: <span class="nb">false</span>
<span class="o">}</span>

所以我们的.JSON就是


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<span class="o">{</span>
  <span class="s2">"</span><span class="nv">$schema</span><span class="s2">"</span>: <span class="s2">"http://json-schema.org/draft-04/schema#"</span>,
  <span class="s2">"type"</span>: <span class="s2">"object"</span>,
  <span class="s2">"properties"</span>: <span class="o">{</span>
    <span class="s2">"A"</span>: <span class="o">{</span>
      <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
    <span class="o">}</span>,
    <span class="s2">"B"</span>: <span class="o">{</span>
      <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
    <span class="o">}</span>,
    <span class="s2">"C"</span>: <span class="o">{</span>
      <span class="s2">"type"</span>: <span class="s2">"object"</span>,
      <span class="s2">"properties"</span>: <span class="o">{</span>
        <span class="s2">"D"</span>: <span class="o">{</span>
          <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
        <span class="o">}</span>,
        <span class="s2">"E"</span>: <span class="o">{</span>
          <span class="s2">"type"</span>: <span class="s2">"string"</span>
        <span class="o">}</span>,
        <span class="s2">"F"</span>: <span class="o">{</span>
          <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
        <span class="o">}</span>,
        <span class="s2">"G"</span>: <span class="o">{</span>
          <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
        <span class="o">}</span>,
        <span class="s2">"H"</span>: <span class="o">{</span>
          <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
        <span class="o">}</span>
      <span class="o">}</span>,
      <span class="s2">"required"</span>: <span class="o">[</span>
        <span class="s2">"D"</span>,
        <span class="s2">"E"</span>,
        <span class="s2">"F"</span>,
        <span class="s2">"G"</span>,
        <span class="s2">"H"</span>
      <span class="o">]</span>
    <span class="o">}</span>,
    <span class="s2">"I"</span>: <span class="o">{</span>
      <span class="s2">"type"</span>: <span class="s2">"integer"</span>
    <span class="o">}</span>,
    <span class="s2">"J"</span>: <span class="o">{</span>
      <span class="s2">"type"</span>: <span class="s2">"boolean"</span>
    <span class="o">}</span>
  <span class="o">}</span>,
  <span class="s2">"required"</span>: <span class="o">[</span>
    <span class="s2">"A"</span>,
    <span class="s2">"B"</span>,
    <span class="s2">"C"</span>,
    <span class="s2">"I"</span>,
    <span class="s2">"J"</span>
  <span class="o">]</span>
<span class="o">}</span>

那么所有的API第一步就会通过json的结构验证。之后还会有业务对应的Value验证,会有业务组合以及数据库的验证。持续学习中

PS:
最后补一个如果报错的话出现的info,还是不错的。


1
2
3
4
5
6
7
8
9
10
11
12
Response body doesn<span class="s1">'t match expectation.
Expected: The content to match the given JSON schema.
error: object has missing required properties (["A","B"])
   level: "error"
   schema: {"loadingURI":"xxxxx.JSON&gt;#","pointer"}
   instance: {"pointer":"/result"}
   domain: "validation"
   keyword: "required"
   required: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N"]
   missing: ["A","C"]

</span>
声明:本文转载自 TesterHome 移动测试社区,作者为 TesterHome 移动测试社区,原文网址:https://testerhome.com/topics/4969

最后编辑于:2016/11/19作者: 归根落叶

关注微信公众号 – 聚合软件测试类精华

关注微信公众号 – 聚合软件测试类精华