TAGS :Viewed: 2 - Published at: a few seconds ago

[ Python: convention name for a test ]

Is there a convention for naming tests in Python when using the unittest module. I know that each method inside a class which inherits from unittest.TestCase should start with test, but I wonder what is much better:

1. A short descriptive name without docstring

def test_for_a_date_on_a_weekday(self):
        customer_type = "Regular"
        dates = ["16Mar2009(mon)"]
    self.assertEquals(self.hotel_reservation_system.find_cheapest_hotel(customer_type, dates), "Lakewood")

2. A number following the word test with a docstring which explains the test.

def test_1(self):
    """Tests the cheapest hotel when the date is on a weekday.
    """
    customer_type = "Regular"
    dates = ["16Mar2009(mon)"]
    self.assertEquals(self.hotel_reservation_system.find_cheapest_hotel(customer_type, dates), "Lakewood")

Which option is much preferable, and if any of them what should I use?

Answer 1


Generally it is preferable to increase readability by :
    - choosing an adequate name     
    - describing how it works

Choose your name such that it will be short and descriptive. For readability, use snake_case. For example : test_week_date.

Always include a docstring in your function. This will allow the reader to get all necessary information if the name isn't clear enough OR if he doesn't really understand what the method does / how she does it.

Conclusion : short and descriptive (snake_case) name with a docstring.