Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test single application (not project) in Django?

Tags:

I would like to test my small application, that I keep in a separate package. Right now I created a "test_project" directory, created a test project there and I am using the project's manage.py to run tests. But I keep wondering - is there a better method? Is it possible to launch a single app's tests, perhaps with some default configuration (like, sqlite database)?

like image 267
dotz Avatar asked Jun 05 '12 12:06

dotz


People also ask

How do I run a specific app in Django?

It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py in your app's root dir which setups Django settings and runs ./manage.py test your_app programmatically.

What is the difference between a project and an app in Django?

A project refers to the entire application and all its parts. An app refers to a submodule of the project. It's self-sufficient and not intertwined with the other apps in the project such that, in theory, you could pick it up and plop it down into another project without any modification.

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.


2 Answers

Writing this down because I don't see it on here yet

From the docs

If your django structure looks something like this as it should:

Project dir/
    /myapp
    /myotherapp
    /myproject

Django allows you to execute test from only "myotherapp" by executing:

./manage.py test myotherapp/

This will execute all test under "myotherapp" whereas

./manage.py test 

will execute all test in your projects directory.

like image 180
Nick Brady Avatar answered Sep 19 '22 05:09

Nick Brady


It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py in your app's root dir which setups Django settings and runs ./manage.py test your_app programmatically. One example of runtests.py is Django's own: runtests.py (documentation).

Django's own runtests.py is a good reference but I find it convoluted for most cases. Below are a couple of alternative examples:

  • Django-Modeltranslation
  • My own minimalistic one
like image 20
Dário Avatar answered Sep 19 '22 05:09

Dário