nndhawan
nndhawan

Reputation: 617

Python Unit Test subtle bug

I'm new to Python and I've read the unit test documentation for python and I'm doing something different from the examples provided. I'm comparing sets. I don't know why my code keeps getting a failure. It seems like the code is written correctly. Can someone take a gander and see if they can resolve the problem? I will be eternally greatful!!

I'm trying to get better at unit testing so that's why I'm working this code out.

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()

When I disable the "offending code piece" that is the asserts for the set comparison, the code behaves correctly but when I enable the code I get the following output:

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

Any ideas greatly appreciated! Thanks.

Upvotes: 1

Views: 185

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76406

You're simply missing a parentheses at the end of

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

This confuses the interpreter. Change it to

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

In general, you might try to find an editor (or editor settings) that match parentheses, or warn about unmatched parentheses.

Upvotes: 2

Related Questions