【问题标题】:Problem running pylon webtests. ImportError and TestController is not defined errror运行 pylon webtests 的问题。 ImportError 和 TestController 未定义错误
【发布时间】:2011-05-10 05:28:59
【问题描述】:

我的目录结构如下: gnukhata/测试/功能。 在功能文件夹中,我有网络测试文件。以下是样例测试。

from gnukhata.tests import *

class TestVendorController(TestController):

def test_index(self):
    response = self.app.get(url(controller='vendor', action='index'))

运行此测试文件后,出现以下错误:

Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 651, in   loadByNames
    things.append(self.findByName(name))
  File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 460, in findByName
    return filenameToModule(name)
  File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 98, in filenameToModule
    return _importFromFile(fn)
  File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 117, in _importFromFile
    module = imp.load_source(moduleName, fn, fd)
  File "test_vendor.py", line 1, in <module>
    from gnukhata.tests import *
exceptions.ImportError: No module named tests

如果我写 gnukhata 而不是 gnukhata.tests,那么它会显示以下错误:

   Traceback (most recent call last):
    File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 651, in loadByNames
things.append(self.findByName(name))
    File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 460, in findByName
return filenameToModule(name)
    File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 98, in filenameToModule
return _importFromFile(fn)
    File "/usr/lib/python2.6/dist-packages/twisted/trial/runner.py", line 117, in _importFromFile
    module = imp.load_source(moduleName, fn, fd)
    File "test_vendor.py", line 3, in <module>
    class TestVendorController(TestController):
exceptions.NameError: name 'TestController' is not defined

【问题讨论】:

    标签: python unit-testing pylons nosetests


    【解决方案1】:

    试试我最简单的配置,如果可行,请告诉我:

    import logging
    
    from pylons import request, response, session, tmpl_context as c, url
    from pylons.controllers.util import abort, redirect
    
    from gnukhata.lib.base import BaseController, render
    from gnukhata import model
    import gnukhata.model.meta as meta
    

    init.py:

    from unittest import TestCase
    
    from paste.deploy import loadapp
    from paste.script.appinstall import SetupCommand
    from pylons import url
    from routes.util import URLGenerator
    from webtest import TestApp
    from pylons import config
    
    import pylons.test
    
    __all__ = ['environ', 'url', 'TestController']
    
    # Invoke websetup with the current config file
    SetupCommand('setup-app').run([pylons.test.pylonsapp.config['__file__']])
    
    environ = {}
    
    class TestController(TestCase):
    
        def __init__(self, *args, **kwargs):
            wsgiapp = pylons.test.pylonsapp
            config = wsgiapp.config
            self.app = TestApp(wsgiapp)
            url._push_object(URLGenerator(config['routes.map'], environ))
            TestCase.__init__(self, *args, **kwargs)
    

    【讨论】:

    • 尝试了上述解决方案但没有成功。所以我写了 test.py : from pylons.controllers import WSGIController from pylons.templating import render_mako as render class TestController(WSGIController): def __call__(self, environ, start_response):返回 WSGIController.__call__(self, environ,start_response) 并将 test.py 文件保存在 gnukhata/tests/functional 目录中。使用试验运行测试不会给出任何错误。但是使用鼻子测试,它会给出错误:ImportError: No module named pylons.controllers。
    【解决方案2】:

    gnukhata/tests 目录中有__init__.py 吗?如果不是,则gnukhata.tests 未被识别为模块,您无法从中导入。

    如果确实存在这样的文件,您能否在此处发布 gnukhata/tests/__init__.py 中的导入语句(如果有)?

    【讨论】:

    • init.py 位于 gnukhata/tests 目录中。但它仍然给出相同的导入错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2013-07-12
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2013-12-14
    • 2020-08-13
    相关资源
    最近更新 更多