Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect django testing mode

I'm writing a reusable django app and I need to ensure that its models are only sync'ed when the app is in test mode. I've tried to use a custom DjangoTestRunner, but I found no examples of how to do that (the documentation only shows how to define a custom test runner).

So, does anybody have an idea of how to do it?

EDIT

Here's how I'm doing it:

#in settings.py import sys TEST = 'test' in sys.argv 

Hope it helps.

like image 714
Herberth Amaral Avatar asked Aug 05 '11 13:08

Herberth Amaral


People also ask

How do I test my Django app?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

How do I run tests PY in Django?

Open /catalog/tests/test_models.py.from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

What is RequestFactory in Django?

The request factory The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.

Where does Django store test database?

from the docs: When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!). The TEST dictionary in DATABASES offers a number of settings to configure your test database.


1 Answers

I think the answer provided here https://stackoverflow.com/a/7651002/465673 is a much cleaner way of doing it:

Put this in your settings.py:

import sys  TESTING = sys.argv[1:2] == ['test'] 
like image 106
jjmaestro Avatar answered Sep 29 '22 18:09

jjmaestro