![](/img/trans.png)
[英]ReactorNotRestartable when launching two equivalent unittest with twisted and trial
[英]twisted trial unittest gets ImportError
我正在嘗試為我的扭曲應用程序編寫一些使用試用版的單元測試,我編寫了我的第一個“空”試用單元測試 class 但是當我嘗試運行它時,我得到 ImportError 導入我的應用程序模塊。
我懷疑這是因為試用更改了當前工作目錄,並且當我嘗試使用 object class 導入我的模塊時,我想對其進行單元測試失敗。
我將我的應用程序模塊保存在我自己設置的單個目錄中,它不在 PYTHONPATH 或其他已知的任何目錄中,在我的應用程序中,模塊導入其他模塊,因為它們都在同一個目錄中。
代碼看起來與此類似:
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')
所以錯誤消息如下所示:
exceptions.ImportError:沒有名為 MyModule 的模塊
也許這不是使用試用版或部署 python 應用程序的標准方式。
更新:我只是想出了一個解決方法,只是 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
您的模塊/包是如何組織的? 也許嘗試如下結構,那么您將不需要做任何路徑黑客:
$ 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
從 mypackage package 目錄上方運行測試:
$ trial mypackage
mypackage.test.test_mymodule
MyModuleTestCase
test_something ... [OK]
-------------------------------------------------------------------------------
Ran 1 tests in 0.002s
PASSED (successes=1)
內部文件 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.