如何在 Django 中使用 python manage.py 测试命令 运行 所有测试
How to run all tests with python manage.py test command in django
我正在开发一个 Django 项目,其中有多个应用程序。每个应用程序都有一个测试目录,其中包含对整个项目的测试。我的目录结构如下。
Project
App_1
tests
__init__.py
tests_views.py
App_2
tests
__init__.py
tests_views.py
settings.py
manage.py
我可以运行这样测试
python manage.py test App_1.tests
运行 App_1/tests/test_views.py 中的所有测试。但我必须为我项目中的所有应用程序执行此操作。我想要一个命令,它 运行 在我的项目中的所有应用程序中进行所有测试。我试过 运行ning
python manage.py test
但我得到以下错误
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?
任何人都可以告诉我如何使用一个命令在我的应用程序中进行所有测试吗?
运行 python manage.py test
是一次 运行 项目中所有测试的正确方法,您的错误是由其他原因引起的。
您的测试文件夹结构是否有问题?要使用默认的单元测试功能,它们应该像这样存储:
myproject/
myapp/
tests/
__init__.py
test_models.py
test_views.py
我认为你的问题是因为你的 tests
文件夹中可能有一个 tests
文件夹,这会混淆单元测试。还要确保您的文件夹中有 __init__.py
,以便 python 可以看到里面的文件。查看 here Django 测试文档。
我遇到了类似的问题。我的文件结构是这样的
project/
apps/
app_1/
test/
__init__.py
test_views.py
app_2/
test/
__init__.py
test_models.py
manage.py
对我来说帮助设置文件夹(不是已安装应用程序中的应用程序)。
所有测试均按命令 python ./manage.py test apps/
运行,注意尾部斜杠
app_1 的测试通过命令 python ./manage.py test app_1
运行
І如果您要在应用程序中创建测试包,请删除应用程序中的默认 test.py 文件。这将解决您的问题。
我正在开发一个 Django 项目,其中有多个应用程序。每个应用程序都有一个测试目录,其中包含对整个项目的测试。我的目录结构如下。
Project
App_1
tests
__init__.py
tests_views.py
App_2
tests
__init__.py
tests_views.py
settings.py
manage.py
我可以运行这样测试
python manage.py test App_1.tests
运行 App_1/tests/test_views.py 中的所有测试。但我必须为我项目中的所有应用程序执行此操作。我想要一个命令,它 运行 在我的项目中的所有应用程序中进行所有测试。我试过 运行ning
python manage.py test
但我得到以下错误
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?
任何人都可以告诉我如何使用一个命令在我的应用程序中进行所有测试吗?
运行 python manage.py test
是一次 运行 项目中所有测试的正确方法,您的错误是由其他原因引起的。
您的测试文件夹结构是否有问题?要使用默认的单元测试功能,它们应该像这样存储:
myproject/
myapp/
tests/
__init__.py
test_models.py
test_views.py
我认为你的问题是因为你的 tests
文件夹中可能有一个 tests
文件夹,这会混淆单元测试。还要确保您的文件夹中有 __init__.py
,以便 python 可以看到里面的文件。查看 here Django 测试文档。
我遇到了类似的问题。我的文件结构是这样的
project/
apps/
app_1/
test/
__init__.py
test_views.py
app_2/
test/
__init__.py
test_models.py
manage.py
对我来说帮助设置文件夹(不是已安装应用程序中的应用程序)。
所有测试均按命令 python ./manage.py test apps/
运行,注意尾部斜杠
app_1 的测试通过命令 python ./manage.py test app_1
І如果您要在应用程序中创建测试包,请删除应用程序中的默认 test.py 文件。这将解决您的问题。