从膨胀文件生成存根
swagger-stub的Python项目详细描述
虚张声势存根
swagger存根自动创建swagger rest api的存根。这个存根可以在pytest fixture中的任何地方用于单元测试。
除了模拟api之外,还可以模拟一些调用,并检查对api的每个调用。
示例用法
importpytestimportrequestsfromswagger_stubimportswagger_stub# This is the fixture of your stub# You only need to specify the path of the swagger file and the address# where you want to bind your stub.@pytest.fixturedeftest_stub():returnswagger_stub([('swagger.yaml','http://foo.com')]).next()# Then you can use this fixture anywhere you want like your API is really running.deftest_swagger_stub(test_stub):# Get a definition exampletest_stub.definitions['Foo']# Check a simple callresponse=requests.get('http://foo.com/v1/bar/')assertresponse.status_code==200assertresponse.json()=={'foo':'bar'}# Check that an invalid body cause an errorresponse=requests.post('http://foo.com/v1/bar/',data='invalid data')assertresponse.status_code==400# Mock a calltest_stub.add_mock_call('get','/test',{'mock':'call'})response=requests.get('http://foo.com/v1/test')assertresponse.json()=={'mock':'call'}# Set some side_effect like in the mock librarytest_stub.add_mock_side_effect('get','/iter',[{'test':'1'},{'test':'2'},{'test':'3'}])response=requests.get('http://foo.com/v1/iter')assertresponse.json()=={'test':'1'}response=requests.get('http://foo.com/v1/iter')assertresponse.json()=={'test':'2'}response=requests.get('http://foo.com/v1/iter')assertresponse.json()=={'test':'3'}# This side effect will raise a custom errortest_stub.add_mock_side_effect('get','/error',Exception)withpytest.raises(Exception):response=requests.get('http://foo.com/v1/error')
设置
进行安装或pip install swagger存根
0.2.1(2016-6-6)
- 修复重复的基路径。
0.2.0(2016-5-2)
- 添加对定义示例的访问权限。
0.1.1(2016-1-31)
- 把执照改成麻省理工学院。
0.1(2016-1-29)
- pypi上的第一个版本。