Home > Software engineering >  Why is Python assertEqual telling me I'm missing the second argument in this unittest?
Why is Python assertEqual telling me I'm missing the second argument in this unittest?

Time:03-29

I'm currently testing the construction of some objects in Python, and I simply can't get past this infuriating error. Here is my code:

from unittest import TestCase 

class TestEquals(TestCase):
    def test_success(self):
        self.assertEqual("string","string")

def main():
    TestEquals.test_success(TestEquals)

if __name__ == "__main__":
    main()

(In my actual test I'm initiating an object to assert equality on, but I chose to boil it down to two identical strings, for abstraction) In my mind, just calling assertEqual on two identical strings shouldn't fail, yet it does.

Traceback (most recent call last):
  File "tests/ex.py", line 11, in <module>
    main()
  File "tests/ex.py", line 8, in main
    TestEquals.test_success(TestEquals)
  File "tests/ex.py", line 5, in test_success
    self.assertEqual("string","string")
TypeError: assertEqual() missing 1 required positional argument: 'second'

I get a TypeError saying I'm missing a second argument. Maybe I've gone insane, but to me it sure does look like I've included two arguments.

I'm dying to know whether I'm just being dumb in the way I'm using the function, or if I've encountered a rare bug in the unittest library.

Thanks in advance!

To be certain that the error is coming from how I'm using the assertEquals function, and not my class implementation, I tried boiling the error down as far as I could.

I tried the with test_success(self): syntax to no avail.

I tried providing the msg argument, but I still get the same result.

CodePudding user response:

No need to create main method. Use unittest's main method.

import unittest

class TestEquals(unittest.TestCase):
    def test_success(self):
        self.assertEqual("string","string")

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

Output:

.
----------------------------------------------------------------------
Ran 1 test in 0.009s

OK
  • Related