I am new to unit testing, but finally getting started.
I have been running into a situation where my unit test names apparently grow too long to readable due to the multiple parameters and combinations of them. For example, consider these hypothetical test case names resembling my actual test case names:
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsFinanceAndDesignationIsManager_AccessAllowed
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsFinanceAndDesignationIsAnalyst_AccessDenied
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsFinanceAndDesignationIsTemp_AccessDenied
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsHrAndDesignationIsManager_AccessAllowed
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsHrAndCorpConselor_AccessDenied
GetAllEmployeeRecords_UserBelongsToHeadQuarterAndDeptIsHrAndDesignationIsTemp_AccessDenied
GetAllEmployeeRecords_UserDoesNotBelongToHeadQuarter_AccessDenied
I feel that the UserBelongsToHeadQuarter phrase in the names is redundant for the 6 out of 7 test cases but it is also important to distinguish the 6 tests from the 7th test.
Using a [TestCategory] might work but my team members have concerns that failing test case might have difficulty communicating what exact scenario is failing on test execution reports and/or the [TestCategory] is more suitable for breaking down the tests by component or feature.
Is there a better way to organize or name these for more readability?
I looked up unit test practices at Microsoft page or in NodaTime code base but I don't find any longer names.
_AllowFinanceManager
would likely work just as well. Of course, data driven approaches as suggested by Ewan make it completely unnecessary to come up with names for what are effectively just different instances of the same test case.GetAllEmployeeRecords
method into dedicated file, then name of the method can be dropped from the test name. Data driven approach will save space and time to understand/read test cases.GetAllEmployeeRecords { UserBelongsToHeadQuarter { ... } , UserDoesNotBelongToHeadQuarter { ⇒access_denied() ... } }
. Each step in the process is a new level; each parameter is a new element inside a level