我的目录结构如下: gnukhata /测试/功能。 在功能文件夹中,我有web测试文件。以下是样本测试。
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
答案 0 :(得分: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)
答案 1 :(得分:0)
__init__.py
目录中是否有gnukhata/tests
?如果没有,则gnukhata.tests
不会被识别为模块,您无法从中导入。
如果此类文件确实存在,您可以在此处发布gnukhata/tests/__init__.py
中的导入语句吗?