2011-05-10 50 views
1

我有如下的目錄結構: gnukhata/tests/functional。 在功能文件夾中我有Web測試文件。以下是樣品測試。運行pylon webtests的問題。 ImportError和TestController沒有定義errror

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.tests的,如果我寫gnukhata話,就說明了以下錯誤:

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 

回答

0

試試我最簡單的配置,讓我知道它是否工作:

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) 
+0

嘗試了上述解決方案,但沒有奏效out.So我寫test.py:從pylons.controllers 匯入pylons.templating進口render_mako WSGIController 作爲渲染 類的TestController(WSGIController): 高清__call __(自我,ENVIRON ,start_response): 返回WSGIController .__調用__(self,environ,start_response) 並將test.py文件保存在gnukhata/tests/functional目錄中。 使用trial運行測試不會給出任何錯誤。但是使用nosetest,它會給出錯誤:ImportError:沒有名爲pylons.controllers的模塊。 – ssonaldd 2011-05-12 12:49:43

0

是否有一個__init__.pygnukhata/tests目錄?如果不是,則gnukhata.tests不被識別爲模塊,您不能從中導入。

如果此類文件確實存在,您可以在這裏發佈gnukhata/tests/__init__.py中的導入語句嗎?

+0

__init__.py在gnukhata/tests目錄中。但它仍然會導致相同的導入錯誤。 – ssonaldd 2011-05-12 12:31:27

相關問題