James D. Bloom的出色MockServer的友好Python客户端
项目描述
MockServer Python客户端
James D. Bloom的出色MockServer的Python客户端:http://www.mock-server.com/
哲学
测试应该是可读的,Mock Server已经非常完整且可定制。这个库试图使用python的kwargs来保持简单和直观,以避免大声明。
我们认为,如果期望足够简单,模拟应该能够适应一行120个字符的代码:)
client.stub(request(method="GET", path="/auth"), response(code=401, body="unauthorized"))
安装
警告:这是一个非常早期的版本,API可能将发生变化
从源码
git clone https://github.com/internap/python-mockserver-friendly-client.git
cd python-mockserver-friendly-client
python setup.py install
从PyPi直接
pip install mockserver-friendly-client
先决条件
您需要一个正在运行的MockServer,请参阅http://www.mock-server.com/mock_server/getting_started.html#start_mockserver
此项目的测试使用以下Docker镜像:https://github.com/internap/python-mockserver-friendly-client/blob/master/docker-compose.yml
用法
Mock Server API并未全部覆盖。我们只实现了所需的功能,如果您需要尚未实现的功能,您可以提出问题并/或做出贡献
存根
(当你测试你的代码如何使用另一个组件的数据时)
from mockserver import MockServerClient, request, response
client = MockServerClient("http://localhost:1080")
client.stub(
request(method="GET", path="/that/thing", querystring={"is": "good"}, headers={"so": "good"}),
response(code=418, body="i'm a teapot", headers={"hi": "haa"})
)
- 您还可以添加一个
times(N)
作为第三个参数来限制这个存根可以调用的次数,默认为无限 - 所有参数始终是可选的。如果没有指定,则匹配所有内容。
期望
(当调用另一个组件IS您正在测试的组件时使用)
使用expect
将记住请求,并在调用verify_expectations
时验证它。
import json
from mockserver import MockServerClient, request, response, times
client = MockServerClient("http://localhost:1080")
client.expect(
request(method="POST", path="/postme", body=json.dumps({"some": "json"})),
response(code=204, body=json.dumps({"return": "something"}), headers={"Content-Type": "application/json"}),
times(1)
)
client.verify_expectations() # AssertionError !
times(N)
参数对于expect是必填的。
重置
(因为测试不应该互相影响)
from mockserver import MockServerClient
client = MockServerClient("http://localhost:1080")
client.reset()
自定义和快捷键
此客户端消耗Mock Server的REST API: https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi
这里有一些已经准备好的快捷键,未来可能还有更多。
模拟表单提交
from mockserver import MockServerClient, request, response, form
client = MockServerClient("http://localhost:1080")
client.stub(
request(method="POST", body=form({"user": "foo", "pass": "bar"})),
response(code=201)
)
目前它接受1级dict
。对于数组模拟,使用{"key[index]": "value"}
返回JSON
from mockserver import MockServerClient, request, json_response
client = MockServerClient("http://localhost:1080")
client.stub(
request(path="/stuff"),
json_response(code=200, body={"full": "json", "structure": ["with", "stuff", 1]})
)
这会自动将主体转换为json,并将application/json
内容类型添加到头部。
期望JSON请求
from mockserver import MockServerClient, request, json_equals
client = MockServerClient("http://localhost:1080")
client.expect(
request(body=json_equals({"key": "value"})),
response(),
times(1)
)
这仅匹配提供json主体的请求。JSON对话会自动进行。如果您只想匹配给定JSON的部分,可以使用json_contains
提供匹配的部分
from mockserver import MockServerClient, request, json_contains
client = MockServerClient("http://localhost:1080")
client.expect(
request(body=json_contains({"key": "value"})),
response(),
times(1)
)
例如,这将匹配{"key": "value"}
或{"key": "value", "another": "key"}
验证请求
from mockserver import MockServerClient, request, times
client = MockServerClient("http://localhost:1080")
client.verify(
request(path="/some_path", querystring({"key": "value"})),
times(1)
)
验证将检查MockServer上的请求,如果请求未找到,将引发AssertionError
。
更多文档
目前尚无官方文档,但您可以将测试视为一种文档类型,它们相当明确且易于遵循,有助于阐明每个功能的目的。
最佳实践
使您的测试设置/拆卸像这样可能是个好主意。
class ServerMockingTestBase(...):
def setUp(self):
super(ServerMockingTestBase, self).setUp()
self.client = MockServerClient(MOCK_SERVER_URL)
self.client.reset()
def tearDown(self):
super(ServerMockingTestBase, self).tearDown()
self.client.verify_expectations()
故障排除
检查MockServer的日志是首先应该去的地方。如果您没有看到任何日志,请尝试另一个LOG_LEVEL,例如INFO。
如果问题在代码中,请打开一个问题:)
贡献
表单可能不是最终的,我们很乐意听听您对客户端的看法!
请随意提出问题并发送一些pull request,我们将很高兴查看它们!
确保所有新代码都已测试,当前测试针对MockServer容器运行。
运行测试
您可以使用https://pypi.python.org/pypi/tox轻松运行测试
tox -e py34
您还可以直接调用test-runner.sh
,您需要安装https://pypi.python.org/pypi/nose
您还可以启动容器
docker-compose up-d
并在您最喜欢的IDE中运行测试:)
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。