Encapsulation of test functions, test classes/test methods for python+pytest interface automation

Table of contents

foreword

General rules for test case encapsulation

Encapsulation of test functions

Encapsulation of test classes/methods

sample code

Summarize


foreword

In the python+pytest interface automation series, our previous articles basically did not encapsulate the code, but in the actual writing of automated test scripts, we all need to encapsulate the test code so that it can be recognized and executed by the test framework.

For example, the request code for a single interface is as follows:

import requests

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}

url = "https://www.cnblogs.com/lfr0123/"
res = requests.get(url=h_url, headers=headers)

Suppose we need to write the above code into a test case that can be executed by the test framework. It is obviously not enough to write the code in this way, and the following additions are required:

  • The code needs to be encapsulated into a test function or test class that the unit test framework (pytest or unittest) can recognize, otherwise it will not be recognized and executed.
  • It is necessary to add an assertion, that is, the comparison between the result and the expectation, so that the unit test framework can determine whether the execution result of the use case is passed. If the result == expectation, it means passed, otherwise it fails.

The encapsulation of functions and classes in python is not explained here. For pytest assertions, you can refer to the article pytest(5)-assertions. The purpose of this article is to let everyone understand how to encapsulate test codes in interface automation testing.

General rules for test case encapsulation

There are two types of encapsulation of test cases, test function and test class. The general rules of encapsulation are as follows:

  • A test function corresponds to a test case.
  • Multiple test methods can be defined in a test class, one test method corresponds to one test case, and the test class can be regarded as a set of test cases.
  • The name of the test function or test method in pytest must start with test, and the test class name must start with Test. For specific naming rules, please refer to my previous article pytest(3)-test naming rules.
  • For single-interface test verification, a single-interface test case contains only one interface request, that is, an interface request is encapsulated into a test function or test method.
  • For the test verification of scenarios (multiple interfaces), a scenario test case needs to request multiple interfaces, so multiple interface requests need to be encapsulated in the same test function or method.
  • Generally, the positive verification and abnormal verification of an interface are encapsulated into different methods and encapsulated in the same test class. For example, if you define a login test class, the request for correct username and password is encapsulated into one method (that is, a test case), and the request for correct username and wrong password is encapsulated into another method (that is, another test case).
  • It is also possible to encapsulate a function point or an interface case associated with a function in the same test class. For example, the interfaces involved in the personal center can be encapsulated in the same test class

Encapsulation of test functions

Generally speaking, a test function corresponds to a use case. The above code is written as a test case, the example is as follows:

It is emphasized that the name of the test function in pytest must start with test, such as test_get_home.

Encapsulation of test classes/methods

A test class is equivalent to a test case set, and each method in the class corresponds to a test case. Take the login interface as an example, encapsulate it into a test class, the example is as follows:

It is emphasized that the name of the test class in pytest needs to start with Test, such as TestLogin, and there cannot be an init method in the test class. The test method in the test class must start with test, such as test_login_normal.

sample code

In pytest, you can use the command line or use the code method, namely pytest.main() to execute the use case. For details, please refer to the article pytest(1)-Introduction.

The complete sample code is as follows:

# @time: 2022-03-24
# @author: 给你一页白纸
# 微信公众号:测试上分之路

import requests
import pytest
import json

def test_get_home():
    '''
    请求首页接口
    :return:
    '''
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
    }

    url = "https://www.cnblogs.com/lfr0123/"
    res = requests.get(url=url, headers=headers)
    # 断言,判断返回结果的code是否等于200,当然实际接口测试中一般返回结果中还会有别的字段需要断言
    assert res.status_code == 200

class TestLogin:
    '''
    登录接口校验
    '''
    url = "http://127.0.0.1:5000/login"
    headers = {"Content-Type": "application/json;charset=utf8"}

    def test_login_normal(self):
        '''正确用户名、正确密码登录'''
        data = {
            "username": "AndyLiu",
            "password": "123456"
        }
        res = requests.post(url=self.url, json=data, headers=self.headers)
        # 断言
        assert res.status_code == 200
        assert json.loads(res.text)["token"]

    def test_login_error(self):
        '''正确用户名、错误密码登录'''
        data = {
            "username": "AndyLiu",
            "password": "111111"
        }
        res = requests.post(url=self.url, json=data, headers=self.headers)
        # 断言
        assert res.status_code == 200
        assert not json.loads(res.text)["token"]

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

Summarize

  • The encapsulation of test functions, test classes/test methods, in fact, no matter what the unit test framework is, it follows the same method.
  • Each has its own requirements in terms of naming methods. For example, there are certain differences between the test naming methods in pytest and unittest.
  • Consider a function or method with its own assertion as a test case, then the test class is actually a test case set containing one or more test cases, and each method in the class corresponds to a test case.
  • Which test methods to put in a test class, in other words which test cases should be included in a test case set, can be determined according to the project's own situation or the tester's own ideas. The main purpose is to be clear.

 

Guess you like

Origin blog.csdn.net/MXB_1220/article/details/131731255