簡體   English   中英

從觀察為真時,AssertEqual返回False

[英]AssertEqual returning False when, from Observation is True

我目前正在為考試做准備,教授給出了一個測試腳本。 問題與數獨游戲有關。 在本節中,我必須將一組值的所有非零值作為一組返回到Sudoku表(由2D數組表示)。

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在明確為真時會返回false?

該函數的返回值與[6] ,因此AssertEqual返回false

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

不等於[6]

好吧,所以我聯系了我的教授,很顯然,測試代碼是不正確的,它是要反轉列表,以便列表中唯一的數字是該行中省略的數字。 我們還好

這可能是由於數據類型錯誤引起的。 當sudoku1是一個列表並包含列表時,set([6])將給出一個type()集合

[6]==set([6])

將返回False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM