Skip to content

Naming of Test Classes #2

@brianmfear

Description

@brianmfear

While I definitely agree that test classes should mimic the name of the class it tests, I disagree that the name should start with TEST_. There is a simple reason for this: You cannot easily find a test for a particular class in any system you want to use.

In Salesforce, Setup > Develop > Apex Classes lets you use list views, including the Rolodex feature. Let's say I have the classes: AccountController, LeadController, TEST_AccountController, and TEST_LeadController. To find my unit tests, I can now press "T", which is pretty cool, but to find AccountController or LeadController, I now have to use different letters. Also, in the Force.com IDE, you can select a file by typing. So, to find the TEST_AccountController, I need to type "TEST_A" (at minimum) to find the class I'm looking for.

Conversely, let's rename the classes: AccountController, AccountControllerTest, LeadController, LeadControllerTest. Now, when I press "A", I get just the account controller and its unit tests, and when I press "L", I get just the lead controller and its unit tests. Once you have about 200 live classes, you'll come to appreciate not having scroll through pages of TEST_X classes. In the Force.com IDE, I only need to type Acc (probably), and scroll down exactly twice to find my controller's test.

However, I would allow for a single exception: classes that test entire functional units (crossing multiple classes, triggers, etc). Those should all be lumped together to make them easy to find, so starting with a prefix completely makes sense in that scenario (if you use it at all). I'd probably avoid using "TEST" for this prefix, though, because T is fairly common letter. I'd probably recommend they start with a more obscure letter, like X: "XTEST_ClassName". Some naming convention that developers can know they need to visit for a certain class of tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions