At the moment I am running python manage.py test
every once in a while after I make significant changes in my django project. Is it possible to run those tests automatically whenever I change and save a file in my project? It'll be useful to detect bugs earlier (I know rails has something like this with rspec). I am using nose and django-nose. Thanks in advance.

- 1,389
- 2
- 15
- 25
-
1[This project](https://pypi.python.org/pypi/nosy) will help if you want to run your test suite on code changes rather than commits. – Matt Mar 01 '13 at 20:47
-
Thanks for the tip, it looks like what I need for the moment being. Would you by chance know if nosy can easily be added to django-nose (like as a plug-in: [django-nose short explanation on that](https://github.com/jbalogh/django-nose#custom-plugins))? – user1011444 Mar 01 '13 at 23:17
-
possible duplicate of [Is there something like 'autotest' for Python unittests?](http://stackoverflow.com/questions/108892/is-there-something-like-autotest-for-python-unittests) – mlt Mar 11 '15 at 06:14
16 Answers
Use entr:
- $
brew install entr
- $
find . -name '*.py' | entr python ./manage.py test
Or, for extra credit, combine it with ack:
$ ack --python | entr python ./manage.py test
If you want it to even find new files as you add them:
$ until ack -f --python | entr -d python ./manage.py test; do sleep 1; done

- 816
- 10
- 31

- 7,688
- 6
- 46
- 48
-
6And if you are using silver searcher, change "ack -f --python" to "ag -l --python" – c17r Jul 22 '16 at 15:12
-
2Hadn't heard of [The Silver Searcher](http://geoff.greer.fm/ag/), thanks for the tip! – clacke Jul 25 '16 at 08:54
-
4never heard of `entr`. it`s even more useful than what I was looking for. thanks :) – jorgemdnt May 29 '17 at 13:19
-
2@c17r One year later, and I'm still using `ag` (silver searcher) almost every single workday. – clacke May 31 '17 at 18:20
-
3Also consider [ripgrep](https://github.com/BurntSushi/ripgrep) as an alt to ag/ack. – Brian M. Hunt Oct 17 '17 at 12:05
-
2@BrianM.Hunt I checked out rg about a year ago, but at that time it had strange output (lots of empty lines) compared to the equivalent ag call, and I saw no reason to figure out why, as ag was working well for me. – clacke Oct 30 '17 at 12:42
-
1I would later check out `rg` again (possibly right after writing that comment) and I am now using it every day. Good stuff. – clacke Nov 27 '18 at 02:04
py.test answer (which also works for nose):
pip install pytest-xdist
py.test -f # will watch all subfolders for changes, and rerun the tests
Since py.test understands nose, this works for nose too.

- 29,987
- 31
- 114
- 156
-
3Nose is no longer maintained, and py.test is just oh so much better and more pythonic anyways. – The Unfun Cat May 03 '17 at 05:43
I'm a JavaScript developer so I used the tools JS developer have built with Node.js to achieve the same goal in my projects. It is very simple but you also need to install nodeJS to get it working.
I created a file called gruntfile.js in my project root directory:
//gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
watch: {
files: ['*.py'],
tasks: ['shell']
},
shell: {
test: {
command: 'python main_test.py'
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['watch']);
};
What it's doing is basically watching any file in that directory that has a py extension and if they changed it execute a shell command which in this case is my python test (you might wanna change it, my test name was main_test.py). In order to run this grunt script you need to install Node.js and after that you will have npm in your global path. after that you need to insall a few node modules as well. All these modules except grunt-cli will be stored in your current folder so make sure you are at the root of your project or what ever folder you put that gruntfile.js in. then run the fallowing commands.
npm install grunt-cli -g
npm install grunt
npm install grunt-contrib-watch
npm install grunt-shell
Don't worry about the size, these are very small modules. Now that you have every thing setup you can simply run grunt
and it will start watching your py files and when you saved them it will run your tests. It may not be best way for running python tests but as I said I'm a JavaScript developer and I think Grunt has provided a very simple way of executing tests even for other languages so I use it.

- 6,552
- 3
- 34
- 33
I just tried nose-watch
and it worked fantastic! install the plugin and run the test with the --with-watch
option.
Update: :( it does not seem to work well when running tests from django-nose's manage.py helper.
Eventually I opted to use tdaemon, which supports django, although might require a bit of fiddling as well for full fledged projects.
For example here is how I ran it for my django project:
tdaemon -t django --custom-args=a_specific_app_to_test -d --ignore-dirs logs
The --custom-args
was to focus tests to specific app (same as you would do python manage.py test a_specific_app_to_test
The -d
argument is to enable debug logging, which prints which file change triggered the run.
The --ignore-dirs
was necessary because my tests wrote to the logs (which in itself is a problem!) and tdaemon
went into an infinite loop.

- 10,307
- 6
- 65
- 88
Another Javascript dev here, I've found nodemon
(https://github.com/remy/nodemon) to work pretty well. By default it watches *.js
files but that's configurable with the --ext
flag. To use it, do:
npm install -g nodemon
cd /your/project/dir/
nodemon --ext py --exec "python manage.py test"
Now, whenever a *.py
file changes, it'll re-run your command. It even finds new files.

- 4,031
- 4
- 41
- 36
-
I also have been doing this, but I find when tests finish it registers as a "crash" and sometimes the teardown doesn't seem to run. Haven't quite figured it out, but wondering if you'd hit similar problems? – dcsan Jun 17 '20 at 09:54
-
@dcsan I haven't but I haven't touched the project I was using this in for a long time either. Sorry I can't help. – Tom Saleeba Jun 18 '20 at 02:08
I did this using gulp. Install gulp-shell:
npm install gulp-shell --save-dev
And in the gulpfile.js:
var shell = require('gulp-shell')
gulp.task('run-tests', shell.task([
'python3 manage.py test']))
gulp.task('watch', function(){
gulp.watch(['./your-project-name/**/*.html', './your-project-name/**/*.py'], ['run-tests']);
});
gulp.task('default',['run-tests','watch']);
And it runs the tests anytime there are changes saved to any .py or .html files!

- 1,034
- 1
- 8
- 22
You can use Django Supervisor on top of Django. This will avoid the use of a CI tool (which may be useful in any case, this isn't invalidating the other response - maybe just complementary).

- 648
- 5
- 15
I would recommend setting up django-nose and sniffer. It's quite easy to setup and works great. Something along the lines of this scent.py was the only customization I needed. Then you can just run sniffer -x myapp.tests
.
Nose comes with some other goodies that make tests a bit nicer to work with as well.
if you use git control code, another way to is use git hook pre-commit
maybe error like remote: fatal: Not a git repository: '.'
, check this post https://stackoverflow.com/a/4100577/7007942

- 59
- 5
I've found the easiest way is to use gulp as recommended by this post. Note that gulp-shell
(recommended by other answers) is actually blacklisted by gulp, but thankfully you don't even need a plugin. Try this instead:
// gulpfile.js
const { watch } = require('gulp')
var exec = require('child_process').exec
function test (cb) {
exec('python manage.py test', function (err, stdout, stderr) {
console.log(stdout)
console.log(stderr)
cb(err)
})
}
exports.default = function () {
watch('./**/*.py', test)
}
In the past, I've tried many of the options suggested in other answers. This one was comparatively painless. It's helpful if you have some knowledge of JavaScript.

- 1,931
- 2
- 21
- 33
I wrote a Gulp task to automatically run ./manage.py test
whenever any specified Python files are changed or removed. You'll need Node for this.
First, install Gulp:
yarn add -D gulp@next
Then use the following gulpfile.js
and make any necessary adjustments:
const { exec } = require('child_process');
const gulp = require('gulp');
const runDjangoTests = (done) => {
const task = exec('./manage.py test --keepdb', {
shell: '/bin/bash', // Accept SIGTERM signal. Doesn't work with /bin/sh
});
task.stdout.pipe(process.stdout);
task.stderr.pipe(process.stderr);
task.on('exit', () => {
done();
});
return task;
};
gulp.task('test', runDjangoTests);
gulp.task('watch', (done) => {
let activeTask;
const watcher = gulp.watch('**/*.py', {
// Ignore whatever directories you need to
ignored: ['.venv/*', 'node_modules'],
});
const runTask = (message) => {
if (activeTask) {
activeTask.kill();
console.log('\n');
}
console.log(message);
activeTask = runDjangoTests(done);
};
watcher.on('change', (path) => {
runTask(`File ${path} was changed. Running tests:`);
});
watcher.on('unlink', (path) => {
runTask(`File ${path} was removed. Running tests:`);
});
});
Then simply run node node_modules/gulp/bin/gulp.js watch
to run the task :)

- 3,115
- 5
- 28
- 39
One of key features - run only impacted tests on file changes. Available as plugin for PyCharm

- 494
- 5
- 9
On ubuntu this script works, after installing inotifywait
(sudo apt install inotify-tools
):
inotifywait -qrm -e close_write * |
while read -r filename event; do
python manage.py test accord
done

- 15,047
- 5
- 57
- 61
You'll need a continuous integration server, something like Jenkins.

- 588,541
- 66
- 880
- 895
-
Thanks for the answer. I'll definitely check into that when I use Selenium. For now, I just do basic tests using the 'fake' server shipped with Django. – user1011444 Mar 01 '13 at 23:14
-
Jenkins has nothing to do with Selenium. You can easily use it with normal unit tests. – Daniel Roseman Mar 02 '13 at 09:08
-
1Worth mentioning here the [`django-jenkins`](https://github.com/kmmbvnr/django-jenkins) project. – Burhan Khalid Nov 28 '13 at 05:08
-
2I think this a bit overblown for just a file save. Is there something like the rails `guard` gem? but for python? – peterw Jan 07 '14 at 19:19
-
4that answer is too heavy for TDD. The OP asked about how to write the fast-n-easy tests that are the heartbeat of what Jenkins run. your advice would have long turnaround and possibly require a check-in – Phlip Nov 29 '14 at 14:24