Run tests automatically when saving file

In test-driven development, the process of "update file"-> "execute test command in console" was tedious. So here's how to automate this process.

Operation image

test.gif

environment

Preparation

sudo apt install inotify-tools

code

Create the following script and place it at the root of your project.

autorun.sh


#!/usr/bin/env bash

TEST_RUNNER="pytest -s tests" #Specify the test command you want to execute
TARGETS="./src ./tests" #Specify the monitored directory

while inotifywait -r -e modify -e create -e delete $TARGETS; do
  $TEST_RUNNER
done

Execution authority is also granted.

chmod +x ./autorun.sh

Run

./autorun.sh

reference

-[How to use the inotify wait command](https://qiita.com/hana_shin/items/9e03ad7a40b4fd7afb67#4-%E3%83%A2%E3%83%8B%E3%82%BF%E3%83%BC%E3 % 82% AA% E3% 83% 97% E3% 82% B7% E3% 83% A7% E3% 83% B3% E3% 81% AE% E4% BD% BF% E3% 81% 84% E6% 96 % B9-m)

Recommended Posts

Run tests automatically when saving file
Selectively run tests
Run unit tests the moment you save the file
Automatically execute python file
Automatically convert to py file when ui file is updated in PySide