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

后端 未结 14 1815
有刺的猬
有刺的猬 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:16

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

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

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

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

    I'm used watchr, something like Watchr

    0 讨论(0)
  • 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

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

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

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

    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.

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