在Python测试中使用HTTP Archive (HAR) 文件
项目描述
要将此请求发送到API后端并断言API响应与该响应匹配……
{
"log": {
"version": "1.2",
"entries": [
{
"request": {
"method": "POST",
"url": "mock://example.com/users/",
"headers" : [
{
"name": "Accept",
"value": "application/json"
}
],
"postData": {
"mimeType": "application/json",
"text" : {
"username": "foo_username",
"email": "foo@example.com",
"group": "479e75e6-755a-46f1-a949-d6fee3671b3c"
}
},
"comment" : "Test validation errors"
},
"response": {
"status": 201,
"statusText": "Created",
"headers" : [
{
"name": "Allow",
"value": "GET, POST, HEAD, OPTIONS"
},
{
"name": "Vary",
"value": "Accept, Cookie"
}
],
"content": {
"mimeType": "application/json",
"text": {
"username": "foo_username"
}
}
}
}
]
}
}
使用此测试
import datetime
# Import the appropriate backend for your framework.
# Currently there are backends for the `requests` library...
from test_har.requests_har as test_har
# and for the Django ReST Framework.
# from test_har.django_rest_har as test_har
# Contributions for other frameworks welcome!
from .. import models
class MyAPITest(test_har.HARTestCase):
"""
Specify the HAR file to use by default.
"""
# Path to file relative to this test file
# Set to `None` to skip automatic HAR parsing and set up
example_har = 'example.har.json'
def test_my_response(self):
"""
Write your test as you would.
"""
# For convenience, `self.example` is the parsed JSON in
# `self.example_har`, `self.entry` is the first request/response
# entry from `log/entries/0` in the HAR., `self.headers` is a
# dictionary of the response headers for that entry, and
# `self.content` is the response body content from that entry.
# Make any changes to the test fixture necessary for the test such
# as creating related objects before a POST that requires them.
group = models.Group(
name='foo_group',
uuid=self.entry["request"]["postData"]["text"]["group"])
# Make any changes to the HAR necessary for the assertions to work
# before sending the requests
self.content["some_dynamic_value"] = models.my_dynamic_function()
# Send the HAR requests, assert responses match the HAR, and return
# the responses. Currently, assertions are made against the
# response: `Status` code, `Status` reason text, `Content-Type`
# MIME type, other headers in the HAR, and the response body
# content. If the response MIME type is a JSON type,
# then assertions will be made against each top-level key
# individually and ignore any key in the response not included in
# the HAR.
now = datetime.datetime.now()
responses = self.assertHAR(self.example)
# Make any other assertions, or do anything else you'd like to do,
# with the responses.
self.assertAlmostEqual(
datetime.strptime(response[0].json()['created'], format='...'),
now, delta=datetime.timedelta(seconds=1),
msg='Wrong automatic creation time')
def test_my_other_response(self):
"""
Test a different HAR file.
"""
# Replace `self.example` and the other convenience attributes with
# the content form another HAR file
self.setUpHAR('other.har.json')
responses = self.assertHAR(self.example)
...
为什么?
编写API后端的测试通常需要在测试中包含大量的重复代码来构建内容,要么是POST到API,要么是用于断言的预期响应内容。例如,构建一个Python字典来表示发送到或从API返回的JSON。同样,测试返回的内容通常需要许多详细的断言,以便充分覆盖API应该如何表现。
虽然编写重复的测试代码很繁琐,但更重要的是,它不太易读,使得确定测试旨在覆盖的行为变得不必要地困难。理想情况下,应该能够以与API使用的格式更接近的格式描述API的预期行为。同样理想的是,应该能够清楚地阅读相关的请求和响应。
浏览器用于记录浏览器会话的基于JSON的HTTP Archive (HAR) 格式为我们提供了这样的格式,特别是对于基于JSON的API。 test_har 包提供了使用HAR文件驱动测试和对响应进行更常见断言的支持,同时仍然允许开发者像以前一样继续执行HAR文件无法覆盖的任何操作。
项目详情
下载文件
下载适用于您平台的应用程序文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
test-har-0.2.dev0.tar.gz (12.6 kB 查看哈希值)
构建分布
test_har-0.2.dev0-py3-none-any.whl (17.0 kB 查看哈希值)
关闭
test-har-0.2.dev0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a771f8aac1c9e9989f68d76c53a952b0cf8960531de853c9b25fca73c82aa7eb |
|
MD5 | 68c40129c9143f1c77682e21af16d50c |
|
BLAKE2b-256 | 20615b56c49f4f64a759c15a79e28a4547a2a468edb17988d1c53719dec0f4bb |
关闭
test_har-0.2.dev0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8cfe5437d7cd73d91fda8f46a8e5a4755b96b531753382ddc7a73fdd603df97d |
|
MD5 | 58cde148e122384210ba304431d2f260 |
|
BLAKE2b-256 | 13475bb98fa57c8f0221d30e133cd26ec13e28aa9e5b32512839f745ff334627 |