我一直在使用Python进行单元测试的在线课程,并且在课程的第一部分中,他们以一个简陋的电话簿为例。作为练习,他们希望我们实现两个功能以通过以下单元测试
import unittest
from phonebook import Phonebook
class PhonebookTest(unittest.TestCase):
def setUp(self):
self.phonebook = Phonebook()
def test_lookup_entry_by_name(self):
self.phonebook.add("Bob", "12345")
self.assertEqual("12345", self.phonebook.lookup("Bob"))
def test_missing_entry_raises_KeyError(self):
with self.assertRaises(KeyError):
self.phonebook.lookup("missing")
def test_empty_phonebook_is_consistent(self):
self.assertTrue(self.phonebook.is_consistent())
def test_phonebook_with_normal_entries_is_consistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "012345")
self.assertTrue(self.phonebook.is_consistent())
def test_phonebook_with_duplicate_entries_is_inconsistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "12345")
self.assertFalse(self.phonebook.is_consistent())
def test_phonebook_with_numbers_that_prefix_one_another_is_inconsistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "123")
self.assertFalse(self.phonebook.is_consistent())
def test_phonebook_adds_names_and_numbers(self):
phonebook = Phonebook()
self.phonebook.add("Sue", "12345")
self.assertIn("Sue", phonebook.get_names())
self.assertIn("12345", phonebook.get_numbers())
此文件放置在名为test_phonebook.py的python文件中。两个功能get_names()
和get_numbers()
放在一个名为phonebook.py的文件中。这是我在下面的尝试实现
class Phonebook:
def __init__(self):
self.entries = {}
def add(self, name, number):
self.entries[name] = number
def lookup(self, name):
return self.entries[name]
def is_consistent(self):
return self.entries
def get_names(self):
return self.entries.keys()
def get_numbers(self):
return self.entries.values()
当我尝试在test_phonebook.py上运行单元测试时,它失败并显示NameError: name 'phonebook' is not defined
。我不明白为什么会这样。
答案 0 :(得分:0)
我猜您在phonebook
中的self.phonebook
中实例化了setUp
对象,所以您应该使用self.phonebook.get_names()
,因为您想使用类对象来调用类方法,而不是创建自己的phonebook = Phonebook()
并使用它。
def test_phonebook_adds_names_and_numbers(self):
#Use self to access phonebook object and associated methods
self.phonebook.add("Sue", "12345")
self.assertIn("Sue", self.phonebook.get_names())
self.assertIn("12345", self.phonebook.get_numbers())
此外,您的其他测试用例也不一致。要检查字典是否为空,应使用assertFalse
而不是assertTrue
;对于非空字典,应使用assertTrue
,修复这些问题以及上面提到的问题(下面的unittest类)应该运行得很好
class PhonebookTest(unittest.TestCase):
def setUp(self):
self.phonebook = Phonebook()
def test_lookup_entry_by_name(self):
self.phonebook.add("Bob", "12345")
self.assertEqual("12345", self.phonebook.lookup("Bob"))
def test_missing_entry_raises_KeyError(self):
with self.assertRaises(KeyError):
self.phonebook.lookup("missing")
def test_empty_phonebook_is_consistent(self):
self.assertFalse(self.phonebook.is_consistent())
def test_phonebook_with_normal_entries_is_consistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "012345")
self.assertTrue(self.phonebook.is_consistent())
def test_phonebook_with_duplicate_entries_is_inconsistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "12345")
self.assertTrue(self.phonebook.is_consistent())
def test_phonebook_with_numbers_that_prefix_one_another_is_inconsistent(self):
self.phonebook.add("Bob", "12345")
self.phonebook.add("Mary", "123")
self.assertTrue(self.phonebook.is_consistent())
def test_phonebook_adds_names_and_numbers(self):
self.phonebook.add("Sue", "12345")
self.assertIn("Sue", self.phonebook.get_names())
self.assertIn("12345", self.phonebook.get_numbers())
输出为
$ python3.7 -m unittest script.py
.......
----------------------------------------------------------------------
Ran 7 tests in 0.000s
OK