我试着写一个简单的堆栈类来学习TDD。但问题是它无法通过使用正确代码的测试
代码如下:
class Stack:
def __init__(self):
self.stack = []
def push(self,new_item):
self.stack.append(new_item)
def pop(self):
return int(self.stack.pop(0))
下面是测试类:
import pytest
from Stack import Stack
def test_it_can_push():
stack = Stack()
stack.push(2)
assert stack.stack is [2]
下面是错误:
def test_it_can_push():
stack = Stack()
stack.push(2)
> assert stack.stack is [2]
E assert [2] is [2]
E + where [2] = <Stack.Stack instance at 0x7f2273491560>.stack
test_stack.py:7: AssertionError
有人能告诉我怎么解决这个问题吗
您正在使用
is
执行标识检查(id
CPython中的内存位置),这永远不会相等,因为操作数是两个不同的列表(它们是可变对象),尽管它们具有相同的元素,您可以使用id
进行检查做公平测试:
相关问题 更多 >
编程相关推荐