2016-09-21 125 views
1

我是Python的新手,我已經閱讀了Python的單元測試文檔,我正在做一些與提供的示例不同的東西。我正在比較套餐。我不知道爲什麼我的代碼不斷失敗。它看起來像代碼被正確寫入。有人可以採取一個甘德,看看他們是否可以解決這個問題?我將永遠是偉大的!Python單元測試細微的錯誤

我想在單元測試中變得更好,所以這就是爲什麼我要這個代碼。

import unittest 

def determineIntersections(listingData, carList): 
    listingDataKeys = [] 
    for key in listingData: 
     listingDataKeys.append(key) 

    carListKeys = [] 
    for car in carList: 
     carListKeys.append(car[0]) 

    intersection = set(carListKeys).intersection(set(listingDataKeys)) 
    difference = set(carListKeys).symmetric_difference(set(listingDataKeys)) 

    resultList = {'intersection' : intersection, 
        'difference' : difference} 
    return resultList 

class TestHarness(unittest.TestCase): 
    def test_determineIntersections(self): 
     listingData = {"a": "", "b": "", "c": "", "d": ""} 
     carList = {"a": "", "e": "", "f": ""} 
     results = determineIntersections(listingData, carList) 
     print results['intersection'] 
     print results['difference'] 

     self.assertEqual(len(results['intersection']), 1) 
     self.assertSetEqual(results['intersection'], set(["a"]) # offending code piece 
     self.assertEqual(results['difference'], set(["b", "c", "d", "e", "f"])) # offending code piece 

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

當我禁用「攻擊代碼塊」,即斷言該組比較,代碼行爲正確,但是當我使我的代碼得到以下輸出:

python UnitTester.py 
    File "UnitTester.py", line 39 
    if __name__ == '__main__': 
          ^
SyntaxError: invalid syntax 

任何想法不勝感激!謝謝。

+2

你有一個缺少的括號)在行的末尾。 –

回答

2

你只需在

self.assertSetEqual(results['intersection'], set(["a"]) 

末缺少一個括號,這混淆瞭解釋。將其更改爲

​​

一般情況下,你可能會試圖找到匹配括號中的那一個編輯器(或編輯器設置),或警告括號不匹配。

+0

非常感謝。我以爲我排除了這一點。我只是感到沮喪。非常感謝。 – nndhawan

+0

當然,這些事情發生。祝你好運。 –