如何在App Engine中对REST方法进行单元测试?

时间:2010-12-06 04:19:03

标签: unit-testing google-app-engine rest google-cloud-datastore

我没有找到一种方法来自动测试应用程序的REST方法,同时使用setUp和tearDown方法来保留每个测试的唯一性。

gaetestbed在测试之间为我提供了一个干净的数据存储区。 httplib2允许我轻松调用REST方法并解析其响应;但为了做到这一点,我的应用程序的本地实例必须在端口8080上运行并调用每个测试。这违背了类似gaetestbed的刷新数据存储区的目的,因为数据在调用之间保留。

我可以在 setUp tearDown 方法中启动和停止GAE服务器,但这看起来很浪费且耗时。有没有更好的办法?

2 个答案:

答案 0 :(得分:3)

使用gaetestbed或您自己的单元测试代码(如this),只需使用mocked / fake环境字典直接实例化您的处理程序类,然后调用方法(initialize()和get()/ post()/在webapp的情况下直接使用。

答案 1 :(得分:0)

使用REST方法进行功能测试时,我们最终编写了帮助调用来清除内部缓存并强制我们的引擎与数据库同步。

我没有使用gaetestbed,但我认为你可以在测试之间flush the datastore

"Use case: develop and test locally with real data
Restore to: your local development server.
Once you have restored the data to your local development server, I would highly suggest that you take a backup of your datastore.
You can find your datastore files in a temporary folder on your local machine (e.g., on my Mac, it's /var/folders/bz/bzDU030xHXK-jKYLMXnTzk+++TI/-Tmp-/). To find the folder on your own machine, flush the datastore (./manage.py flush) and you will see the path to your datastore folder printed in the resulting output."