A little bash script to run tests when a file has been changed If you just want a , here’s the script you can plop in whatever file you’re using—explanation follows. tl:dr .bashrc The function runs your tests once by calling , watches every file in and , and runs the tests again when a watched file changes. pw phpunit tests php src tests You can specify any arguments after , e.g. or . PHPUnit pw pw ./tests/Unit/FooTest.php pw --filter test_true_is_true The script uses Facebook’s library, which—on OSX—can be installed installed via . Watchman Homebrew brew install watchman Watchman watches files and triggers actions when they change. The reasoning behing choosing Watchman: it’s easy to install, simple to configure, and reliable. The command—which ships with Watchman—is a specialised interface for Watchman to invoke build tools in response to file changes—exactly what we need! watchman-make Let’s do a line-by-line review of our watch function. function pw { } The function name determines the command name. I like short commands—PHPUnit is aliased to on my machine—so an abbreviated version of seems like a good fit. p phpunit-watch run="clear && printf '\e[3J' && vendor/bin/phpunit" Since we’re going to need the actual “run” command twice, let’s store it in a variable. To break it down further, clears the terminal (to keep previous test runs from cluttering it) and runs the tests. clear && printf ‘\e[3J’ vendor/bin/phpunit [[ -n $@ ]] && args=$@ || args="tests" arguments to work. We’ll default the arguments to tests, which means the actual command that we’ll run is . If we provide any arguments to , they’ll replace , for example would run . watchman-make needs (I’d love to be proven wrong here so I can clean this part up!) vendor/bin/phpunit tests pw test pw —-stop-on-failure vendor/bin/phpunit —-stop-on-failure The next two commands bring everything together. eval "$run $args" Manually triggers the command once before watching. This way we see immediately see test results without having to change a file first. watchman-make \-p 'src/**/*.php' 'tests/**/*.php' \--make=$run \-t "$args" Finally, time for the Watchman part! The parameter specifies which folders we want to watch. I personally set up way more globs like and since I’m mostly working with Laravel. specifies which command we’re going to run on change, and will pass extra arguments to the command (remember, that variable we defaulted to ). -p app/**/*.php database/**/*.php --make -t --make tests I’m still dreaming of a -like CLI tool for PHPUnit, which also allows you to filter and rerun specific tests without breaking out of the watch function, but being able to run tests on change is already a vast workflow boost. Jest To wrap things up, here’s the full function again: pw This post builds further on a Coffee and Code article by Jonathan Knapp.