gpt4 book ai didi

Python 单元测试模拟 API key

转载 作者:行者123 更新时间:2023-12-01 04:43:06 24 4
gpt4 key购买 nike

我正在为 client.py 的 Client 类编写单元测试,该类查询 API。每个测试都使用 c = client.Client("apikey") 实例化客户端。一次运行一个测试工作正常,但运行所有测试(例如使用 py.test)时,我会收到 401:“异常:响应 401:未经授权的访问。请求必须包含有效的 api key 。 ”

我有一个有效的 API key ,但不应将其包含在单元测试中。我希望能解释一下为什么 "apikey" 仅适用于一个查询。更具体地说,如何模拟对 API 的调用?下面是一个单元测试示例:

def testGetContextReturnFields(self):
c = client.Client("apikey")
contexts = c.getContext("foo")

assert(isinstance(contexts[0]["context_label"], str))
assert(contexts[0]["context_id"] == 0)

最佳答案

将 API 调用的测试和 Client.getContext() 方法的测试分开。要显式测试 API 调用,请修补请求对象...

import client
import httpretty
import requests
from mock import Mock, patch
...
def testGetQueryToAPI(self):
"""
Tests the client can send a 'GET' query to the API, asserting we receive
an HTTP status code reflecting successful operation.
"""
# Arrange: patch the request in client.Client._queryAPI().
with patch.object(requests, 'get') as mock_get:
mock_get.return_value = mock_response = Mock()
mock_response.status_code = 200

# Act:
c = client.Client()
response = c._queryAPI("path", 'GET', {}, None, {})

# Assert:
self.assertEqual(response.status_code, 200)

# Repeat the same test for 'POST' queries.

为了测试 getContext(),使用 httpretty 模拟 HTTP...

@httpretty.activate
def testGetContextReturnFields(self):
"""
Tests client.getContext() for a sample term.
Asserts the returned object contains the corrcet fields and have contents as
expected.
"""
# Arrange: mock JSON response from API, mock out the API endpoint we expect
# to be called.
mockResponseString = getMockApiData("context_foo.json")
httpretty.register_uri(httpretty.GET,
"http://this.is.the.url/query",
body=mockResponseString,
content_type="application/json")

# Act: create the client object we'll be testing.
c = client.Client()
contexts = c.getContext("foo")

# Assert: check the result object.
self.assertTrue(isinstance(contexts, list),
"Returned object is not of type list as expected.")
self.assertTrue(("context_label" and "context_id") in contexts[0],
"Data structure returned by getContext() does not contain"
" the required fields.")
self.assertTrue(isinstance(contexts[0]["context_label"], str),
"The \'context_label\' field is not of type string.")
self.assertEqual(contexts[0]["context_id"], 0,
"The top context does not have ID of zero.")

关于Python 单元测试模拟 API key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com