Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading