49π
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
18π
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.
- [Django]-Difference between different ways to create celery task
- [Django]-Django values_list vs values
- [Django]-Where to store secret keys DJANGO
11π
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.
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Django debug display all variables of a page
- [Django]-Django: Error: You don't have permission to access that port
10π
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.
- [Django]-How can I use Bootstrap with Django?
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Annotate with latest related object in Django
9π
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.
- [Django]-Django β "no module named django.core.management"
- [Django]-Is virtualenv recommended for django production server?
- [Django]-Access request in django custom template tags
- [Django]-Django admin and MongoDB, possible at all?
- [Django]-Exclude a field from django rest framework serializer
- [Django]-Django Local Settings
2π
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!
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-How to log all sql queries in Django?
- [Django]-Django South β table already exists
1π
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).
- [Django]-Django delete FileField
- [Django]-How to resize an ImageField image before saving it in python Django model
- [Django]-"CSRF token missing or incorrect" while post parameter via AJAX in Django
1π
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.
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-Select distinct values from a table field
- [Django]-Django signals vs. overriding save method
1π
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
- [Django]-Django won't refresh staticfiles
- [Django]-Make the first letter uppercase inside a django template
- [Django]-Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7
1π
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.
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-Accepting email address as username in Django
- [Django]-Silence tqdm's output while running tests or running the code via cron
0π
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 π
- [Django]-Django 1.7 β "No migrations to apply" when run migrate after makemigrations
- [Django]-Django β comparing old and new field value before saving
- [Django]-Trying to parse `request.body` from POST in Django
0π
One of key features β run only impacted tests on file changes. Available as plugin for PyCharm
- [Django]-Embedding JSON objects in script tags
- [Django]-How to delete a record in Django models?
- [Django]-'WSGIRequest' object has no attribute 'user' Django admin
0π
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
- [Django]-Run Django shell in IPython
- [Django]-TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
- [Django]-What is the difference render() and redirect() in Django?
0π
Similar to entr
you can use watchexec
with
brew install watchexec
watchexec -rc -e py -- python ./manage.py test
with options
-r # restart
-c # clear screen
-e extensions # list of file extensions to filter by
- [Django]-Favorite Django Tips & Features?
- [Django]-Django: How do I add arbitrary html attributes to input fields on a form?
- [Django]-CSRF with Django, React+Redux using Axios
- [Django]-How to serve media files on Django production environment?
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
- [Django]-Django β how to unit test a post request using request.FILES