Automated test scripts for interfaces that are ready to go: the secret weapon to quickly improve test efficiency

Table of contents

Summary:

Environmental preparation

Write test cases

run test script

Summarize


 Summary: 

As a test engineer, we often need to conduct interface tests to verify whether the interface complies with the specification and is stable and reliable. However, manual testing will inevitably lead to omissions and human errors, so we need to write automated test scripts to realize interface automated testing.

This article will introduce how to use Python to write interface automation test scripts, and introduce some of the key points and deep thinking.

Environmental preparation

Before we start writing test scripts, we need to make sure the following software is installed:

  • Python development environment
  • requests library
  • unittest testing framework

After installing the above software, we can start writing test code.

Write test cases

A test case is a description of specific steps and data information used to test an interface. Usually, an interface has multiple test cases, which we can place in the same test class.

import unittest
import requests

class TestAPI(unittest.TestCase):

    def setUp(self):
        self.base_url = "http://127.0.0.1:8000/api/"
        self.headers = {"Content-Type": "application/json"}

    def test_login_success(self):
        url = self.base_url + "login"
        data = {"username": "admin", "passwor": "123456"}  #此次代码需要修改
        response = requests.post(url, headers=self.headers, json=data)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.json().get("token"))

    def test_login_fail(self):
        url = self.base_url + "login"
        data = {"username": "admin", "passwor": "wrong_password"} #此次代码需要修改
        response = requests.post(url, headers=self.headers, json=data)
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.json().get("code"), 1001)

    def tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main()

In the above example, we wrote two test cases test_login_successand test_login_failto test the normal and abnormal return of the login interface respectively. Among them, setUpand are the preconditions and postconditions of each test case tearDownrespectively , base_urland headersare the public data that each test case needs to use.

Here, we've used requeststhe library to send requests and get responses, and unittestthe framework for test assertions. test_login_successIn , we verify whether the status code of the response is 200, and determine whether the response body contains tokenthe field; and test_login_failin , we verify whether the status code and error code of the response meet expectations.

run test script

After writing the test code, we can execute the following command on the command line interface to run the test script:

python test_api.py

If all test cases pass, the output should be something like this:

Ran 2 tests in 0.052s OK

If the test case execution fails, the output information will tell you the reason and location of the failure.

Summarize

Through this article, we learned how to use Python to write interface automation test scripts, and introduced some of the key points and in-depth thinking. In actual work, we can adjust and expand according to project requirements and actual conditions, such as adding data-driven, logging, exception handling and other functions to make the test code more complete and robust.

Structural framework diagram of automated test learning steps:

 The editor also prepared some benefits:

 

Guess you like

Origin blog.csdn.net/Free355/article/details/130265443