Python unittest改变变量看似超出范围

问题描述 投票:0回答:1

我有以下Tic-Tac-Toe代码:

class TicTacToeBoard:
board = [[0,0,0], [0,0,0], [0,0,0]]

def printBoard(self):
    print(self.board)

def putPiece(self, position1, position2):
    self.board[position1][position2] = 1

我是单元测试代码,如下所示:

def testestablishTicTacToeBoard(self):
    test = TicTacToeBoard()
    self.assertEqual(test.board, [[0,0,0],[0,0,0],[0,0,0]])

def testPutPiece(self):
    test = TicTacToeBoard()
    self.assertEqual(test.board, [[0,0,0],[0,0,0],[0,0,0]])
    test.putPiece(1,1)
    self.assertEqual(test.board, [[0,0,0],[0,1,0],[0,0,0]])

在我进行第二次单元测试之前,第一次单元测试没有问题。一旦我进行了第二次单元测试,第一次单元测试不再通过:

self.assertEqual(test.board, [[0,0,0],[0,0,0],[0,0,0]])
AssertionError: Lists differ: [[0, 0, 0], [0, 1, 0], [0, 0, 0]] != [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

在第二次单元测试中发生了什么导致第一次不再通过?

python python-unittest
1个回答
2
投票

它看起来像是因为董事会在所有实例之间共享(它是一个类属性,而不是实例属性)。

您可以使用此更改类代码,它应该工作:

class TicTacToeBoard:

    def __init__(self): 
        # Now each instance (self) has its own board:     
        self.board = [[0,0,0], [0,0,0], [0,0,0]]

    def printBoard(self):
        print(self.board)

    def putPiece(self, position1, position2):
        self.board[position1][position2] = 1

如果你想说服自己真正发生了什么,你可以比较两个版本(实例/类属性):

class TicTacToeBoard:

    shared_board = [[0,0,0], [0,0,0], [0,0,0]]

    def __init__(self):
        self.board = [[0,0,0], [0,0,0], [0,0,0]]

    def __str__(self):
        return (
            f"Instance board: {self.board}\n"
            f"Class board (shared): {self.shared_board}" 
        )

    def putPiece(self, position1, position2):
        self.board[position1][position2] = 1
        self.shared_board[position1][position2] = 2
>>> b1 = TicTacToeBoard()
>>> b2 = TicTacToeBoard()
>>> b1.putPiece(1, 2)
>>> print(b1)

Instance board: [[0, 0, 0], [0, 0, 1], [0, 0, 0]]
Class board (shared): [[0, 0, 0], [0, 0, 2], [0, 0, 0]]

>>> print(b2)

Instance board: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Class board (shared): [[0, 0, 0], [0, 0, 2], [0, 0, 0]]

请注意第二个tictactoe板b2如何在(1, 2)位置没有,但在(1, 2)位置有两个。

© www.soinside.com 2019 - 2024. All rights reserved.