Skip to content

GitHub API 封装测试 tests/utils/patchers/test_github.py ​

验证 objection.utils.patchers.github.Github 的 _call(带缓存)、get_latest_version、get_assets:HTTP 请求、状态码检查、JSON 解析、响应缓存与 404 异常。

📋 模块概览 ​

项目值
文件路径tests/utils/patchers/test_github.py
被测对象objection.utils.patchers.github.Github
用例数5
框架pytest + unittest + mock

🎯 测试意图 ​

  • 确认 _call 对 200 响应缓存结果(request_cache 长度 1),第二次同路径请求命中缓存返回首次结果。
  • 确认 get_latest_version 返回 tag_name。
  • 确认 get_assets 在 404 时抛异常,200 时返回 assets 列表。

🧪 用例清单 ​

用例行号验证点
test_makes_call_and_stores_result_in_cache32200 响应缓存且 cache 长度 1
test_makes_call_and_stores_result_in_cache_and_fetches_next_from_cache45第二次命中缓存返回首次结果
test_makes_call_and_gets_latest_version67返回 tag_name
test_makes_call_and_fails_to_get_assets79404 抛异常
test_makes_call_and_gets_assets90200 返回 assets 列表

⚙️ 测试手法 ​

每个用例以 @mock.patch('objection.utils.patchers.github.requests') 替换 requests,构造 mock.Mock() 设 status_code/.json.return_value。setUp 预置 self.mock_response(含 tag_name='10.6.9' 与 assets)。缓存用例先调用一次 _call('/test') 再改 mock_requests.get 返回值,第二次调用断言仍返回首次结果(证明缓存命中)。

关键代码 tests/utils/patchers/test_github.py:45:

python
@mock.patch('objection.utils.patchers.github.requests')
def test_makes_call_and_stores_result_in_cache_and_fetches_next_from_cache(self, mock_requests):
    ...
    self.github._call('/test')
    # entry is now stored in cache, update the next response object
    mock_response = mock.Mock()
    mock_response.status_code = 200
    mock_response.json.return_value = {'other'}
    mock_requests.get.return_value = mock_response
    result = self.github._call('/test')
    self.assertEqual(result, self.mock_response)

🔍 源码索引 ​

用例位置
test_makes_call_and_stores_result_in_cachetests/utils/patchers/test_github.py:32
test_makes_call_and_stores_result_in_cache_and_fetches_next_from_cachetests/utils/patchers/test_github.py:45
test_makes_call_and_gets_latest_versiontests/utils/patchers/test_github.py:67
test_makes_call_and_fails_to_get_assetstests/utils/patchers/test_github.py:79
test_makes_call_and_gets_assetstests/utils/patchers/test_github.py:90

🔗 相关文档 ​

基于 GPL-3.0-or-later 许可发布