使用鼻子导入错误,使用原始单元测试没有导入错误?

发布于 2024-08-31 12:19:24 字数 2560 浏览 4 评论 0原文

使用 Nose 运行单元测试时出现 ImportError 错误,而当我单独运行它时则不会。此处引用的所有文件均可在 http://gist.github.com/395541# 中查看。

如果我直接运行测试脚本 importTest-Test.py,我会得到以下输出:

C:\usr\x\data\src\Python\mmm>python importTest-Test.py
In mmdb
In BusinessLogic
[]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

如果我允许 Nose 运行它,我会得到一个错误:

C:\usr\x\data\src\Python\mmm>nosetests.exe
E
======================================================================
ERROR: Failure: ImportError (No module named mmdb.DataAccess.AttemptDB)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\loader.py", line 382, in loadTestsFromName
    addr.filename, addr.module)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "C:\usr\x\data\src\Python\mmm\importtest-Test.py", line 2, in <module>
    import importtest
  File "C:\usr\x\data\src\Python\mmm\importtest.py", line 1, in <module>
    from mmdb.BusinessLogic.AttemptManager import AttemptManager
  File "C:\usr\x\data\src\Python\mmm\mmdb\BusinessLogic\AttemptManager.py", line 1, in <module>
    from mmdb.DataAccess.AttemptDB import AttemptDB
ImportError: No module named mmdb.DataAccess.AttemptDB

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

The filesvolvedinthepackagewhichnose 有困难结构如下 - 有些可以在这里看到 http://gist.github.com/395541# .:

mmm\importtest-Test.py
mmm\importtest.py
mmm\mmdb
mmm\__init__.py
mmm\mmdb\BusinessLogic
mmm\mmdb\BusinessObject
mmm\mmdb\DataAccess
mmm\mmdb\__init__.py
mmm\mmdb\BusinessLogic\AttemptManager.py
mmm\mmdb\BusinessLogic\Collections
mmm\mmdb\BusinessLogic\__init__.py
mmm\mmdb\BusinessLogic\Collections\__init__.py
mmm\mmdb\BusinessObject\__init__.py
mmm\mmdb\DataAccess\AttemptDB.py
mmm\mmdb\DataAccess\__init__.py

这发生在 Win32 / Python 2.6 / Nose 0.11.3 上。

我将不胜感激任何帮助。

谢谢。

I get an ImportError when running my unittests using Nose and I don't when I just run it standalone. All files referred to here may be seen at http://gist.github.com/395541# .

If I run the test script, importTest-Test.py, directly I get this output:

C:\usr\x\data\src\Python\mmm>python importTest-Test.py
In mmdb
In BusinessLogic
[]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

If I allow Nose to run it I get an error :

C:\usr\x\data\src\Python\mmm>nosetests.exe
E
======================================================================
ERROR: Failure: ImportError (No module named mmdb.DataAccess.AttemptDB)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\loader.py", line 382, in loadTestsFromName
    addr.filename, addr.module)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "C:\usr\x\data\src\Python\mmm\importtest-Test.py", line 2, in <module>
    import importtest
  File "C:\usr\x\data\src\Python\mmm\importtest.py", line 1, in <module>
    from mmdb.BusinessLogic.AttemptManager import AttemptManager
  File "C:\usr\x\data\src\Python\mmm\mmdb\BusinessLogic\AttemptManager.py", line 1, in <module>
    from mmdb.DataAccess.AttemptDB import AttemptDB
ImportError: No module named mmdb.DataAccess.AttemptDB

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

The files involved in the package which nose is having difficulties with are in the following structure - some may be seen here http://gist.github.com/395541# .:

mmm\importtest-Test.py
mmm\importtest.py
mmm\mmdb
mmm\__init__.py
mmm\mmdb\BusinessLogic
mmm\mmdb\BusinessObject
mmm\mmdb\DataAccess
mmm\mmdb\__init__.py
mmm\mmdb\BusinessLogic\AttemptManager.py
mmm\mmdb\BusinessLogic\Collections
mmm\mmdb\BusinessLogic\__init__.py
mmm\mmdb\BusinessLogic\Collections\__init__.py
mmm\mmdb\BusinessObject\__init__.py
mmm\mmdb\DataAccess\AttemptDB.py
mmm\mmdb\DataAccess\__init__.py

This is happening on Win32 / Python 2.6 / Nose 0.11.3 .

I'd be grateful for any help.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀子花开つ 2024-09-07 12:19:24

默认情况下,nose 操作它使用的 PYTHONPATH。您可以尝试使用 -P 开关关闭此行为。

By default, nose manipulates the PYTHONPATH it uses. You might try turning off this behavior using the -P switch.

不必在意 2024-09-07 12:19:24

您可以尝试从顶级文件夹 mmm 中删除 __init__.py
(原始答案在这里:https://stackoverflow.com/a/3073368/19166

You could try to remove __init__.py from the top level folder mmm
(original answer here: https://stackoverflow.com/a/3073368/19166)

暮倦 2024-09-07 12:19:24

看来您的问题可能是由于文件名中的破折号“-”造成的。另请参阅:https://stackoverflow.com/a/11055442/1063605

Looks like your problem could be due to the dash "-" in your filename. See also: https://stackoverflow.com/a/11055442/1063605

抱猫软卧 2024-09-07 12:19:24

这是涉及 PyUnit非常具体的用例的答案。

我有一组单元测试在 PyDev 下运行良好。有一天,我犯了一个打字错误,PyDev 添加了对 pandas 包的一部分的自动导入。我通常将代码折叠起来,所以我没有立即看到这一点。

测试集后面部分出现的错误是“错误:无法导入nose”。

在调试中,我发现某个数据文件名其中一个子目录名重复。测试运行程序似乎将工作目录更改为包含 .py 文件的子目录,但没有返回到项目目录。调用 os.path.realpath(" _ file _ ") 设置数据文件路径会返回测试子目录而不是预期的项目目录,最终结果是找不到数据并且测试失败。

“修复”设置数据文件路径的代码解决了此错误。然而,当我重新处理剩余的错误时,我发现并删除了不需要的导入语句。此时,数据文件路径开始出现错误,因此我将其改回原始形式,一切正常。

所以......如果你发现你突然遇到这些“鼻子”错误,可能是你的编辑无意中引入了一个 import 语句,使 PyUnit 陷入困境。

我在这里添加答案是因为我的部分经验是使用原始单元测试(来自 PyDev 上下文菜单)运行各个测试文件,并且对为什么它们以这种方式工作但在由完整测试运行程序运行时却不然感到非常困惑。

This is an answer for a very specific use case involving PyUnit.

I had a set of unit tests running fine under PyDev. One day, I made a typing error, and PyDev added an automatic import for a part of the pandas package. I usually keep my code folded so I didn't see this right away.

The error that appeared in a later part of the test set was "Error: could not import nose".

In debugging, I found that a data file name had one of the subdirectory names repeated. It appeared that the test runner was changing the working directory to the subdirectory containing the .py file, but not going back to the project directory. A call to os.path.realpath(" _ file _ ") to set up the data file path was returning the test subdirectory instead of the expected project directory, with the net effect that the data wasn't found and the test failed.

"Fixing" the code that set up the data file path resolved this error. However, as I worked my way back through the remaining errors, I discovered and removed the unwanted import statement. At that point, the data file path started giving errors, so I changed it back to the original form and everything was fine.

So... if you find that you are suddenly getting these "nose" errors, it may be that your editing has inadvertently introduced an import statement that throws PyUnit out of whack.

I'm adding the answer here because part of my experience was running the individual test files with raw unittest (from the PyDev context menu), and feeling very puzzled as to why they worked that way but not when run by the full test runner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文