How to run all tests with python manage.py test command in django

Muhammad Hassan picture Muhammad Hassan · Apr 11, 2016 · Viewed 15.9k times · Source

I am working on a django project in which I have multiple apps. Every app has a tests directory which has test for whole project. My directory structure is as follow.

Project
      App_1
           tests
               __init__.py
               tests_views.py
      App_2
           tests
               __init__.py
               tests_views.py
      settings.py
      manage.py

I can run tests like this

python manage.py test App_1.tests

which run all tests in App_1/tests/test_views.py. But I have to do this for all app in my project. I want a single command which run all tests inside all apps in my project. I tried by running

python manage.py test

but I got following error

Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/test/runner.py", line 209, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/srv/www/project/shared/env/local/lib/python2.7/site-packages/django/test/runner.py", line 150, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/usr/lib/python2.7/unittest/loader.py", line 204, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/usr/lib/python2.7/unittest/loader.py", line 285, in _find_tests
    for test in self._find_tests(full_path, pattern):
  File "/usr/lib/python2.7/unittest/loader.py", line 265, in _find_tests
    raise ImportError(msg % (mod_name, module_dir, expected_dir))
ImportError: 'tests' module incorrectly imported from '/vagrant/code/project/App_1/tests'. Expected '/vagrant/code/project/App_1'. Is this module globally installed?

Can any one tells me how I can all tests in my app with a single command?

Answer

DrBuck picture DrBuck · Apr 11, 2016

Running python manage.py test is the right way to run all the tests in your projects at once, your error is caused by something else.

Is there a problem with the folder structure of your tests? To use the default unittest functionality they should be stored like this:

myproject/
   myapp/
       tests/
           __init__.py
           test_models.py
           test_views.py

I think your problem is caused because you may have a tests folder within your tests folder, which is confusing unittest. Also make sure you have __init__.py in your folders so python can see the files inside. Have a look here for the Django testing documentation.