Writing effective unit tests

Its ideal to maintain a benchmark for coverage within the code base. Coverage is measured by classes, functions and line covered. We should aim for > 80% coverage.

 

We currently use MVP as the design pattern which makes its easy to test and also better structured code overall.

 

Testing Tools available for Android

  1. JUnit (main testing framework in Java ecosystem)

  2. Mockito (used to mock objects)

  3. Powermock (extension of mockito, adds mocking static variables and functions )

  4. Roboelectric (provides android runtime on jvm for unit testing android dependancies)

  5. AndroidJUnit and Espresso (instrumented tests)


    Testing Tools available for OpenSRP server

  6. (1-3 ) above on the android section

  7. SpringJUnit4ClassRunner (Used to bootsrap spring IOC container for testing )


Important points to remember 

  1. Its important to consider the purpose and stengths of each tool for testing. Consider the tool on frameword from above list starting from the top.

  2. Dont mix powermock with Roboelectric or AndroidJUnit/Espresso. Tests will not work since powemock affects classloading

  3. Dont Prepare a class for Test using the powermock if the same class is under test. Jacoco will not be able to report coverage for such tests

  4. Write tests when writing the code 

  5. If (4) is not possible the write the code keeping in mind that it needs to be tested

  6. Mockito can be used will the other testing tools

  7. Avoid static methods if they need to have state or call instances of other classes which track state 

  8. Don't instantiate objects in a method that would be need to when testing that function works(tight coupling)



MVP design Pattern

  1. Model (includes Interactors) Use JUnit and Mockito

  2. Presenter

    1. If the presenter have any android dependencies then use Robolectric +Mockito

    2. If the presenter does not have any android dependencies use JUnit and Mockito

  3. Views Use Robolectric +Mockito

  4. Utils and Helpers (JUnit +Mockito)

 

Best practices

Unit tests should:

  1. Be Simple and contain as little logic as possible

  2. Clearly explain what behaviour they’re testing e.g with pattern [Method Under Test]_ [Scenario/Condition]_ [Expected Result]

  3. Make sure that frequently changing code is covered

  4. Focus on public functions , no private functions

  5. Cover all uses cases and edge cases of each function

  6. Not test code outside the scope of that test

 

Interpreting the coveralls report

The below is a report for the coveralls that is on the PR after coveralls runs successfully. The report is usually edited after new commits

 

From the above

19 of 520 (80.58%) changed or added relevant lines in 17 files are covered.

( This show % percentage of new code that has been tested) This should be used to benchmark if the PR has meet the requirement for the project

Overall coverage increased (+1.6%) to 60.157%

This shows the overall change to the test coverage

 

The above shows 3 main metrics

  • Covered Lines (Lines that have been tested in this PR)

  • Changed/Added Lines (The number of lines that was changed on the PR)

  • % (The % of lines that the class has been tested on this PR)

    On clicking each line above one is able to know which lines have not been tested. They will he highlighted in read and tagged as new as shown below



Testing with Robolectric