Python 如何在谷歌应用程序引擎中进行单元测试?

Python 如何在谷歌应用程序引擎中进行单元测试?,python,google-app-engine,Python,Google App Engine,在搜索甚至阅读了关于AppEngine单元测试的文档之后,我仍然无法在我的项目中使用它 任务很简单,我在main.py中有我的模型和请求处理程序。 我已经安装了它,并将其包含在我的测试类中 我的测试课是这样的 from main import CloudDocument # My model class TestCloudDocument(DataStoreTestCase, unittest.TestCase): def test_find_document(self):

在搜索甚至阅读了关于AppEngine单元测试的文档之后,我仍然无法在我的项目中使用它

任务很简单,我在main.py中有我的模型和请求处理程序。 我已经安装了它,并将其包含在我的测试类中

我的测试课是这样的

from main import CloudDocument   # My model
class TestCloudDocument(DataStoreTestCase, unittest.TestCase):
    def test_find_document(self):
        self.assertEqual(CloudDocument.all().count(), 0)
当我运行上面的测试时,它返回True,但我的数据存储有记录,我本以为这个测试会失败

测试类似乎没有看到我的应用程序的数据存储。我怎样才能做到 查看并访问我的本地数据存储


Gath

单元测试在一个隔离的环境中运行,有自己的本地数据存储。只有添加到测试或
设置
函数中的记录才会出现。

我建议使用ext.testbed()而不是gaetestbed。正如JJ(gaetestbed的维护者)所指出的,它被ext.testbed所取代

如果您这样做并且希望测试现有的和填充的数据存储,那么可以使用

 def init_datastore_v3_stub(self, enable=True, datastore_file=None,
                             use_sqlite=False, **stub_kw_args):
    """Enable the datastore stub.

    The 'datastore_file' argument can be the path to an existing
    datastore file, or None (default) to use an in-memory datastore
    that is initially empty.  If you use the sqlite stub and have
    'datastore_file' defined, changes you apply in a test will be
    written to the file.  If you use the default datastore stub,
    changes are _not_ saved to disk unless you set save_changes=True.

    Note that you can only access those entities of the datastore file
    which have the same application ID associated with them as the
    test run. You can change the application ID for a test with
    setup_env().

    Args:
      enable: True if the fake service should be enabled, False if real
        service should be disabled.
      datastore_file: Filename of a dev_appserver datastore file.
      use_sqlite: True to use the Sqlite stub, False (default) for file stub.
      stub_kw_args: Keyword arguments passed on to the service stub.
    """

无耻的插件:如果您想从setUp方法中加载很多对象,您可以下载包

这不是测试点。在测试的
设置
方法中,您可以将自己的测试数据放入为测试创建的数据存储存根中。