3

在搜索甚至阅读有关应用程序引擎单元测试的文档后,我仍然无法使其在我的项目中运行。

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

我的测试类看起来像这样;

from main import CloudDocument   # My model
class TestCloudDocument(DataStoreTestCase, unittest.TestCase):
    def test_find_document(self):
        self.assertEqual(CloudDocument.all().count(), 0)

当我运行上述测试时,它返回 True 但我的数据存储区有记录,我预计此测试会失败。

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

加思

4

3 回答 3

3

单元测试在独立的环境中运行,具有自己的本地数据存储。只有您在测试或setUp函数中添加的记录才会出现。

于 2012-11-05T07:59:09.210 回答
1

无耻插件:如果你想从 setUp 方法中加载很多对象,你可以使用appengine-fixture-loader

于 2014-12-09T18:24:04.630 回答
0

我建议使用 ext.testbed(Python 的本地单元测试)而不是 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.
    """

http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/testbed/__init__.py#431

于 2012-11-05T18:41:03.170 回答