Faster cars with lower insurance costs?

What are some faster cars available in the UK which have relatively low insurance costs. I am 20 years old so cars such as Imprezas where the insurance is 4000+ is too much. Are there any cars in the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Painless UI Testing with Kaspresso

I have been using Kotest for about a year now and it is safe to say that it has encouraged me to test more of my code. Frameworks like Kotest and Spek provide a good alternative for unit testing. However, UI testing still feels lacking.

Most Android developers are using Espresso for writing UI tests. Even with its super clean API, Espresso feels repetitive and verbose. My general solution is to work around this by extracting the repetitive code for reuse. It is rarely a clean solution.

Part of my dev-wishlist has been to establish this perfect loop of TDD flow with Android where all the features start with a UI test, followed by multiple unit tests to satisfy the business logic. The goal is that we have a solid, well-tested, and self-documented feature at the end of each cycle. I started searching for other options which led me to several frameworks, most of which were named after a coffee (I sense a trend!).

The resulting tests are reduced to a combination of actions and assertions inside the screen:

What could be a better example than to revisit the Grade Calculator we built in the previous post? This time let’s build it in a purely Test-Driven style.

Let’s create a simple Android project with an empty Activity. Then add the following test dependencies:

If you set up your project using the Android Studio wizard, then most of these dependencies should already be present. The dependencies that we will manually add are Kotest, Mockk, and Kaspresso.

Kotest and Mockk are the same as in the previous post and are used for unit testing and mocking. Kaspresso will be used for UI tests.

Let’s add a UI test for the happy path of our calculator without touching the activity first:

If we try to run this test, it will fail because we have not yet added the GradeCalculatorScreen. This test serves as a blueprint of what we want to achieve for the grade calculation. Let’s break down each step:

Every step is described with a step block. This part is optional, you can skip the steps if you want and directly write the encapsulating code. However, it is very helpful since not only it provides a comment on what is happening but this information also gets printed in the logs.

In the first step, we launch the activity, this can be done directly with the test rule though.

In the next two steps, we perform some actions on the GradeCalculatorScreen and then verify their results. Note that all the methods come from the library, we only provide view bindings in the Screen implementations.

At this point, we cannot even compile the application due to missing classes. Let’s provide an implementation for our GradeCalculatorScreen.

As explained earlier, the screen class binds the UI elements and consolidates them into one place. Since we do not have them in the layout XML, the project won’t compile. So here’s a simple layout for it:

The next step is to provide the business logic to calculate the grade.

Create a GradeCalculatorSpec class inside the test package and add the specs for grade calculation:

Once all the tests are passing, the resulting implementation would look something like this:

The final step is to bind the GradeCalculator with the UI. Let’s add a click listener to the button and use the user input to calculate the grade.

If all goes well, all tests should be passing now. Also, if you look at the logcat output, it will have a complete summary of all test steps.

Using Kaspresso, you can also handle runtime permissions, change device state, execute ADB commands right from your code. The flexibility that it provides for writing tests in truly amazing.

This is a very basic example that does not handle any edge cases. However, I hope it was enough to give you a taste of test-driven development with Android.

We started with a crude idea without any implementation details in mind and used tests to shape our logic. There are a couple of benefits to this approach:

Add a comment

Related posts:

Will my insurance rates go up if I get a speeding ticket in someone elses

I was driving an insured vehicle which belongs to a nonlicensed driver., I received a speeding ticket in this car. Whose insurance will go up? ANSWER: I suggest that you try this site where one can…

What Jobs Do I Keep On My Resume?

The question was posed on Quora today: “How do you know what jobs to put on a resume and when to take them off?” This question is important as you need to know when too much is indeed too much on…

Writing Contests and Grants for May 2022

They present a challenge to meet editorial requirements. There is also the passive side of the competition. You do some active hours of writing to submit your entry this month. And a few months…