Appendix B. testing: Hold Your Code to Account

image

Everybody knows that good code needs to work.

But each code change that you make runs the risk of introducing fresh bugs that stop your code from working as it should. That’s why thorough testing is so important: it means you get to know about any problems in your code before it’s deployed to the live environment. In this appendix, we’ll discuss JUnit and KotlinTest, two libraries which you can use to unit test your code so that you always have a safety net.

Kotlin can use existing testing libraries

As you already know, Kotlin code can be compiled down to Java, JavaScript or native code, so you can use existing libraries on its target platform. When it comes to testing, this means that you can test Kotlin code using the most popular testing libraries in Java and JavaScript.

Let’s see how to use JUnit to unit test your Kotlin code.

Add the JUnit library

The JUnit library (https://junit.org) is the most frequently used Java testing library.

To use JUnit in your Kotlin project, you first need to add the JUnit libraries to your project. You can add libraries to your project by going to the File menu and choosing Project Structure → Libraries, or, if you have a Gradle project, you can add these lines to your build.gradle file:

image

Unit testing is used to test individual units of source code, such as classes or functions.

Once the code is compiled, you can then run the tests by right-clicking the class or function name, and then selecting the Run option.

To see how to use JUnit with Kotlin, we’re going to write a test for the following class named Totaller: the class is initialized with an Int value, and it keeps a running total of the values which are added to it using its add function:

class Totaller(var total: Int = 0) {
    fun add(num: Int): Int {
        total += num
        return total
    }
}

Let’s see what a JUnit test might look like for this class.

Create a JUnit test class

Here’s an example JUnit test class named TotallerTest that’s used to test Totaller:

image

Each test is held in a function, prefixed with the annotation @Test. Annotations are used to add programmatic information about your code, and the @Test annotation is a way of telling tools “This is a test function”.

Tests are made up of actions and assertions. Actions are pieces of code that do stuff, while assertions are pieces of code that check stuff. In the above code, we’re using an assertion named assertEquals which checks that the two values it’s given are equal. If they’re not, assertEquals will throw an exception and the test will fail.

Note

You can find out more about using JUnit here: https://junit.org

In the above example, we’ve named our test function shouldBeAbleToAdd3And4. We can, however, use a rarely used feature of Kotlin which allows us to wrap function names in back-ticks (`), and then add spaces and other symbols to the function name to make it more descriptive. Here’s an example:

image

For the most part, you use JUnit on Kotlin in almost the same way you might use it with a Java project. But if you want something a bit more Kotliny, there’s another library you can use, named KotlinTest.

Using KotlinTest

The KotlinTest library (https://github.com/kotlintest/kotlintest) has been designed to use the full breadth of the Kotlin language to write tests in a more expressive way. Just like JUnit, it’s a separate library which needs to be added to your project if you want to use it.

KotlinTest is pretty vast, and it allows you to write tests in many different styles, but here’s one way of writing a KotlinTest version of the JUnit code we wrote earlier:

image

The above test looks similar to the JUnit test you saw earlier, except that the test function is replaced with a String, and the calls to assertEquals have been rewritten as shouldBe expressions. This is an example of KotlinTest’s String Specification—or StringSpec—style. There are several testing styles available in KotlinTest, and you should choose the one which is best suited to your code.

But KotlinTest isn’t just a rewrite of JUnit (in fact, KotlinTest uses JUnit under the hood). KotlinTest has many more features that can allow you to create tests more easily, and with less code, than you can do with a simple Java library. You can, for example, use rows to test your code against entire sets of data. Let’s look at an example.

Use rows to test against sets of data

Here’s an example of a second test which uses rows to add lots of different numbers together (our changes are in bold):

image

You can also use KotlinTest to:

  • * Run tests in parallel.

  • * Create tests with generated properties.

  • * Enable/disable tests dynamically. You may, for example, want some tests to run only on Linux, and others to run on Mac.

  • * Put tests in groups.

and lots, lots more. If you’re planning on writing a lot of Kotlin code, then KotlinTest is definitely worth a look.

You can find out more about KotlinTest here:

https://github.com/kotlintest/kotlintest

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

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