How to run your tests with a single keystroke
Tags: tdd, intellij
Published: 2022-11-09 23:34:42

---
title: How to run your tests with a single keystroke
published: true
description: Why & how to run automated tests in your IDE with a single keystroke with a practical example in Intellij
tags: TDD, Intellij
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nel326pyrspnbb0qbyt8.jpeg
# Use a ratio of 100:42 for best results.
# published_at: 2022-11-09 22:36 +0000
---

> Photo by <a href="https://unsplash.com/@johnnyboylee?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Johnny Briggs</a> on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

## Why?

What is the point of going through the trouble to set all this up? I will try to briefly explain the reason I like to set up my test runs in this way.

### Test-driven development

TDD is a practice which has many variations. The canonical "red, green, refactor" variation is an oft quoted one:

1. Red: Write the tests, they fail (compile fail also counts)
2. Green: Implement the code which the tests test, tests pass
3. Refactor: Clean up the code (both implementation & tests), presumably running the tests again to see if you broke something while refactoring

This is a very short & simple description which doesn't get into details, some of which can be important. One of which is the prediction step:

1a. Red: Write the tests
1b. Red: Try to *predict* the *result* of their failure
1c. Red: Run tests
1d. Red: Compare the actual failure result to your prediction
1e. Red: If they differ, either fix your understanding of the tests or fix the tests

There are other variations including or excluding this one.

In general, I find that if the developer writes code *with* tests (before, after or in-between), this is enough to get the test-driven benefit: The tests guide the code/design leading to just enough code being written in a readable, decoupled manner which makes it easy to understand & modify.

### Time

To get this benefit, time is of importance. The time window within which both tests & implementation are written has to be small because the human short-term memory can hold only so much information and for a finite, often short, amount of time.

This leads to two requirements:

1. Your tests have to run very fast
2. You have to run your tests often

Writing fasts tests is a skill to be practiced & perfected but is out of scope of this article. However, I assure you it is possible to do, even for higher level non-unit tests.

Running tests often is easier to do when the process of running them is simple. The simplest process I can think of is a single keypress.

## How?

In this part of the article the theory ends and practice begins. The example is very specific and I leave it to you to extract the principles and apply it to your set of tools if needed.

We will be using:

- [Intellij Idea](https://www.jetbrains.com/idea/)
- Intellij plugin [Run Configuration as Action](https://plugins.jetbrains.com/plugin/9448-run-configuration-as-action)

### 1. Create a test configuration

Create an Intellij [Run Configuration](https://www.jetbrains.com/help/idea/run-debug-configuration.html) to run your tests.

Try it out - run it by selecting it in the Run Configuration drop-down menu and clicking Run button.

When you're happy with it, give it a nice name you'll remember, like `unit tests`.

### 2. Configure action keyboard shortcut

Go to `Preferences` / `Keymap` / `Plugins` / `Run Configuration as Action`.

Scroll down until you find an entry `Run 'unit tests'` (you may have to restart Intellij for it to show up in the list).

Select it, right-click and choose `Add Keyboard Shortcut`.

Enter keyboard shortcut, i.e. `F10`. Sort out any conflicts how you see fit.

Press your shortcut to see if the configuration runs.

### 3. Diversify

On a more complex project (e.g. multiple stack layers of a web application) there can be multiple run configurations for varying types of tests. Create each one with a different name and set up a different keyboard shortcut for each.

Examples:

- `F10` Backend unit tests
- `F9` Frontend unit tests
- `Ctrl+F10` Backend integration tests (testing adapter code with external services)

This way you can selectively run only one type of tests, according to the part of your system you are working on.