python - 全局名称 'RomanNumeralConverter' 未定义

标签 python python-2.7

我正在尝试编写单元测试,但总是在全局名称未定义时出现错误。

我的代码在test_TestCase.py

import unittest


class RomanNumeralConverterTest(unittest.TestCase):

    def test_parsing_millenia(self):
        value = RomanNumeralConverter("M")  # type: RomanNumeralConverter
        self.assertEquals(1000, value.convert_to_decimal)

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

    def test_parsing_decade(self):
        value = RomanNumeralConverter("X")
        self.assertEquals(10, value.convert_to_decimal())

    def test_parsing_half_decade(self):
        value = RomanNumeralConverter("V")
        self.assertEquals(5, value.convert_to_decimal())

    def test_parsing_parsing_one(self):
        value = RomanNumeralConverter("I")
        self.assertEquals(1, value.convert_to_decimal())

    def test_empty_roman_numeral(self):
        value = RomanNumeralConverter(" ")
        self.assertTrue(value.convert_to_decimal() == 0)
        self.assertFalse(value.convert_to_decimal() > 0)

    def test_no_roman_numeral(self):
        value = RomanNumeralConverter(None)
        self.assertRaises(TypeError, value.convert_to_decimal)


if __name__ == "__main__":
    unittest.main()

recipe1.py 中的代码

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}

    @property
    def convert_to_decimal(self):
        val = 0  # type: int
        for char in self.roman_numeral:
            val += self.digit_map[char]
        return val

NameError:全局名称“RomanNumeralConverter”未定义

<小时/>

在 0.001 秒内运行 8 次测试

失败(错误=8)

最佳答案

在你的unittest.py中,看起来你错过了导入你在其他文件中定义的类..

关于python - 全局名称 'RomanNumeralConverter' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130851/

相关文章:

Python - 根据每个日期的不同时间的条件过滤数据框

python - 如何防止 jsonschema validate() 连接到互联网

python - 在 Pandas 的条件下递增添加

Python:遍历列表项 x 次?

python - 从python中的numpy npz文件加载数组

Python + Selenium : get span value from "ng-bind"

python - 为什么 pandas bool 过滤会产生 float ?

php - Python 对 PHP 生成并存储在 Mysql 中的盐进行乱码

python - 如何获得书 "Web Scraping with Python: Collecting Data from the Modern Web"第 7 章数据标准化部分中的相同结果

Python 2.7 - 无效文字错误