Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 729
Muhammad Hassan Avatar asked Apr 11 '16 10:04

Muhammad Hassan


People also ask

How do I run test 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.

How do I run a Python test from the command line?

You can invoke testing through the Python interpreter from the command line: python -m pytest [...] This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.

How do I run manage py?

To run a task of the manage.py utilityOn the main menu, choose Tools | Run manage.py task, or press Ctrl+Alt+R . The manage.py utility starts in its own console.

Which function in unit test will run all of your tests in Python?

pytest. The pytest test runner supports the execution of unittest test cases. The actual benefit of the pytest is to writing pytest test cases.


1 Answers

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.

like image 122
DrBuck Avatar answered Oct 09 '22 21:10

DrBuck