简体   繁体   English

扭曲的试验单元测试得到 ImportError

[英]twisted trial unittest gets ImportError

I'm trying to write some unittests using trial for my twisted application, I wrote my first 'empty' trial unittest class but when I try to run it I get ImportError for importing my app module.我正在尝试为我的扭曲应用程序编写一些使用试用版的单元测试,我编写了我的第一个“空”试用单元测试 class 但是当我尝试运行它时,我得到 ImportError 导入我的应用程序模块。

This is as I suspect because trial changes the current working directory and when I try to import my module with the object class that I want to unittest it fails.我怀疑这是因为试用更改了当前工作目录,并且当我尝试使用 object class 导入我的模块时,我想对其进行单元测试失败。

I keep my application modules in a single directory which I set up by myself, it's not in any directory from PYTHONPATH or else known, in my app the modules import other modules since they all are in the same dir.我将我的应用程序模块保存在我自己设置的单个目录中,它不在 PYTHONPATH 或其他已知的任何目录中,在我的应用程序中,模块导入其他模块,因为它们都在同一个目录中。

The code looks similiar to this:代码看起来与此类似:

from twisted.trial import unittest
from twisted.spread import pb
from twisted.internet import reactor, protocol 
from MyModule import MyTestSubject

class MyTestSubjectTest(unittest.TestCase):

    def setUp(self):
        print('\nset up')


    def test_startConsoleServer(self):
        ts = MyTestSubject()
        .... # here goes the body of the test


    def tearDown(self):
        print('\ntear down')

So the error msg looks like this:所以错误消息如下所示:
exceptions.ImportError: No module named MyModule exceptions.ImportError:没有名为 MyModule 的模块

Maybe this is not the standard way of using trial or deploying a python app.也许这不是使用试用版或部署 python 应用程序的标准方式。

UPDATE: I just figured out a workaround for this, just append the app directory to sys.path so the imports part will look like this:更新:我只是想出了一个解决方法,只是 append 应用程序目录到 sys.path 所以导入部分看起来像这样:

from twisted.trial import unittest
from twisted.spread import pb
from twisted.internet import reactor, protocol 
import sys, os; sys.path.append(os.path.abspath(os.path.curdir))
from MyModule import MyTestSubject

How are your modules / packages organised?您的模块/包是如何组织的? Perhaps try a structure like the following, then you won't need to do any path hackery:也许尝试如下结构,那么您将不需要做任何路径黑客:

$ ls -R mypackage
mypackage:
__init__.py  __init__.pyc  mymodule.py  mymodule.pyc  test

mypackage/test:
__init__.py  __init__.pyc  test_mymodule.py  test_mymodule.pyc

run tests from just above the mypackage package dir:从 mypackage package 目录上方运行测试:

$ trial mypackage
mypackage.test.test_mymodule
  MyModuleTestCase
    test_something ...                                                     [OK]

-------------------------------------------------------------------------------
Ran 1 tests in 0.002s

PASSED (successes=1)

inside file test_mymodule.py:内部文件 test_mymodule.py:

from twisted.trial import unittest

from mypackage.mymodule import MyTestSubject

class MyModuleTestCase(unittest.TestCase):
    def test_something(self):
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM