2016-09-21 59 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

当然,这些事情发生。祝你好运。 –