python请求的vcr模拟
betamax的Python项目详细描述
贝塔马克
betamax是对请求的模拟。这将发出模拟请求 容易多了。它在travis ci上进行测试。
用更幽默的方式说:"betamax记录了你的http交互,所以nsa 不必。"
示例使用
frombetamaximportBetamaxfromrequestsimportSessionfromunittestimportTestCasewithBetamax.configure()asconfig:config.cassette_library_dir='tests/fixtures/cassettes'classTestGitHubAPI(TestCase):defsetUp(self):self.session=Session()self.headers.update(...)# Set the cassette in a line other than the context declarationdeftest_user(self):withBetamax(self.session)asvcr:vcr.use_cassette('user')resp=self.session.get('https://api.github.com/user',auth=('user','pass'))assertresp.json()['login']isnotNone# Set the cassette in line with the context declarationdeftest_repo(self):withBetamax(self.session).use_cassette('repo'):resp=self.session.get('https://api.github.com/repos/sigmavirus24/github3.py')assertresp.json()['owner']!={}
它还能做什么?
如果您不熟悉VCR,您可能需要更好的解释 betamax可以。
betamax会拦截您提出的每个请求并尝试查找匹配的 已被拦截并记录的请求。有两件事可以 发生:
- 如果有匹配的请求,它将返回 与之相关。
- 如果存在不匹配的请求并且允许记录新的 响应,它将发出请求,记录响应并返回 响应。
记录的请求和相应的响应-也称为交互- 存储在称为磁带的文件中。(示例磁带可以在 文档的"示例"部分的 目录 盒式磁带称为您的磁带库,或您的盒式磁带库。
VCR盒式磁带兼容性
Betamax可以在这个时间点使用任何VCR录制的磁带。唯一 警告是,python请求在每个响应上返回一个url。VCR不 现在就把它放在磁带里,不过我们会的。任何录像机录制的磁带 很遗憾,回放响应将没有响应的url属性 已经归还了。这是一个小烦恼,但不是什么可以 固定. < /P>
贡献
您可以在waffle.io上查看项目板,了解 每个问题都是。