python unittest assertIsInstance 意外结果
python unittest assertIsInstance unexpected result
我无法断言对象是 class 的实例
在下面 class 你会看到 obj 确实是 TrieNode 的一个实例
但是,单元测试失败,抱怨它是 Bool
from collections import defaultdict
class TrieNode:
def __init__(self):
self.children = defaultdict(TrieNode)
self.is_word = False
def suffixes(self, suffix = ''):
"""
Recursive function that collects the suffix for
all complete words below this point
"""
pass
class Trie(object):
def __init__(self):
self.root = TrieNode()
def add(self, word):
"""
Add `word` to trie
"""
current_node = self.root
for char in word:
current_node = current_node.children[char]
current_node.is_word = True
def exists(self, word):
"""
Check if word exists in trie
"""
current_node = self.root
for char in word:
if char not in current_node.children:
return False
current_node = current_node.children[char]
return current_node.is_word
def find(self, prefix):
"""
Find the Trie node that represents this prefix
:param prefix: For example 'f'
:return: TrieNode 'f':{...}
"""
current_node = self.root
for char in prefix:
if char not in current_node.children:
return False
current_node = current_node.children[char]
return current_node
my_trie = Trie()
wordList = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in wordList:
my_trie.add(word)
obj = my_trie.find('a')
print(type(obj)) # <class '__main__.TrieNode'>
print(isinstance(obj, TrieNode)) # True
如你所见,我得到了
print(type(node_a)) # <class '__main__.TrieNode'>
print(isinstance(node_a, TrieNode)) # True
现在我有了这个单元测试
import unittest
from problem_05_trie_autocomplete import Trie, TrieNode
class TestTrieAutoComplete(unittest.TestCase):
"""
python -m unittest
"""
def setUp(self):
self.my_trie = Trie()
def test_add_to_trie(self):
"""
Add words to Trie and check their existence
:return:
"""
word_list = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in word_list:
self.my_trie.add(word)
self.assertTrue(self.my_trie.exists(word))
def test_assert_is_trienode(self):
obj = self.my_trie.find('a')
self.assertIsInstance(obj, TrieNode)
def test_assert_is_with_type_trienode(self):
obj1 = self.my_trie.find('a')
self.assertIs(type(obj1), TrieNode)
我在最近两次测试中有两个错误
self.assertIsInstance(obj, TrieNode)
AssertionError: False is not an instance of <class 'problem_05_trie_autocomplete.TrieNode'>
======================
self.assertIs(type(obj1), TrieNode)
AssertionError: <class 'bool'> is not <class 'problem_05_trie_autocomplete.TrieNode'>
FAILED (failures=2)
为什么 obj 和 obj1 在 unittest 中是 Bool 同时在 class obj 是 TrieNode 的一个实例,它应该是?
提前致谢
谢谢@MisterMiyagi
这是为面临相同问题的人提供的解决方案
要学习的一点是设置函数中 my_trie 的状态将是所有其他测试
将使用的状态
import unittest
from problem_05_trie_autocomplete import Trie, TrieNode
class TestTrie(unittest.TestCase):
"""
python -m unittest
"""
def setUp(self):
self.my_trie = Trie()
self.word_list = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in self.word_list:
self.my_trie.add(word)
def test_add_to_trie(self):
"""
Add words to Trie and check their existence
:return:
"""
for word in self.word_list:
self.my_trie.add(word)
self.assertTrue(self.my_trie.exists(word))
def test_unexisting_word(self):
self.assertFalse(self.my_trie.exists("unexistingWord"))
self.assertFalse(self.my_trie.find('x'))
self.assertFalse(self.my_trie.find('y'))
def test_assert_is_trienode(self):
obj = self.my_trie.find('a')
self.assertIsInstance(obj, TrieNode)
def test_assert_is_with_type_trienode(self):
obj1 = self.my_trie.find('a')
self.assertIs(type(obj1), TrieNode)
我无法断言对象是 class 的实例 在下面 class 你会看到 obj 确实是 TrieNode 的一个实例 但是,单元测试失败,抱怨它是 Bool
from collections import defaultdict
class TrieNode:
def __init__(self):
self.children = defaultdict(TrieNode)
self.is_word = False
def suffixes(self, suffix = ''):
"""
Recursive function that collects the suffix for
all complete words below this point
"""
pass
class Trie(object):
def __init__(self):
self.root = TrieNode()
def add(self, word):
"""
Add `word` to trie
"""
current_node = self.root
for char in word:
current_node = current_node.children[char]
current_node.is_word = True
def exists(self, word):
"""
Check if word exists in trie
"""
current_node = self.root
for char in word:
if char not in current_node.children:
return False
current_node = current_node.children[char]
return current_node.is_word
def find(self, prefix):
"""
Find the Trie node that represents this prefix
:param prefix: For example 'f'
:return: TrieNode 'f':{...}
"""
current_node = self.root
for char in prefix:
if char not in current_node.children:
return False
current_node = current_node.children[char]
return current_node
my_trie = Trie()
wordList = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in wordList:
my_trie.add(word)
obj = my_trie.find('a')
print(type(obj)) # <class '__main__.TrieNode'>
print(isinstance(obj, TrieNode)) # True
如你所见,我得到了
print(type(node_a)) # <class '__main__.TrieNode'>
print(isinstance(node_a, TrieNode)) # True
现在我有了这个单元测试
import unittest
from problem_05_trie_autocomplete import Trie, TrieNode
class TestTrieAutoComplete(unittest.TestCase):
"""
python -m unittest
"""
def setUp(self):
self.my_trie = Trie()
def test_add_to_trie(self):
"""
Add words to Trie and check their existence
:return:
"""
word_list = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in word_list:
self.my_trie.add(word)
self.assertTrue(self.my_trie.exists(word))
def test_assert_is_trienode(self):
obj = self.my_trie.find('a')
self.assertIsInstance(obj, TrieNode)
def test_assert_is_with_type_trienode(self):
obj1 = self.my_trie.find('a')
self.assertIs(type(obj1), TrieNode)
我在最近两次测试中有两个错误
self.assertIsInstance(obj, TrieNode)
AssertionError: False is not an instance of <class 'problem_05_trie_autocomplete.TrieNode'>
======================
self.assertIs(type(obj1), TrieNode)
AssertionError: <class 'bool'> is not <class 'problem_05_trie_autocomplete.TrieNode'>
FAILED (failures=2)
为什么 obj 和 obj1 在 unittest 中是 Bool 同时在 class obj 是 TrieNode 的一个实例,它应该是?
提前致谢
谢谢@MisterMiyagi
这是为面临相同问题的人提供的解决方案 要学习的一点是设置函数中 my_trie 的状态将是所有其他测试
将使用的状态import unittest
from problem_05_trie_autocomplete import Trie, TrieNode
class TestTrie(unittest.TestCase):
"""
python -m unittest
"""
def setUp(self):
self.my_trie = Trie()
self.word_list = [
"ant", "anthology", "antagonist", "antonym",
"fun", "function", "factory",
"trie", "trigger", "trigonometry", "tripod"
]
for word in self.word_list:
self.my_trie.add(word)
def test_add_to_trie(self):
"""
Add words to Trie and check their existence
:return:
"""
for word in self.word_list:
self.my_trie.add(word)
self.assertTrue(self.my_trie.exists(word))
def test_unexisting_word(self):
self.assertFalse(self.my_trie.exists("unexistingWord"))
self.assertFalse(self.my_trie.find('x'))
self.assertFalse(self.my_trie.find('y'))
def test_assert_is_trienode(self):
obj = self.my_trie.find('a')
self.assertIsInstance(obj, TrieNode)
def test_assert_is_with_type_trienode(self):
obj1 = self.my_trie.find('a')
self.assertIs(type(obj1), TrieNode)