Python 测试webapp2:未识别所需的登录

Python 测试webapp2:未识别所需的登录,python,google-app-engine,webapp2,Python,Google App Engine,Webapp2,我在app.yaml中有以下内容 handlers: - url: /.* script: app.application secure: always login: required 当运行测试时,我按照谷歌的建议使用这个 class SearchTest(unittest.TestCase): def setUp(self): # Set up app simulator app = webapp2.WSGIApplication([(

我在app.yaml中有以下内容

handlers:
- url: /.*
  script: app.application
  secure: always
  login: required
当运行测试时,我按照谷歌的建议使用这个

class SearchTest(unittest.TestCase):
    def setUp(self):

        # Set up app simulator
        app = webapp2.WSGIApplication([('/search', search.Search)], debug=True)
        self.testapp = webtest.TestApp(app)

        # Google testbed
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.testbed.init_user_stub()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

        # Disable caching to prevent data from leaking between tests
        ndb.get_context().set_cache_policy(False)


    def testNotLoggedin(self):
        # Test user is redirected to login when not logged in

        assert not users.get_current_user()

        response = self.testapp.get('/search')
        self.assertEqual(response.status_int, 302)
        assert response.headers['Location']
testNotLoggedIn失败,错误为200!=302因此,即使需要登录,用户似乎仍然可以访问。这让我觉得app.yaml在测试中没有被识别


如何确保app.yaml被识别并且用户需要登录?

此测试代码绕过app.yaml调度。在appengine和dev服务器上,HTTP请求通过App.yaml路由到WSGI应用程序实例,处理login:required的前端逻辑发生在调用请求处理程序之前。在这个测试代码中,您将直接访问WSGI应用程序:self.testapp.get'/search'只是调用WSGI应用程序自己的内部URL映射来访问search.search请求处理程序


要测试的条件更像是集成测试,需要运行的dev服务器或部署的appengine测试版本。这是一个好主意,它比testbed等人所能做的要大。

我相信这些规则在发送程序到达应用程序之前就已经由发送程序处理了。如果不启动dev_appserver实例并通过HTTP测试到该服务器,那么没有一个好的方法来测试它。尽管在我链接到它的testrunner中,看起来您导入了dev_appserver…感谢您的回复:。同时,我向每个处理程序添加了所需的登录名。这样,我仍然可以测试它,而无需花费太多精力创建一个更大的集成测试东西。