PolarisProject / salesforceStyleGuide

Code style guide for Salesforce
GNU General Public License v2.0
26 stars 20 forks source link

Naming of Test Classes #2

Open brianmfear opened 9 years ago

brianmfear commented 9 years ago

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.

ckoppelman commented 9 years ago

XTest might be a better choice to keep it out of production code, but I think that's obscure.

I'm surprised that the Force.com IDE doesn't support Ctrl+Shift+T => Open Type dialog that Eclipse for Java supports (allowing you to type TAC for TEST_AccountContoller). MavensMate supports something equivalent, as do most other editors.

brianmfear commented 9 years ago

I just tried Ctrl+Shift+T, and it didn't seem to work. The dialog opens, but I don't get any results. I do use the Developer Console's Ctrl+Shift+O, which works in a similar manner, however, and using "TestClassName" would let me find tests using just "Test" as a search term.

Speaking from experience, though, it seems like there will inevitably be a single class that needs to start with the word Test that isn't actually a test class, and then it will get buried in between all the test classes. Usually some sort of utility class for unit testing (like creating users, accounts, etc with consistent fields). I've just seen it happen frequently enough that I'd rather not recommend it. And I definitely recommend a TestData or TestUtility type class that does contain common constructs. I think it's more beneficial to simply be consistent with names though, so if an established convention already exists, it should be followed.