Use a Naming Convention

I almost omitted this. After all, everyone knows to use a naming convention, right? And everyone knows the benefits in terms of self-documenting code, maintainability, expression of intent, and so forth.

Then I remembered all the code bases I have seen with tests that have meaningless names such as test1 and test2, incomplete conventions such as testMethod indicating that this is a test of method but not telling what aspect of the method is being tested, and inconsistent conventions. So here goes.

Use a convention. Use your convention consistently. Make sure your convention captures everything that needs to be captured. Seriously consider using industry-standard conventions, whether explicit or de facto standards, if they exist to minimize ramp-up time and retraining for new hires. If the convention needs to change, make sure you have a plan for how and when to change the instances of the old convention. Enforce your convention with static checkers where reasonable.

For tests, the elements of a good naming convention may include the following items.

• A syntactic marker to distinguish tests from nontests. Common examples include affixing the class with Test, prefixing test methods with test, and metadata markers such as JUnit and TestNG @Test annotations or NUnit [Test] attributes.

• References to the symbol under test such as using the class under test as the base name to which to affix Test and using the method under test as a component of the test method name.

• Description of the conditions or variations that distinguish the particular test such as meaningful values or business-rule contexts.

• Delimiters to separate the various components of the names.

• Guidelines on how to apply the convention for specific nonobvious cases. For example, should the test for the constructor of class Class start with testClass or testConstructor?

My particular current convention for tests (see Listing 5-2) in Java using JUnit or TestNG follows.

• Suffix the class under test with Test for forming the test class name. I use it as a suffix because a) it is the more common convention, b) I am working with lots of code that is about testing systems and naturally starts with Test already, and c) it avoids confusion with the tests for other suffixing rules, such as those for exceptions.

• Prefix test methods with test. It is a bit of a holdover from prior to JUnit 4, but I like how it clearly delineates test from nontest methods independent of the annotations.

• Use the method under test as the base name for test methods.

• Use “_” as a separator between the base test name and the description of the test variant, if a description is included.

• Add a brief description of the test variant. The description can be omitted for unique and obvious “happy path” cases.

• Constructor tests use the name of the class.

• Attribute getter and setter pairs are tested together with a method name starting with testSetGet.

Listing 5-2: A brief demonstration of my current Java test-naming conventions in action. Note that the formatting is compressed for brevity.

class Payment {
  public Payment(Double amount) { ... }
  public void setAmount(Double amount) { ... }
  public Double getAmount() { ... }
  public PaymentTransmissionResult transmit() { ... }
}

class PaymentTest {
  @Test public void testPayment () { ... }
  @Test public void testPayment_ZeroAmount() { ... }
  @Test public void testSetGetAmount() { ... }
  @Test public void testTransmit(){ ... }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.71.106