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