简体   繁体   English

如何在 Django 中使用 python manage.py test 命令运行所有测试

[英]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.我正在开发一个 django 项目,其中有多个应用程序。 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 python manage.py test App_1.tests

which run all tests in App_1/tests/test_views.py.在 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 python manage.py 测试

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.运行python manage.py test是一次运行项目中所有测试的正确方法,您的错误是由其他原因引起的。

Is there a problem with the folder structure of your tests?测试的文件夹结构有问题吗? To use the default unittest functionality they should be stored like this:要使用默认的 unittest 功能,它们应该像这样存储:

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.我觉得你的问题造成的,因为你可能有一个tests文件夹中的tests文件夹,这是混淆了单元测试。 Also make sure you have __init__.py in your folders so python can see the files inside.还要确保你的文件夹中有__init__.py以便 python 可以看到里面的文件。 Have a look here for the Django testing documentation. 在此处查看 Django 测试文档。

Іf you are creating a test package in your application, delete default test.py file in your app. І如果您要在应用程序中创建测试包,请删除应用程序中的默认 test.py 文件。 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所有测试都通过命令python ./manage.py test apps/注意尾部斜杠运行

tests for app_1 runs by command python ./manage.py test app_1 python ./manage.py test app_1通过命令python ./manage.py test app_1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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