【问题标题】:AssertEqual returning False when, from Observation is TrueAssertEqual 返回 False 时,从 Observation 为 True
【发布时间】:2017-08-12 22:22:37
【问题描述】:

我目前正在复习考试,有教授给出的试卷。 问题涉及游戏 Sodoku;在本节中,我必须将一行值的所有非零值作为一个集合返回到数独表(由二维数组表示)中。

def get_values_from_row(puzzle, row): 
    rowVal = []
    try:
        for i in puzzle[row]:
            if i != 0:
                rowVal.append(i)
    except IndexError: 
        print('Invalid Row')
    if len(rowVal) == 0: 
        return rowVal
    else: 
        rowVal = set(rowVal) 
        print(rowVal)
        return(rowVal)

这里是数独板

sudoku1 = [[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]]

当我为第 0 行运行函数时,我得到了预期的 {1,2,3,4,5,6,7,8,9}。但是,当我运行测试脚本时返回失败。

这是测试脚本中的相关代码:

import unittest


class Test(unittest.TestCase):

    def test_get_values_from_row(self):
        sudoku1 = [
               [5, 3, 4, 0, 7, 8, 9, 1, 2],
               [6, 7, 0, 0, 9, 5, 0, 4, 8],
               [1, 9, 8, 0, 4, 0, 5, 6, 7],
               [8, 5, 9, 7, 6, 1, 4, 2, 3],
               [4, 2, 6, 8, 5, 3, 7, 9, 1],
               [7, 1, 3, 0, 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]]

       self.assertEqual(sg.get_values_from_row(sudoku1, 0), set([6]))

sg 正是我正在编辑的脚本导入的内容。当我查看日志时,显然6 不在集合中,当我将其更改为3 时,同样的事情发生了。看起来无论测试值是什么,它都会从我返回的列表中删除

Traceback (most recent call last):'
File "question_1_iii_test.py", line 23, in test_get_values_from_row'
self.assertEqual(sg.get_values_from_row(sudoku1, 0), set([6]))'
AssertionError: Items in the first set but not the second:'
1
2
3
4
5
7
8
9
Items in the second set but not the first:
6

我的问题是: 为什么 AssertEqual 明显为真时返回假?

【问题讨论】:

  • 我认为你的逻辑是倒退的,你实际上是在问{1, 2, 3, 4, 5, 7, 8, 9} == {6},显然是False。你想检查集合是不相交的吗?或者你想知道它是否是一个子集?
  • 好吧,我没有写测试脚本;这就是我不明白 AssertEqual 到底想做什么的问题;如果它说{1, 2, 3, 4, 5, 7, 8, 9} == {6},那么测试脚本一定有问题吧?因为当问题要求返回一整套值而不仅仅是一个值时,那会测试什么
  • 您确定 get_values_from_row() 应该返回使用的值而不是未使用的值吗 - 测试似乎暗示它应该返回未使用的值。
  • The function should return a list (as a set built-in type) containing all non-zero values in that row, an empty list if the row is empty (i.e. contains only zeros). 是准确的措辞

标签: python unit-testing equality sudoku


【解决方案1】:

函数的返回值与[6]不一样,所以AssertEqual返回false

sg.get_values_from_row(sudoku1, 0) = [5, 3, 4, 7, 8, 9, 1, 2]

不等于[6]

【讨论】:

  • 但是为什么只在测试脚本的返回值中省略了6?
  • 第一行 (0) 中没有 6[5, 3, 4, 0, 7, 8, 9, 1, 2],
  • 那么如果 6 不在行中,我为什么要测试它呢?这可能是某种反向测试吗?
  • 好吧。我认为您错过了第一排 sodoku1 行(0)` [5, 3, 4, 0, 7, 8, 9, 1, 2],`中的一些元素。这一行有一个 6。所以我认为你需要测试 6 是否在集合中。
【解决方案2】:

好的,所以我联系了我的教授,显然,测试代码不正确,这意味着要反转列表,以便列表中唯一的数字是行中省略的数字,所以在某种程度上;我们都很好

【讨论】:

    【解决方案3】:

    这可能是由于数据类型错误。 set([6]) 将给出一个 type() 集合,而 sudoku1 是一个列表并包含列表

    [6]==set([6])
    

    将返回 False

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 2015-10-03
      • 2021-07-02
      • 2013-09-18
      • 2011-05-22
      • 2012-11-11
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多