Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test custom django-admin commands

I created custom django-admin commands

But, I don't know how to test it in standard django tests

like image 732
dixon Avatar asked Aug 17 '09 08:08

dixon


People also ask

How do I test my Django site?

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 you pass arguments in Django management command?

The parameter parser is an instance of argparse. ArgumentParser (see the docs). Now you can add as many arguments as you want by calling parser 's add_argument method. In the code above, you are expecting a parameter n of type int which is gotten in the handle method from options .


1 Answers

If you're using some coverage tool it would be good to call it from the code with:

from django.core.management import call_command from django.test import TestCase  class CommandsTestCase(TestCase):     def test_mycommand(self):         " Test my custom command."          args = []         opts = {}         call_command('mycommand', *args, **opts)          # Some Asserts. 

From the official documentation

Management commands can be tested with the call_command() function. The output can be redirected into a StringIO instance

like image 89
Jorge E. Cardona Avatar answered Oct 08 '22 02:10

Jorge E. Cardona