The use of mock in unit testing of python technology stack

1. What is a mock?

Mock test is a test method that uses a virtual object to create a test method for some objects that are not easy to construct or obtain during the test process.

2. The role of mock

In particular, the upstream and downstream unfinished processes in the development process make it impossible to test at present, and some specific objects need to be virtualized for testing.

unittest is a built-in unit test library in python. When doing interface testing, if the developed interface has not been developed, if we want to test the joint debugging of the interface, we can't wait. At this time, we can use unittest.mock to simulate the return of the interface and perform Interface testing.

Three, take a chestnut

Example: 1. Add two new interfaces, login and obtain personal information, which are developed by A and B respectively. 2. A's login interface has not been developed yet, and B's interface for obtaining personal information has been developed. 3. The known login interface returns 3 states: successful login, failed login, and abnormal login.

Scene source code: case.py

If you want to learn interface automation testing, here I recommend a set of videos for you. This video can be said to be the number one interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and use. Technical exchanges of various masters: 798478386      

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337

# -*-coding:utf-8 -*-

def login():
    # 登录接口,尚未开发完成
    # 登录成功返回:{"result": "success", "message": "登录成功"}
    # 登录失败返回:{"result": "fail", "message":"账号或密码错误"}
    # 登录异常返回:{"code": "404", "message": "找不到页面"}
    # message返回失败原因

    pass


def get_user_info():
    # 根据登录的结果success or fail,判断跳转到对应页面

    result = login()
    print(result)
    try:
        if result["result"] == "success":
            return "登录成功"
        elif result["result"] == "fail":
            return "登录失败"
        else:
            return "未知失败"
    except:
        return "服务端异常"

Unit test case design:

# -*-coding:utf-8 -*-
from unittest import mock
import unittest
import case


class TestLogin(unittest.TestCase):
    # 单元测试用例

    def test_login_success(self):
        # 测试登录成功场景
        # mock一个支付成功的数据

        case.login = mock.Mock(return_value={"result": "success", "message": "登录成功"})
        # 根据支付结果测试页面跳转
        statues = case.get_user_info()
        self.assertEqual(statues, "登录成功")

    def test_login_fail(self):
        # 测试登录失败场景
        # mock一个登录失败的数据

        case.login = mock.Mock(return_value={"result": "fail", "message": "账号或密码错误"})
        # 根据登录结果测试页面跳转
        statues = case.get_user_info()
        self.assertEqual(statues, "登录失败")

    def test_login_fail2(self):
        # 测试登录失败场景
        # mock一个登录失败的数据

        case.login = mock.Mock(return_value={"result": "", "message": "账号或者密码为空"})
        # 根据登录结果测试页面跳转
        statues = case.get_user_info()
        self.assertEqual(statues, "未知失败")

    def test_login_error(self):
        # 测试登录异常场景
        # mock一个登录异常的数据

        case.login = mock.Mock(return_value={"code": "404", "message": "找不到页面"})
        # 根据登录结果测试页面跳转
        statues = case.get_user_info()
        self.assertEqual(statues, "服务端异常")


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

Execute the unittest unit test case:

 

Guess you like

Origin blog.csdn.net/caixiangting/article/details/132190588