Interface Testing & Interface Test Automation

With the requirements of agile development and rapid iteration, automated testing has become standard in the software development process. However, UI automation, due to frequent changes in interface requirements and the stability of element identification, has a low input-output ratio and is difficult to keep up with the development cycle requirements; unit testing focuses more on the method/function itself, and it is difficult to analyze the business logic from the business logic level. To achieve the quality confidence requirements of integration testing, the interface testing for Service is very suitable for automated testing.

Interface: Java Interface, HTTP interface (rest), RPC (dubbo), etc.

1. Interface automated testing framework – Java stack

  • Persistence layer (context data, test data driven): JdbcTemplate (bare SQL), MyBatis (reused development code), POI (deprecated)
  • Logic control layer (logic verification): HttpClient, FastJson, TestNG, Json Schema
  • Presentation layer (test report): ExtentReporter
  • Mock: Moco, SpringBoot (including logic), Apollo (hot update)
  • Others: Maven, Gitlab, Jenkins

2. Advantages of interface testing

  1. It is easier to implement continuous integration and improve system robustness;
  2. Automated testing is cost-effective and more stable than UI automation;
  3. Large-scale systems are more complex and have more and more modules between systems (microservices), which are divided by interfaces and tested by modules;
  4. Bugs are easy to locate;
  5. Reduce R&D costs (not reduced) and improve efficiency (regression testing).

PS: Interface automation does not have to be done in one step. It can be done in small steps, gradually improved, and continued.

3. Interface automated testing process

The interface testing and functional testing processes are basically the same, using the V model.

Requirements discussion
        Requirements review
                Scene design
                Use case design
        Data/Tool Preparation
test execution

Requirements stage->Project establishment, product design, requirements documents (test intervention)

R&D stage->UI design, front-end development, back-end development, test design, test development

Testing phase->Environment setup, test execution (function, automation, performance), bug fixing, test report

Project goes online -> Online regression testing, online reporting (follow-up, promotion), added monitoring (interface survival monitoring, port number, heartbeat monitoring)

4. Interface testing benefits

  1. From serial to parallel
  2. Change testing work from late stage to early stage
  3. Test execution time reduced
  4. Test data sharing can continuously accumulate and reduce the missed test rate.
  5. Test reports can be managed, traced, and queried
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

5. Interface test scope

  1. Functional testing (equivalence class division method, boundary value analysis method, error inference method, cause and effect diagram method, decision table driven method, orthogonal test method, function diagram method, scenario method);
  2. Abnormality testing (data abnormality-null, “”, data type, environmental abnormality-load balancing architecture, hot and cold backup);
  3. Performance testing – narrow sense (load testing, stress testing, concurrency testing, stability testing/reliability testing);
  4. Safety test.

6. Manual interface testing tools

  • Manual: PostMan, HTTPRequest (FireFox plug-in), Fiddler
  • Semi-automated: JMeter (result statistics are not perfect)
  • Others: HttpWatch, developer tools, Wireshark

7. Interface automated testing plan

A. Robot Framework

Advantages: Keyword driven, supports test log and test report generation, supports database operations.

Disadvantages: Interface use cases are not concise; specific syntax needs to be mastered.

** Settings ***
Library RequestsLibrary
Library Collections

***Test Cases***
test_get_event_list # Query press conference (GET request)
    ${payload}= Create Dictionary eid=1
    Create Session event http://127.0.0.1:8000/api
    ${r}= Get Request event /get_event_list/ params=${payload}
    Should Be Equal As Strings ${r.status_code} 200
    log ${r.json()}
    ${dict} Set variable ${r.json()}
    #Assert result
    ${msg} Get From Dictionary ${dict} message
    Should Be Equal ${msg} success
    ${sta} Get From Dictionary ${dict} status
    ${status} Evaluate int(200)
    Should Be Equal ${sta} ${status}

B. JMeter

Advantages: Supports parameterization. If you do subsequent interface performance testing, you can consider it.

Disadvantages: Creating interface use cases is not efficient.

C, HttpRunner

Advantages: Based on YAML/JSON format, focusing on the interface itself; the interface is simple to write, test reports are generated, and the interface is recorded.

Disadvantages: There is no editor for syntax verification (error-prone), and the interface initialization data needs to be processed separately.

[
  {
    "config": {
      "name": "testcase description",
      "variables": [],
      "request": {
        "base_url": "http://127.0.0.1:5000",
        "headers": {
          "User-Agent": "python-requests/2.18.4"
        }
      }
    }
  },
  {
    "test": {
      "name": "test case name",
      "request": {
        "url": "/api/get-token",
        "headers": {
          "device_sn": "FwgRiO7CNA50DSU",
          "user_agent": "iOS/10.3",
          "os_platform": "ios",
          "app_version": "2.8.6",
          "Content-Type": "application/json"
        },
        "method": "POST",
        "date": {"sign": "958a05393efef0ac7c0fb80a7eac45e24fd40c27"}
      },
      "validate": [
        {"eq": ["status_code", 200]},
        {"eq": ["headers.Content-Type", "application/json"]},
        {"eq": ["content.success", true]},
        {"eq": ["content.token", "baNLX1zhFYP11Seb"]}
      ]
    }
  }
]

TIPS: Get started quickly – HttpRunner V2.x Chinese documentation

D.gauge

BDD behavior-driven testing framework.

advantage:

  • Behavior files and script files are separated, essentially achieving data drive;
  • Powerful and flexible, essentially using Python to write interface use cases;
  • Automatically generate test reports;
  • VSCode has plug-in support.

Disadvantages: Requires understanding of BDD.

# Behavior description file
# test post request

* post "http://httpbin.org/post" interface
     |key |status_code|
     |------|-----------|
     |value1|200 |
     |value2|200 |
     |value3|200 |

# test script
@step("post <url> interface <table>")
def test_get_request(url, table):
    values = []
    status_codes = []
    for word in table.get_column_values_with_name("key"):
        values.append(word)
    for word in table.get_column_values_with_name("status_code"):
        status_codes.append(word)
    for i in range(len(values)):
        r = requests.post(url, data={"key": values[i]})
        result = r.json()
        assert r.status_code == int(status_codes[i])

TIPS: Writing Specifications

E. unittest + requests + HTMLRunner

Advantages: Flexible and powerful, layered testing, data-driven, test reporting, CI.

Disadvantage: code.

# Data file
{
    "test_case1": {
        "key": "value1",
        "status_code": 200
    },
    "test_case2": {
        "key": "value2",
        "status_code": 200
    },
    "test_case3": {
        "key": "value3",
        "status_code": 200
    },
    "test_case4": {
        "key": "value4",
        "status_code": 200
    }
}

# test case
import requests
import unittest
from ddt import ddt, file_data


@ddtclass InterfaceTest(unittest.TestCase):

    def setUp(self):
        self.url = "http://httpbin.org/post"

    def tearDown(self):
        print(self.result)

    @file_data("./data/test_data_dict.json")
    def test_post_request(self, key, status_code):
        r = requests.post(self.url, data={"key": key})
        self.result = r.json()
        self.assertEqual(r.status_code, status_code)

The mainstream one is not necessarily the best, the one that is most suitable is choosing the right technology stack and cost/ROI (input-output ratio).

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.