---
description: >-
  Running your tests automatically every time you save helps a lot with
  languages like Elixir.
---

# Run tests automatically on save

Reported from [dev.to](https://dev.to/petros/run-tests-automatically-on-save-1bcm)

I was looking for a solution to run tests automatically every time I save any changes. The best way so far for me is the following [hex](https://hex.pm/) package:

[mix\_test\_watch](https://hex.pm/packages/mix_test_watch)

### Install the dependency

```elixir
# mix.exs (v1.13)
def deps do
  [
    {:mix_test_watch, "~> 1.0", only: :dev}
  ]
end
```

### Configure it in your project

```elixir
# config/config.exs
import Config
​
if config_env() == :dev do
  config :mix_test_watch,
    clear: true
end
```

The `clear: true` option means that the screen will clear every time tests run. This is useful because it is easier to scroll back to the top of the most recent test run.

### Start watching for changes

In your terminal or within a VS Code terminal, this works great:

```shell
mix test.watch --seed 0 --max-failures 1 --include pending
```

### Example

Here's an example of how this looks in VS Code:

<figure><img src="https://722931016-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDUq5E45vIeUDn4lY3Psn%2Fuploads%2F1UNR1T44c5469rDsnYZX%2Fvs-code-elixir-example-run-tests.gif?alt=media&#x26;token=5ae6c1fa-6469-4cac-bfa9-358455b54071" alt=""><figcaption><p>Saving a file in VS Code, runs the tests automatically in the terminal below.</p></figcaption></figure>

Enjoy!

{% hint style="info" %}
Do you have feedback or questions about this? [Email me](mailto:petros@hey.com).
{% endhint %}
