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

时间:2016-01-23 09:32:47

标签: python nose python-unittest

当我使用unittest进行简单测试时,它不显示任何错误。 但是,当我尝试测试我的测试文件时,它会执行ImportError。以下是nessesary信息。

项目结构:

-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

Testfile - 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)

Appfile: - 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输出:

▶ 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

1 个答案:

答案 0 :(得分:1)

默认情况下,nose会对file 'video.mp4' inpoint 23.342 outpoint 23.372 file 'video.mp4' inpoint 57.031 outpoint 57.064 ... 进行一些调整。您可以在此处找到一种可能的解决方案: Accepted answer of - Python Nose Import Error

如果从项目结构中删除sys.path

__init__.py

目录,它应该可以工作。