How to automatically run tests when there's any change in my project (Django)?

后端 未结 14 1814
有刺的猬
有刺的猬 2020-12-23 19:45

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

相关标签:
14条回答
  • 2020-12-23 20:00

    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.

    0 讨论(0)
  • 2020-12-23 20:07

    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!

    0 讨论(0)
  • 2020-12-23 20:08

    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.

    0 讨论(0)
  • 2020-12-23 20:10

    Use 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
    
    0 讨论(0)
  • 2020-12-23 20:14

    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.

    0 讨论(0)
  • 2020-12-23 20:14

    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.

    0 讨论(0)
提交回复
热议问题