[파이썬] unittest 테스트 케이스 네이밍 규칙

When writing unit tests in Python using the unittest framework, it is important to follow a consistent naming convention for your test cases. This makes it easier to understand and organize your tests, especially when you have a large number of them.

1. Naming Style

The unittest framework follows the CamelCase naming convention for test case names. Each word in the test case name should start with an uppercase letter, and there should be no underscores between words.

2. Descriptive Names

A good test case name is descriptive and explains what is being tested. It should accurately describe the behavior or purpose of the code being tested. Avoid using generic names like “test_case_1” or “test_function”.

For example, if you are testing a function that calculates the total price of items in a shopping cart, a good test case name would be test_calculate_total_price. This clearly indicates what is being tested.

3. Prefixes

To further organize and categorize your test cases, you can use prefixes in the test case names. This helps to group related tests together.

Here are some commonly used prefixes:

4. Example

Let’s consider a simple example to illustrate the naming convention:

import unittest

class CalculatorTest(unittest.TestCase):
    
    def test_addition(self):
        # Test case for addition
        pass
    
    def test_subtraction(self):
        # Test case for subtraction
        pass
    
    def test_multiply(self):
        # Test case for multiplication
        pass
    
    def test_divide(self):
        # Test case for division
        pass

In this example, the test cases are named using the CamelCase convention, starting with the prefix test_. Each test case name clearly indicates the operation being tested.

Following a consistent naming convention for your test cases will help you and other developers easily understand and maintain your tests. It promotes readability and ensures a standardized approach to test case naming in your Python projects.