python的快照测试实用程序
snapshottest的Python项目详细描述
快照测试
快照测试是一种不用编写实际测试就可以测试api的方法。 案例。
- 快照是保存在文件中的API的单个状态。
- 您有一组api端点的快照。
- 添加新功能后,可以自动生成new 更新的api的快照。
安装
$ pip install snapshottest
与unittest/nose一起使用
fromsnapshottestimportTestCaseclassAPITestCase(TestCase):deftest_api_me(self):"""Testing the API for /me"""my_api_response=api.client.get('/me')self.assertMatchSnapshot(my_api_response)# Set custom snapshot name: `gpg_response`my_gpg_response=api.client.get('/me?gpg_key')self.assertMatchSnapshot(my_gpg_response,'gpg_response')
如果要自动更新快照,可以使用 nosetests --snapshot-update。
使用pytest
deftest_mything(snapshot):"""Testing the API for /me"""my_api_response=api.client.get('/me')snapshot.assert_match(my_api_response)# Set custom snapshot name: `gpg_response`my_gpg_response=api.client.get('/me?gpg_key')snapshot.assert_match(my_gpg_response,'gpg_response')
如果要自动更新快照,可以使用 --snapshot-update配置。
与django一起使用
添加到您的设置:
TEST_RUNNER='snapshottest.django.TestRunner'
创建快照测试:
fromsnapshottest.djangoimportTestCaseclassAPITestCase(TestCase):deftest_api_me(self):"""Testing the API for /me"""my_api_response=api.client.get('/me')self.assertMatchSnapshot(my_api_response)
如果要自动更新快照,可以使用 python manage.py test --snapshot-update。检查Django example。
贡献
在克隆此repo并为snapshottest配置virtualenv之后 (可选,但强烈建议),确保依赖项由 跑步:
make install
开发完成后,可以通过运行:
make lint # and make test
如果更改此README.md,则需要安装pandoc来更新其README.rst对应项(由pypi使用)。 可以通过运行:
make README.rst
注释
这个包的灵感来自jest snapshot testing。
使用此软件包的原因
Most of this content is taken from the Jest snapshot blogpost.
我们想让它尽可能无摩擦地编写好的测试 是有用的。我们注意到当工程师 准备好使用工具后,他们会编写更多的测试 产生稳定健康的代码库。
然而,工程师通常花更多的时间编写测试,而不是 组件本身。结果很多人停止编写测试 最终导致不稳定。
移动应用程序的典型快照测试用例呈现一个ui组件, 截图,然后将其与存储的参考图像进行比较 在测试旁边。如果两个图像不匹配,则测试将失败: 更改是意外的,或者需要更新屏幕截图 到新版本的ui组件。
使用SnapshotTest进行快照测试
在测试api时也可以采用类似的方法。 而不是呈现图形用户界面,这将需要构建 整个应用程序,您可以使用测试呈现程序快速生成 API响应的可序列化值。