3

App Engineの単体テストに関するドキュメントを検索して読んだ後でも、プロジェクトでそれを機能させることはできません。

タスクは単純です。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

gaetestbedの代わりにext.testbed(Pythonのローカルユニットテスト)を使用することをお勧めします。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 に答える