Skip to main content

Simple code: Unit tests

Unit tests are the developers number one safety net. Let that sink in. This is the number one reason for writing unit tests.

Unit tests are written by developers for developers to ensure that the code works as expected and handles happy and sad paths correctly. With enough unit test coverage the tests enable a safe environment for refactoring and rewriting code.

Unit test scope

Unit test should test a single thing, a method or function call and it should test only one use case within. In other words a unit test should test a function with a single input. This is a important guideline to understand. When a unit test tests a function with single input it makes the test isolated, repeatable and predictable.

Example of good tests:

fun findsAddress() {
  val address = findAddress("Stevens street 35", "Southport", "Australia")

fun doesNotFindAddress() {
  val address = findAddress("Stevens street 697", "Southport", "Australia")

If a test contains multiple inputs for a single function it's not anymore isolated. Say a test has seven different inputs and it calls the testable function seven times but with the third input it fails. Because it fails with the third input the remaining four inputs are not tested. Now you have in your hands a test that is proven to work for the first two inputs and fail for the third but you have no  idea whether it works with the remaining four inputs or not. When you start to fix the implementation your safety net can fail for seven different inputs and if any of them fails you can never be sure if the remaining inputs work or not.

Example of a bad test:

fun findAddress() {
  val validAddress = findAddress("Stevens street 35", "Southport", "Australia")

  val invalidAddress = findAddress("Stevens street 697", "Southport", "Australia")

Writing a separate test case for each input surely adds the number of lines of code but it's not a bad thing when it also gives a better safety net when the code needs to be changed.

Exception to the rule

Of course there's a exception to the rule and in this case there are at least two, first one is called parameterized tests and second one is called property based testing.

Example of a parameterized test:

@ValueSource(ints = {1, 35, 50})
fun findsAddresses(streetNumber: Int) {
  val address = findAddress("Stevens street $streetNumber", "Southport", "Australia")

Test logic and logic in tests

Test should be testing the logic of the implementation, they should not introduce any logic itself.

I've seen multiple times a test suite that has a bunch of logic in it, evaluations and conditionals that either manipulate the inputs, choose what functions to call or choose what assertions should be evaluated.
The problem with this is that the logic in tests itself introduces logic with various inputs and outputs and edge cases that themselves can introduce unexpected behaviour and the logic of the tests is not itself tested and verified in any way.

Avoid introducing logic just for tests. It adds complexity and possibility of invalid test functionality. Keep the unit tests as simple as possible.

Writing unit tests

I do try to write tests with TDD approach but I don't find it natural in all situations and in those situations I mix it up and write some of tests after I've written the initial implementation. I personally don't have a strong opinion on when and how you should write your tests as long as the tests are written and they cover the expected use cases and exception cases before the code ends up in trunk/main. Experiment and find a way to write tests that suits you.

Next part

In the next part I'll be writing about another testing subject, integration testing.


Popular posts from this blog

Simple code: Immutability

Immutability is a special thing that in my mind deserves a short explanation and praise. If you're familiar with functional programming you surely recognice the concept of immutability because it's a key ingredient of the paradigm. In the world of object oriented programming it's not as used and as easy to use approach but there are ways to incorporate immutability to parts of the code and I strongly suggest you to do so. Quick intro to immutablity The basic idea of immutability is unchangeable data.  Lets take a example. We have a need to modify a object's property but because the object is immutable we can't just change value but instead we make a copy of the object and while making the copy we provide the new value for the copy. In code it looks something like this. val pencil = Product(name = "Pencil", category = "Office supply") val blackMarker = pencil.copy(name = "Black marker") The same idea can be applied in functions and metho

Simple code: Contracts

Code works around contracts and contracts should be carefully thought and crafted. What are contracts A High abstraction level of contracts for code are API's. They define a interface that is basically a contract that the producer and consumer of the API agree to use to communicate with each other. Two common forms of API's are libraries that are used in code and external API's  that are used via HTTP, RPC etc. When thinking in a bit deeper contracts consist firstly of functions, methods or external endpoints and secondly of data, more precisely on data models and data types within the models.   Defining contracts Contracts should always be defined with careful thought. I've come accross few times to someone saying that "this is for intenal use only so it doesn't need to defined and/or documented as thoughtfully as a public API would be" but I disagree with that. The same care should be be given to internal and external contracts because the contracts are