python接口测试之mock

Mock小丑

你知道吗? Python3里自带的mock非常好用,它可以帮助你摆脱底层某些依赖,将测试的重点聚焦在主逻辑上

本Demo的目录结构:

Demo
|-- bin
|   | -- __init__.py
|   | -- example.py
|-- tests
|   | -- __init__.py
|   | -- test_example.py

1. 被测接口实现

# -*- coding: utf-8 -*-
"""
@author: TerryJay 
@time: 2020/4/11 17:38
"""

from requests import get


# 底层API:获取Github的公共时间线
def get_events():
	responses = get('https://api.github.com/events')

	return responses


# 被测函数:判断id是否为奇数
def is_old_number(res):
	events_id = res.get("id")
	if int(events_id) % 2:
		return "NO"
	else:
		return "YES"

2. 使用unittest库的mock.patch替换掉底层API,使被测函数脱离依赖

# -*- coding: utf-8 -*-
"""
@author: TerryJay 
@time: 2020/4/11 17:38
"""

import unittest
from unittest.mock import patch
import bin.example


class Test_set(unittest.TestCase):
	# 自行定义get_events的返回结果,可以看到id是一个偶数,那么预期结果应该是YES
	fixed_result = {"id": "12021751522", "type": "PullRequestEvent"}

	@patch('bin.example.get', return_value=fixed_result)
	def test_is_old_number(self, mock_get):
		# get_events函数里使用了get方法,它会被patch替换掉
		time_line = bin.example.get_events()
		result = bin.example.is_old_number(time_line)
		self.assertTrue(mock_get.called)
		self.assertEqual(result, "YES")


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

3. 运行结果

因为我们定义好的值是id=12021751522,明显看出是个偶数,那么我们的预期是is_old_number函数应返回YES

..

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

如果断言设置时写错了, 将self.assertEqual(result, "YES")改为self.assertEqual(result, "NO")
再次运行就会抛出异常,这时发现是我们测试人员的预期不正确,注意断言的预期一定要符合需求。

Failure
Traceback (most recent call last):
  File "I:\Anaconda3\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "I:\Anaconda3\lib\unittest\case.py", line 605, in run
    testMethod()
  File "I:\Anaconda3\lib\unittest\mock.py", line 1179, in patched
    return func(*args, **keywargs)
  File "H:\Panda\bin\test_example.py", line 24, in test_is_old_number
    self.assertEqual(result, "NO")
  File "I:\Anaconda3\lib\unittest\case.py", line 829, in assertEqual
    assertion_func(first, second, msg=msg)
  File "I:\Anaconda3\lib\unittest\case.py", line 1202, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "I:\Anaconda3\lib\unittest\case.py", line 670, in fail
    raise self.failureException(msg)
AssertionError: 'YES' != 'NO'
- YES
+ NO



F

======================================================================
FAIL: test_is_old_number (test_example.Test_set)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "I:\Anaconda3\lib\unittest\mock.py", line 1179, in patched
    return func(*args, **keywargs)
  File "H:\Panda\bin\test_example.py", line 24, in test_is_old_number
    self.assertEqual(result, "NO")
AssertionError: 'YES' != 'NO'
- YES
+ NO


----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (failures=1)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木法星人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值