Run cargo test And cargo run Together When Files Change With watchexec

May 2024
watchexec -e rs "bash -c 'cargo test -- --nocapture && cargo run'"

You can also put it in a script like:

filename: start-test-and-run.bash
#!/bin/bash

watchexec -e rs "bash -c 'cargo test -- --nocapture && cargo run'"

Notes

  • There's probably a way to do that directly with cargo watch but I haven't figured that out yet
  • This also let you add other extensions to watch for (e.g. for template files). I wouldn't be surprised if cargo watch can do that too give its use of watchexec under the hood
end of line