[파이썬] unittest setUpClass 및 tearDownClass 활용

In the world of software development, unit testing plays a crucial role in ensuring the robustness and reliability of our code. Python provides a powerful built-in testing framework called unittest, which allows us to write and execute automated tests for our code.

Within the unittest module, there are certain methods that can be used to set up and tear down the necessary resources for our tests. Two of these methods are setUpClass and tearDownClass, which are executed once per test class.

setUpClass method

The setUpClass method is used to set up any necessary resources or perform any required actions before executing the test cases within a test class. It is executed only once per test class, before any test method is run.

Here’s an example of how the setUpClass method can be used:

import unittest

class MyTestClass(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # Set up necessary resources or perform actions
        cls.db = DatabaseConnection()
        print("Set up class")

    def test_something(self):
        # Test something using the set up resources
        result = self.db.query("SELECT * FROM users")
        self.assertEqual(len(result), 5)

    def test_another_thing(self):
        # Test another thing using the set up resources
        result = self.db.query("SELECT * FROM products")
        self.assertEqual(len(result), 10)

    @classmethod
    def tearDownClass(cls):
        # Clean up any resources or perform actions
        cls.db.close_connection()
        print("Tear down class")

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

In the above example, we define a setUpClass method using the @classmethod decorator. In this method, we can set up any necessary resources, such as establishing a database connection, initializing objects, loading necessary data, etc. This method will be executed only once before any test methods are called within the test class.

tearDownClass method

The tearDownClass method is the counterpart to setUpClass. It is used to tear down or clean up any resources that were set up in the setUpClass method. This method is executed once per test class, after all the test methods have been executed.

Here’s an example of how the tearDownClass method can be used:

import unittest

class MyTestClass(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # Set up necessary resources or perform actions
        cls.db = DatabaseConnection()
        print("Set up class")

    def test_something(self):
        # Test something using the set up resources
        result = self.db.query("SELECT * FROM users")
        self.assertEqual(len(result), 5)

    def test_another_thing(self):
        # Test another thing using the set up resources
        result = self.db.query("SELECT * FROM products")
        self.assertEqual(len(result), 10)

    @classmethod
    def tearDownClass(cls):
        # Clean up any resources or perform actions
        cls.db.close_connection()
        print("Tear down class")

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

In this example, we define a tearDownClass method using the @classmethod decorator. In this method, we can perform any necessary cleanup actions, such as closing database connections, deleting temporary files, etc. This method will be executed only once after all the test methods have been called within the test class.

Using the setUpClass and tearDownClass methods can help save time and resources by setting up and tearing down resources only once per test class instead of for each individual test method.

That’s it! Now you have a better understanding of how to use the setUpClass and tearDownClass methods in the Python unittest framework to set up and tear down the required resources for your tests. Happy testing!