Skip to content

Testing

Alexander Yevsyukov edited this page Aug 20, 2024 · 4 revisions

Use Kotlin for writing tests

  • Backticked function names are faster to write than @DisplayName("blah-blah-blah") following void blahBlahBlah() in Java.
  • Infix assertions from Kotest are faster to write and read.

Tests already written in Java are gradually migrated to Kotlin.

We do use Java for tests when we need to test Java-specific API.

Naming a test suite class

Please use the pattern <SubjectClassName>Spec. We use the Spec suffix to highlight the fact that tests are real and actionable specification of the code.

Make the test suite class internal

If it's not an abstract base to be used from other modules, do make the test suite class internal.

Add @DisplayName("...") before the test suite

We use the annotation instead of a backticked class name for the following reasons:

  1. It's easier to search in IDE.
  2. Tests and production code come close in the alphabetical order.
  3. The value of annotation as a string gives you more options in writing, including backticking the name of the test subject.
  4. It's easier to reference a test suite in writing if it MyClassSpec rather than 'MyClass' should.

Backtick test subject name

It should look like this:

@DisplayName("`MyClass` should")
internal class MyClassSpec {
  // ...
}

Use special wrapping for nested classes

Do you backicked names for nested classes with the following wrapping:

@DisplayName("`Type` extensions should")
internal class TypeExtsSpec {

    @Nested inner class
    `Obtain simple type name` {
       // ...
    }
}

This way it's easier to read the goal of the nested class when scanning the code from top to bottom.

Start the text of the name with a capital letter to help distinguish nested classes from test functions in the list of test results.

Clone this wiki locally