【问题标题】:How can I run local unit tests on models that use Google Cloud Storage on GAE (python)如何在 GAE (python) 上使用 Google Cloud Storage 的模型上运行本地单元测试
【发布时间】:2016-02-03 23:52:14
【问题描述】:

我正在尝试为调用存储在谷歌云存储上的文件的模型编写单元测试,但我还没有找到任何关于如何模拟 GCS 服务以进行单元测试的示例。

似乎应该有一个我可以使用的stub service,并且我在其中描述的testbed docs 中看到了对 gcs 的一些引用,但还没有确定一个我可以使用它的示例。

这是我拥有的模型的精简/示例版本:

import peewee
from google.appengine.api import app_identity
import cloudstorage

class Example(Model):
    uuid = peewee.CharField(default=uuid4)
    some_property = peewee.CharField()

    @property
    def raw_file_one(self):
        bucket = app_identity.get_default_gcs_bucket_name()
        filename = '/{0}/repo_one/{1}'.format(bucket, self.uuid)
        with cloudstorage.open(filename, 'r') as f:
            return f.read()

    def store_raw_file(self, raw_file_one):
        bucket = app_identity.get_default_gcs_bucket_name()

        filename = '/{0}/stat_doms/{1}'.format(bucket, self.uuid)
        with cloudstorage.open(filename, 'w') as f:
            f.write(raw_file_one)

我将构建测试用例:

import unittest

from google.appengine.ext import testbed    

class TestCase(unittest.TestCase):
    def run(self, *args, **kwargs):
        self.stub_google_services()
        result = super(TestCase, self).run(*args, **kwargs)
        self.unstub_google_services()
        return result

    def stub_google_services(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_all_stubs()

    def unstub_google_services(self):
        self.testbed.deactivate()

进入模块测试,例如:

from application.tests.testcase import TestCase
from application.models import Example

class ExampleTest(TestCase):
    def test_store_raw_file(self):
    ...
    [assert something]

我想我会做类似blobstore = self.testbed.get_stub('blobstore') 的事情来创建一个我可以执行测试的服务(例如blobstore.CreateBlob(blob_key, image))——但我在测试平台参考文档中没有看到 GCS 服务。

关于如何使用 GCS 实现单元测试的想法?

【问题讨论】:

    标签: google-app-engine google-cloud-storage


    【解决方案1】:

    我想你正在寻找:

    from google.appengine.ext.cloudstorage import cloudstorage_stub
    from google.appengine.api.blobstore import blobstore_stub
    

    和:

    blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
    storage_stub = cloudstorage_stub.CloudStorageStub(blob_storage)
    
    testbed._register_stub('blobstore', self.blob_stub)
    testbed._register_stub("cloudstorage", self.storage_stub)
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2014-06-28
      • 2015-12-13
      • 1970-01-01
      • 2019-07-28
      • 2014-01-30
      相关资源
      最近更新 更多