[파이썬] unittest assert 메서드 종류 및 사용법

Unit testing is an essential aspect of software development. It helps ensure that individual units of code, such as functions or classes, work correctly in isolation. The unittest module in Python provides a variety of assertion methods that we can use to validate the behavior of our code during testing.

In this blog post, we will explore different types of assert methods available in unittest and their usage.

1. assertEqual()

The assertEqual() method is used to check if two values are equal. If the values are not equal, this assertion will raise an exception, indicating a test failure.

import unittest

class MyTest(unittest.TestCase):
    def test_equal_numbers(self):
        result = 10 * 5
        expected = 50
        self.assertEqual(result, expected)

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

2. assertTrue()

The assertTrue() method is used to check if a given value is true or evaluates to true. If the value is false or evaluates to false, the assertion fails.

import unittest

class MyTest(unittest.TestCase):
    def test_positive_value(self):
        result = 10
        self.assertTrue(result > 0)

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

3. assertFalse()

The assertFalse() method is used to check if a given value is false or evaluates to false. If the value is true or evaluates to true, the assertion fails.

import unittest

class MyTest(unittest.TestCase):
    def test_negative_value(self):
        result = -10
        self.assertFalse(result > 0)

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

4. assertIn()

The assertIn() method is used to check if a specified value is present in a given iterable or sequence. If the value is not found, the assertion fails.

import unittest

class MyTest(unittest.TestCase):
    def test_value_in_list(self):
        my_list = [1, 2, 3, 4, 5]
        self.assertIn(3, my_list)

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

5. assertNotIn()

The assertNotIn() method is used to check if a specified value is not present in a given iterable or sequence. If the value is found, the assertion fails.

import unittest

class MyTest(unittest.TestCase):
    def test_value_not_in_list(self):
        my_list = [1, 2, 3, 4, 5]
        self.assertNotIn(6, my_list)

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

6. assertRaises()

The assertRaises() method is used to check if a specific exception is raised when executing a statement or invoking a callable. If the exception is not raised, the assertion fails.

import unittest

class MyTest(unittest.TestCase):
    def test_exception_raised(self):
        with self.assertRaises(ValueError):
            int('abc')

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

Conclusion

unittest provides a robust set of assertion methods that allow you to thoroughly test your code. By utilizing these methods, you can write tests that verify the correctness of your application’s units.

In this blog post, we covered some of the most commonly used assert methods, including assertEqual(), assertTrue(), assertFalse(), assertIn(), assertNotIn(), and assertRaises(). However, unittest provides many more assertion methods to cater to different testing scenarios.

Remember, writing effective unit tests is crucial for maintaining the quality and stability of your codebase. So make sure to utilize these assert methods to test your code thoroughly!