我正在尝试使用BFS
算法解决theEight_Puzzle。因此,我需要一个队列来存储每个状态(例如[None, 1, 2, 3, 4, 5, 6, 7, 8]
)。
这是我的Queue
班:
class Queue():
def __init__(self):
self.queue = []
def enqueue(self, number):
self.queue.append(number)
def dequeue(self):
return self.queue.pop(0)
然后我编写了一个测试用例以使用pytest对该功能进行测试
def test_it_handles_lists_correctly():
queue = Queue()
queue.enqueue([1,2,3])
assert list(queue.dequeue()) is [1,2,3]
但是当我运行测试时,它失败并显示以下消息:
=========================== test session starts ===========================
platform win32 -- Python 3.7.0, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: C:\Users\Administrator\Desktop\code-ai\ai-eight-puzzle\eight-puzzle, inifile:
collected 10 items
test_helpers.py .. [ 20%]
test_puzzle.py ...... [ 80%]
test_queue.py .F [100%]
================================ FAILURES =================================
_____________________ test_ic_handles_lists_correctly _____________________
def test_ic_handles_lists_correctly():
queue = Queue()
queue.enqueue([1,2,3])
> assert list(queue.dequeue()) is [1,2,3]
E assert [1, 2, 3] is [1, 2, 3]
E + where [1, 2, 3] = list([1, 2, 3])
E + where [1, 2, 3] = <bound method Queue.dequeue of <Queue.Queue
object at 0x028F5AD0>>()
E + where <bound method Queue.dequeue of <Queue.Queue object at
0x028F5AD0>> = <Queue.Queue object at 0x028F5AD0>.dequeue
test_queue.py:20: AssertionError
=================== 1 failed, 9 passed in 0.17 seconds ====================
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您的Queue
定义似乎不错,但是您必须修复测试。
在断言中使用==
比较运算符,而不要使用is
或,以确保对同一实例断言:
def test_it_handles_lists_correctly():
queue = Queue()
my_list = [1,2,3]
queue.enqueue(my_list)
assert queue.dequeue() is my_list
is
运算符与变量的值不匹配,但实例本身不匹配。