Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AssertEquals in a static method in python

I am learning to code in Python and have hit a bit of a wall. I am trying to create a static method to reuse in a series of tests, and I cannot seem to get a unit test to work within the method. Before moving to the static method the test functioned as below:

self.assertEqual(api_object.status_code, 200, "\nNot getting a 200\n")

within the static method I have tried several possible solutions, but with little success. I have confirmed that the value being passed in is a definite match. A quick summary of variations and failures(and my debug process). Sorry I cannot provide more code, work sensitive stuff:

assertEqual(api_object.status_code, 200, "\nNot getting a 200\n")

error: global name assertEqual is undefined. To rectify I tried:

TestCase.assertEqual(api_object.status_code, 200, "Not getting a 200 back")

TypeError: unbound method assertEqual() must be called with TestCase instance as first argument (got int instance instead). So I tried:

TestCase.assertEqual(TestCase, api_object.status_code, 200, "Not getting a 200 back")

TypeError: unbound method assertEqual() must be called with TestCase instance as first argument (got type instance instead).

At this point I'm a bit stumped. I am using the Django framework. Thanks in advance for the help.

like image 461
Driver Avatar asked Sep 19 '25 02:09

Driver


1 Answers

The big question here is why you are using static methods in the first place.

Don't try to use methods from TestCase here. You could just use assert here:

assert api_object.status_code == 200, "Not getting a 200 back"

You can't call TestCase.assertEqual() without an actual TestCase instance in any case as the method expects to be able to call other methods on self.

like image 128
Martijn Pieters Avatar answered Sep 20 '25 16:09

Martijn Pieters