你知道吗? 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)