Friday, April 8, 2011

What is the best unit test naming convention?

I'm not good at namming actually because I'm not an English native speaker. So I tried many best practices about naming the test methods and I met a few interesting strategies: - [Subject_Scenario_Result] or - [MethodName_StateUnderTest_ExpectedBehavior] And i found a very interesting way at: http://stevesmithblog.com/blog/unit-test-naming-convention/ For me I think those methods both have pros and cons. For the first one, in some complicated scenarios, my method name would look like: Edit_IfIdIsEmpty_ShouldCreateOrganisationAndReturnRedirectToRouteResult Sometime, when I want to write the expected behavior before the senario, I would write the name like: Edit_ShouldCreateOrganisationAndReturnRedirectToRouteResult_IfIdIsEmpty And time goes by, I have a lot of different styles of names and believe me, some of the names are very funny. I like ideas of the second method. When you run all your tests in the solution, the test result list will look beautiful and meanningful. But like the first method, in some complicated scenarioes, the name would be long and personally, I think it's hard to read and I'm not happy with the way he names the test class and test methods. Why don't we combine these methods and use this way: - Class name : ClassNameTests - Method name: Any_sentence_or_phrase_that_can_describe_the_intention_of_unit_test For example: BusinessServiceTests - Search_business_by_keyword_and_location_should_return_result_with_full_information UploadControllerTests - Should_throw_security_exception_if_file_type_is_not_supported I think using this way, we have the freedom of naming but still can provide the full intention of the unit test and the method name is very easy to read. Have a better idea? Please share it :D.

0 comments:

Post a Comment