4

In my setUpClass I would like to create a resource in the database one time, which is then used for all of the tests in the class.

After I create the resource in setUpClass, I would like to perform assertions on it right then and there. However, I'm not sure how to call assertions in setUpClass, given that all of the assertion functions are instance methods, not class methods.

import unittest

class TestFoo(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.foo = cls.make_foo(name='bar')
        # How would I assert that cls.foo['name'] == 'bar'?
        # The following does not work, as the assertEquals function is not a class method
        # cls.assertEquals(cls.foo['name'], 'bar')

    @classmethod
    def make_foo(cls, name):
        # Calls a POST API to create a resource in the database
        return {'name': name}

    def test_foo_0(self):
        # Do something with cls.foo
        pass

    def test_foo_1(self):
        # do something else with cls.foo
        pass
       

The only alternative I can think of is to raise an exception in setUpClass:

    @classmethod
    def setUpClass(cls):
        cls.foo = cls.make_foo(name='bar')
        if cls.foo['name'] != 'bar':
            raise Exception("Failed to create resource, cannot do the tests")

Of course, I do not want to call the assertions from each test, as this will just duplicate the code.


Edit: I don't think this workaround is good, because the failure message will point to the self.assertFalse(self.flag) line, instead of the if cls.foo['name'] ~= 'bar' line. In addition, if you created multiple resources, this would need multiple flags to disambiguate.

    flag=False
    
    @classmethod
    def setUpClass(cls):
        cls.foo = cls.make_foo(name='bar')
        if cls.foo['name'] != 'bar':
            cls.flag=True

    def setUp(self):
        self.assertFalse(self.flag)

3
  • Does this answer your question? How to fail a python unittest in setUpClass? Commented Apr 11, 2021 at 17:17
  • @quamrana No, the linked question is about how to fail a test from setUpClass. My question is how to call assertion functions from setUpClass. While a similar technique could be used (capturing a flag in the cls method and then asserting the flag in setUp), it is not ideal because the log message will just tell you that the flag asserted incorrectly, instead of the actual value you would like to assert. Commented Apr 11, 2021 at 17:22
  • what's wrong with creating a mixin that you give to your other classes to use Commented Apr 11, 2021 at 19:36

1 Answer 1

0

I got something like this to work in my code using a singleton instance. I hope this resembles what you are looking for. I did not test your code because there is some API call involved (allegedly), but a different version of this worked on my computer.

import unittest

class TestFoo(unittest.TestCase):

    # Singletons have a use after-all
   _instance = None

   def __new__(cls, *args, **kwargs):
       if not cls._instance:
           cls._instance = super().__new__(cls)
       return cls._instance

    @classmethod
    def setUpClass(cls):
        cls.foo = cls.make_foo(name='bar')
        # How would I assert that cls.foo['name'] == 'bar'?
        # The following does not work, as the assertEquals function is not a class method
        # cls.assertEquals(cls.foo['name'], 'bar')
        cls._instance.assertEquals(cls.foo['name'], 'bar')

    @classmethod
    def make_foo(cls, name):
        # Calls a POST API to create a resource in the database
        return {'name': name}

    def test_foo_0(self):
        # Do something with cls.foo
        pass

    def test_foo_1(self):
        # do something else with cls.foo
        pass

P.S. If somebody else would like to test this code and subsequently clean up my answer, I would be grateful to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.