If you want to make your TDD sustainable, then please give importance to the readability of your tests. When a programmer reads your test, he or she needs to understand the purpose of test. No developer likes to stop and puzzle through a test to figure out what it does. You can reduce the cognitive load of your reader by making your tests readable. Here are some tips that you can use to improve the readability of your tests. I hope you will find them useful.
Tip 1: Give importance to test nameBy choosing a right name for your test you are giving the first clue to your reader about the intention your test and how the target object is supposed to behave. Try to select a name that says something about the scenario and the expected behaviour.
Tip 2: Structure your unit testTry to follow "Arrange, Act, Assert" pattern to structure your unit test. "Arrange, Act, Assert" basically means that you want to organize your tests such that you first arrange the objects used in the test, then trigger the action and make assertions about the outcome last. You can add whitespace in between these three segments to help others to understand your tests more easily. For example:
Tip 3: Put emphasize on "what" over "how"@Test public void shouldFindCustomerByUsername(){ //Arrange when(customerDaoMock.findByUsername("jonsmi")) .thenReturn(getFakeCustomer()); //Act Customer customer = customerService .findCustomerByUsername("jonsmi"); //Assert assertThat(customer.getId(), is(101L)); assertThat(customer.getUsername(), is("jonsmi")); }
Try to give importance on "what" over "how" even for your test code. Move out the unnecessary implementation details from your test code. This details create noise, which makes harder for your reader to understand what is important in your test. Also try to use Hamcrest matcher utilities such as assertThat, is, anything, notNullValue, hasItem etc. JUnit currently ships with Hamcrest. These utilities help you express your intent clearly and reduce verbosity from your tests. For instance, instead of doing this:
You can do this:assertTrue(activities.contains("PLAY")); assertTrue(activities.contains("READING")); assertTrue(activities.contains("WRITING"));
Tip 4: Extract common features into methods that can be sharedassertThat(activities, hasItems("PLAY", "READING", "WRITING"));
Many times we write the same thing again and again in our test methods. Remember DRY (Don't Repeat Yourself) principle. Extract common or nonessential features into private helpers and setup methods. But be careful not to make your tests so abstract that future readers do not understand what tests do.
Tip 5: A test should only check one thing and check it wellWhen you put multiple tests in a single test method, you are going to confuse others. If you have a big test method, split it into smaller test methods. Each test should focus on single fixture. By doing this, you will improve readability significantly. Another additional benefit you will get, when a test fails you need to look into smaller portion of code to find the reason. So it improves maintainability as well.
Tip 6: Try to avoid magic numbersI think every programmer agrees that magic numbers are bad and should be avoided. Replace them with constants or variables that give them desired meaning, making code easier to read.
Tip 7: Simplify your setup methodDo not dump everything in your setup method (annotated with @Before or @BeforeClass). By doing this, you are making setup method over complicated. It is also an indication of design problem that forces the test to do so much work to put an object under test. Extract all the nonessential details from the setup into private methods. Give appropriate and descriptive names to variables and methods used in setup method.
References:
No comments:
Post a Comment