Python unittest fails when it shouldn't
Question: I ran unit tests in the file below, and one of test cases failed, where it
should not have failed. I got an unexpected result - Assertion error, where in
TestFormatInitMethodArgs I intended to test if `'"' == '"'`, but it tested for
`'"' == None` \- it looks like test in the second testcase checks for equality
from not its own setUp():

    
    
    #!/usr/bin/env python
    
    import csv
    import unittest
    
    class Format:
        def __init__(self, file_path, header=False, flag='r', delimiter=',', quote_char=None):
            self.file_path = file_path
            self.header = header
            self.flag = flag
            self.delimiter = delimiter
            self.quote_char = None
    
    class TestFormatInitMethodDefaults(unittest.TestCase):
        def setUp(self):
            self.file_path = 'C:/Privatus/eurusd.csv'
            self.header = False
            self.flag = 'r'
            self.delimiter = ','
            self.quote_char = None
    
        def test_attributes(self):
            f = Format('C:/Privatus/eurusd.csv')
            self.assertEqual(self.file_path, f.file_path)
            self.assertEqual(self.header, f.header)
            self.assertEqual(self.flag, f.flag)
            self.assertEqual(self.delimiter, f.delimiter)
            self.assertEqual(self.quote_char, f.quote_char)
    
    class TestFormatInitMethodArgs(unittest.TestCase):
    
        def setUp(self):
            self.file_path = 'C:/Privatus/eurusd.csv'
            self.header = True
            self.flag = 'rb'
            self.delimiter = ';'
            self.quote_char = '"'
    
        def test_args(self):
            a = Format('C:/Privatus/eurusd.csv', header=True, flag='rb', delimiter=';', quote_char='"')
            self.assertEqual(self.file_path, a.file_path)
            self.assertEqual(self.header, a.header)
            self.assertEqual(self.flag, a.flag)
            self.assertEqual(self.delimiter, a.delimiter)
            self.assertEqual(self.quote_char, a.quote_char)
    
    if __name__ == '__main__':
        unittest.main()
    

Test results:

    
    
    F.
    ======================================================================
    FAIL: test_args (__main__.TestFormatInitMethodArgs)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "C:\Privatus\repos\working\data.py", line 45, in test_args
        self.assertEqual(self.quote_char, a.quote_char)
    AssertionError: '"' != None
    
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s
    
    FAILED (failures=1)
    

What am I doing wrong?


Answer: You do not seem to set the quote_char attribute in your constructor (**init**)

Try self.quote_char = quote_char instead of self.quote_char = None

