nosetests 做另一个输出 (ImportError) 然后简单的单元测试 (No Error) 为什么?

nosetests does another output (ImportError) then simple unittest (No Error) Why?

当我仅使用 unittest 进行简单测试时,它没有显示任何错误。 但是当我尝试对我的测试文件进行 nosetests 时,它会出现 ImportError。这是必要的信息。

项目结构:

-rwxrwxr-x __init__.py
-rw-rw-r-- __init__.pyc
drwxrwxr-x romannumeralconverter
drwxrwxr-x tests

./romannumeralconverter:
-rwxrwxr-x __init__.py
-rw-rw-r-- __init__.pyc
-rwxrwxr-x romannumeralconverter.py
-rw-rw-r-- romannumeralconverter.pyc

./tests:
-rwxrwxr-x __init__.py
-rw-rw-r-- __init__.pyc
-rw-rw-r-- romannumeralconvertertest.py
-rw-rw-r-- romannumeralconvertertest.pyc

测试文件 - romannumeralconvertertest.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
from romannumeralconverter.romannumeralconverter import RomanNumeralConverter

class RomanNumeralConverterTest(unittest.TestCase):
    def test_parsing_millenia(self):
        """Testing if millenia"""
        value = RomanNumeralConverter("M")
        self.assertEquals(1000, value.convert_to_decimal())

    #@unittest.skip("demonstrating skipping")
    def test_parsing_century(self):
        value = RomanNumeralConverter("C")
        self.assertEquals(100, value.convert_to_decimal())

    def test_parsing_half_century(self):
        value = RomanNumeralConverter("L")
        self.assertEquals(50, value.convert_to_decimal())

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(RomanNumeralConverterTest)
    unittest.TextTestRunner(verbosity=3).run(suite)

应用程序文件:- romannumeralconverter.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class RomanNumeralConverter(object):
    def __init__(self, roman_numeral):
        self.roman_numeral = roman_numeral
        self.digit_map = {"M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1}
        self.i_rule = ('V','X')
        self.x_rule = ('L','C')
        self.c_rule = ('D','M')
        self.rules = {"I": self.i_rule, "X": self.x_rule, "C": self.c_rule}

    def zwei_convert_to_decimal(self):
        val = 0
        oldelement = 0
        for char in self.roman_numeral:
            if oldelement != 0 and self.digit_map[char] > self.digit_map[oldelement]:
                pass
            oldelement = char

    def convert_to_decimal(self):
        val = 0
        for char in self.roman_numeral:
            val += self.digit_map[char]
        return val

正常测试输出:

▶ python romannumeralconvertertest.py
test_parsing_century (__main__.RomanNumeralConverterTest) ... ok
test_parsing_half_century (__main__.RomanNumeralConverterTest) ... ok
test_parsing_millenia (__main__.RomanNumeralConverterTest)
Testing if millenia ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

鼻子测试输出:

▶ nosetests romannumeralconvertertest.py
E
======================================================================
ERROR: Failure: ImportError (cannot import name RomanNumeralConverter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 420, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/username/Development/learning/romannumeralconverter/tests/romannumeralconvertertest.py", line 5, in <module>
    from romannumeralconverter.romannumeralconverter import RomanNumeralConverter
ImportError: cannot import name RomanNumeralConverter

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

sys.path

/home/username/Development/learning/romannumeralconverter/tests
/home/username/Development/learning/romannumeralconverter/tests
/home/username/Development/learning/romannumeralconverter
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/home/username/.local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/python2.7/dist-packages/ubuntu-sso-client

默认情况下,nose 对 sys.path 进行了一些调整。您可以在此处找到一种可能的解决方案: Accepted answer of - Python Nose Import Error.

从您的项目结构中,如果您从以下位置删除 __init__.py

/home/username/Development/learning/romannumeralconverter

目录,应该可以。