【问题标题】:Testing cherrypy WSGI application using Webtest and or nose?使用 Webtest 和或鼻子测试cherrypy WSGI 应用程序?
【发布时间】:2012-12-16 14:13:11
【问题描述】:

如何使用 WSGI 接口使用 nosewebtest 广泛测试带有 json 或 html 结果的cherrypy应用程序?

任何例子都会受到赞赏。

【问题讨论】:

    标签: python testing cherrypy nose webtest


    【解决方案1】:

    这个例子可能会有所帮助:

    from webtest import TestApp
    from nose.tools import eq_
    
    class TestAuthentication(object):
        """
        Tests for the default authentication setup.
    
        If your application changes how the authentication layer is configured
        those tests should be updated accordingly
        """
    
        application_under_test = 'main'
    
        def setUp(self):
            """Method called by nose before running each test"""
            # Loading the application:
            conf_dir = config.here
            wsgiapp = loadapp('config:test.ini#%s' % self.application_under_test,
                              relative_to=conf_dir)
            self.app = TestApp(wsgiapp)
            # Setting it up:
            test_file = path.join(conf_dir, 'test.ini')
            cmd = SetupCommand('setup-app')
            cmd.run([test_file])
    
        def tearDown(self):
            """Method called by nose after running each test"""
            # Cleaning up the database:
            model.DBSession.remove()
            teardown_db()
    
        def test_forced_login(self):
            """Anonymous users are forced to login
    
            Test that anonymous users are automatically redirected to the login
            form when authorization is denied. Next, upon successful login they
            should be redirected to the initially requested page.
    
            """
            # Requesting a protected area
            resp = self.app.get('/secc/', status=302)
            assert resp.location.startswith('http://localhost/login')
            # Getting the login form:
            resp = resp.follow(status=200)
            print resp.forms
            form = resp.forms
            # Submitting the login form:
            form['login'] = u'manager'
            form['password'] = 'managepass'
            post_login = form.submit(status=302)
            # Being redirected to the initially requested page:
            assert post_login.location.startswith('http://localhost/post_login')
            initial_page = post_login.follow(status=302)
            assert 'authtkt' in initial_page.request.cookies, \
                   "Session cookie wasn't defined: %s" % initial_page.request.cookies
            assert initial_page.location.startswith('http://localhost/secc/'), \
                   initial_page.location
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 2023-03-20
      • 2014-10-21
      • 1970-01-01
      • 2013-06-16
      • 2014-01-19
      • 2012-08-17
      • 1970-01-01
      相关资源
      最近更新 更多