日志服务存根测试平台Python Google App Engine

时间:2018-08-08 13:39:33

标签: python google-app-engine google-app-engine-python testbed

class MyTestBase(unittest.TestCase):
    def setUp(self):
        from google.appengine.ext import testbed
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.testbed.init_mail_stub()
        self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)

        self.testbed.init_taskqueue_stub()
        self.taskqueue_stub = self.testbed.get_stub(
            testbed.TASKQUEUE_SERVICE_NAME)

        self.testbed.init_logservice_stub()
        self.log_stub = self.testbed.get_stub(testbed.LOG_SERVICE_NAME)

    def testMailSent(self):
        mail.send_mail(to='alice@example.com',
                       subject='This is a test',
                       sender='bob@example.com',
                       body='This is a test e-mail')
        messages = self.mail_stub.get_sent_messages(to='alice@example.com')
        self.assertEqual(1, len(messages))
        self.assertEqual('alice@example.com', messages[0].to)

    def testTaskAddedToQueue(self):
        taskqueue.Task(name='my_task', url='/url/of/my/task/').add()
        tasks = self.taskqueue_stub.get_filtered_tasks()
        self.assertEqual(len(tasks), 1)
        self.assertEqual(tasks[0].name, 'my_task')

    def testLogAddedtoLogs(self):
        logging.info('hi')
        logs = self.log_stub. #WHAT METHOD TO CALL HERE
        self.assertEqual(len(logs), 1)

此处未提供有关如何使用logservice_stub的文档 https://cloud.google.com/appengine/docs/standard/python/tools/localunittesting 有人帮忙。

1 个答案:

答案 0 :(得分:1)

您最好的选择是浏览源代码并不幸地找到答案

from google.appengine.ext import testbed在这里:

https://github.com/optimizely/python-appengine/blob/master/google/appengine/ext/testbed/init.py

但我认为这最终是您想要的文件:

https://github.com/optimizely/python-appengine/blob/master/google/appengine/api/logservice/logservice_stub.py