Sample Guard Auto Testing
This is a sample python project to demonstrate how you can run tests automatically whenever there is a change in the source file or its associated test file.
Guard is an amazing framework written in the most amazing language (in my opinion) that lets you watch for file changes and run any commands on those files. It has a plethora of plugins too.
There is a plugin called guard-pytest for running python test but there is no support for desktop notifications which is what makes Guard autotesting cool. And I don't want this to be tied to one language so I can use this setup for other language projects also with minor changes.
So I am going with a plugin called guard-yield, which lets you write ruby code in Guardfile itself, to autotest python code and raise notifications using terminal-notifier in my Mac.
You can tweak this file to run tests or whatever in your project in any language and change the notification system as per your OS.
Project Structure
.
├── Gemfile
├── Guardfile
├── README.md
├── requirements.txt
├── source_dir1
│ ├── add.py
│ ├── sub_source_dir1
│ │ ├── sub.py
│ │ └── tests
│ │ └── test_sub.py
│ └── tests
│ └── test_add.py
└── source_dir2
├── mul.py
└── tests
└── test_mul.py
The project is structured in a way that each source file will have its associated test file under tests
subdirectory of file's current directory. It might be different for you.
The requirements.txt
contains the python dependency requirements.
The Gemfile
contains the ruby dependencies for our autotesting to work.
Finally the Guardfile
that contains the DSL for watching file changes, running tests and raise notifications. Check the file for inline comments on how it works.
How to run this project?
- Check out this project in a local directory
- Run
pip install -r requirements.txt
to install pytest. - Test the project by running
pytest
if you want. - Run
bundle
to install Guard and its dependencies from the Gemfile - Run
bundle exec guard
to start Guard in a terminal. - You can hit Enter to run all tests and see the notification.
- Or you can change some source or test files to trigger the autotesting of that file.
Note that if you are using IntelliJ, you need to be aware of when it triggers the autosave. You might change the file but IntelliJ won't immediately save the file if you are in the same frame.
You can always hit ⌘ S
to force save the file so Guard can be alerted quickly.
Screenshots
Success notification
Failure notification
Suggestions and criticisms are welcome.
Thanks to all the projects linked above for the knowledge and inspiration.