全局名称checkRows未在Sudoku Solver中定义

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

我的测试有一些问题。我不确定为什么我在错误后不断收到错误。我是新手使用课程。

正在测试的文件。

class Sudoku_Checker:
  def __init__(self,board):
    self.board = board

  def board_validater(self):
    checkRows(self.board)
    checkCols(self.board)
    checkSquares(self.board)

    return checkRows() == True and checkCols() == True and checkSquares() == True

  def checkRows(self):
      # compare = [1,2,3,4,5,6,7,8,9]
      # for i in self.board:
      #     if i.sort() == compare:
      #         continue
      #     else:
      #         return False
      return True

  def checkCols(self):
      return False

  def checkSquares(self):
      return True
# s = Sudoku_Checker()
# s.board_validater([
#   [5, 3, 4, 6, 7, 8, 9, 1, 2],
#   [6, 7, 2, 1, 9, 0, 3, 4, 8],
#   [1, 0, 0, 3, 4, 2, 5, 6, 0],
#   [8, 5, 9, 7, 6, 1, 0, 2, 0],
#   [4, 2, 6, 8, 5, 3, 7, 9, 1],
#   [7, 1, 3, 9, 2, 4, 8, 5, 6],
#   [9, 0, 1, 5, 3, 7, 2, 1, 4],
#   [2, 8, 7, 4, 1, 9, 6, 3, 5],
#   [3, 0, 0, 4, 8, 1, 1, 7, 9]
# ])

这是测试文件。

import unittest
from ValidSudoku import *

class TestSum(unittest.TestCase):
    def testwillWork(self):
        """
        Check to return True
        """
        grid = [  [5, 3, 4, 6, 7, 8, 9, 1, 2],
          [6, 7, 2, 1, 9, 5, 3, 4, 8],
          [1, 9, 8, 3, 4, 2, 5, 6, 7],
          [8, 5, 9, 7, 6, 1, 4, 2, 3],
          [4, 2, 6, 8, 5, 3, 7, 9, 1],
          [7, 1, 3, 9, 2, 4, 8, 5, 6],
          [9, 6, 1, 5, 3, 7, 2, 8, 4],
          [2, 8, 7, 4, 1, 9, 6, 3, 5],
          [3, 4, 5, 2, 8, 6, 1, 7, 9]]
        checker_for_only_this_grid = Sudoku_Checker(grid)
        self.assertTrue(checker_for_only_this_grid.board_validater())

if __name__ == '__main__':
    unittest.main()

请给我一些提示,谢谢。我不确定我是否以正确的方式组织我的代码。我只想在开始编码之前编写基本测试。

python unit-testing
1个回答
1
投票

将类中的函数定义为成员函数时,例如

class blah:
    ...

    def myfunc(self, args):
        pass

    def newfunc(self):
        # self is how other members of the instance are passed in, hence
        self.myfunc(args) # You must invoke it this way inside your class.

课外:

bla = blah() # instance of your class.
bla.myfunc(args) # your function called.
© www.soinside.com 2019 - 2024. All rights reserved.