How to automatically run tests when there's an

2019-02-01 17:24发布

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.

12条回答
We Are One
2楼-- · 2019-02-01 17:52

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.

查看更多
劫难
3楼-- · 2019-02-01 17:53

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!

查看更多
成全新的幸福
4楼-- · 2019-02-01 17:57

You'll need a continuous integration server, something like Jenkins.

查看更多
Ridiculous、
5楼-- · 2019-02-01 18:00

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).

查看更多
甜甜的少女心
6楼-- · 2019-02-01 18:00

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 :)

查看更多
爷、活的狠高调
7楼-- · 2019-02-01 18:01

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.

查看更多
登录 后发表回答