Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 660
user1011444 Avatar asked Mar 01 '13 20:03

user1011444


People also ask

What is automated testing in Django?

Automated Testing is a widely used tool that uses a collection of predefined test cases. Learn how Django and Selenium to ensure a bug free system. Table of contents.

What order Django tests are run?

Order in which tests are executedAll TestCase subclasses are run first. Then, all other Django-based tests (test cases based on SimpleTestCase , including TransactionTestCase ) are run with no particular ordering guaranteed nor enforced among them.

Does Django use Pytest or unittest?

Helps you write better programs. Many developers from Python community heard of and used unit testing to test their projects and knew about boilerplate code with Python and Django unittest module. But Pytest suggests much more pythonic tests without boilerplate.


3 Answers

Use entr:

  1. $ brew install entr
  2. $ 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
like image 194
clacke Avatar answered Oct 18 '22 21:10

clacke


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.

like image 38
The Unfun Cat Avatar answered Oct 18 '22 21:10

The Unfun Cat


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.

like image 10
Iman Mohamadi Avatar answered Oct 18 '22 23:10

Iman Mohamadi