简体   繁体   中英

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?

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.

Іf you are creating a test package in your application, delete default test.py file in your app. This will solve your problem.

I had a similar problem. My file structure looked like this

project/
    apps/
        app_1/
            test/
                __init__.py
                test_views.py

        app_2/
             test/
                __init__.py
                test_models.py
    manage.py

And for me helps to set folder (not an app from installed apps). All tests runs by command python ./manage.py test apps/ attention to the trailing slash

tests for app_1 runs by command python ./manage.py test app_1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM