Search code examples
pythonunit-testingconsolesequences

Python - Sequence not equal, but it is what I am expecting


def parse(self, input):
    input = input.replace("\n", "").replace(" ", "")
    bits = input.split("=>")
    return bits[:-1]

Given "a => \nb => \nc=> " the output in the console is ["a", "b", "c"] which is exactly what I want. I guess the console must be formatting the output, but I cannot explain why my tests are failing.

My unit test fails, stating that the result is [["a", "b", "c"]] when I perform assertEqual using unittest.

Can anyone explain? I'm pretty new to Python in the sense I've not touched it for a few years, and even then it was pretty limited experience.

Test code

subject = InputParser()
self.assertEqual(subject.parse("a =>\nb => "), ["a", "b"])

Cheers


Solution

  • I just tested this using the module below, and my test passes just fine:

    import unittest
    
    class InputParser:
        def parse(self, input):
            input = input.replace("\n", "").replace(" ", "")
            bits = input.split("=>")
            return bits[:-1]
    
    class InputParserTest ( unittest.TestCase ):
        def test_parse ( self ):
            subject = InputParser()
            self.assertEqual(subject.parse("a =>\nb => "), ["a", "b"])
    
    if __name__ == '__main__':
        unittest.main()