简体   繁体   English

Pycharm - 未找到 django 测试但在控制台中运行

[英]Pycharm - no django tests found but run in console

I followed this tutorial https://docs.djangoproject.com/en/3.0/intro/tutorial05/我按照本教程https://docs.djangoproject.com/en/3.0/intro/tutorial05/

The tests can be run by the command ( polls is the application name)可以通过命令运行测试( polls是应用程序名称)

python manage.py test polls

However under PyCharm IDEA, when I click on the green arrow (line 21-22).但是在 PyCharm IDEA 下,当我单击绿色箭头(第 21-22 行)时。

The message is "No tests were found"消息是“未找到测试”

在此处输入图像描述

The stack trace is gibberish to me堆栈跟踪对我来说是乱码

Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py", line 35, in sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 100, in init self.parseArgs(argv) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 147, in parseArgs self.createTests() File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 159, in createTests self.module) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 220, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions回溯(最后一次调用):文件“/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py”,第 35 行,在 sys.exit(main(argv=args, module =None,testRunner=unittestpy.TeamcityTestRunner,buffer=not JB_DISABLE_BUFFERING)) 文件“/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main. py”,第 100 行,在init self.parseArgs(argv) 文件中“/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py ",第 147 行,在 parseArgs self.createTests() 文件中 "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py",第 159 行,在 createTests self.module) 文件“/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py”,第 220 行,在 loadTestsFromNames 套件中 = [self.loadTestsFromName(name, module) for name in names] File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions /3.7/lib/python3.7/unittest/loader.py", line 220, in suites = [self.loadTestsFromName(name, module) for name in names] File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName module = import (module_name) File "/Users/raychenon/Projects/python/django/mysite/polls/test_views.py", line 8, in from.models import Question File "/Users/raychenon/Projects/python/django/mysite/polls/models.py", line 8, in class Question(models.Model): File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 107, in new app_config = apps.get_containing_app_config(module) File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready settings.INSTALLED_APPS File "/usr/local/lib/python3.7/site-packages/django/ /3.7/lib/python3.7/unittest/loader.py”,第 220 行,在套件中 = [self.loadTestsFromName(name, module) for name in names] 文件“/usr/local/Cellar/python/3.7.7 /Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py”,第 154 行,在 loadTestsFromName 模块 =导入(模块名称)文件“/Users/raychenon/Projects/python/django/mysite/ polls/test_views.py”,第 8 行,from.models 导入问题文件“/Users/raychenon/Projects/python/django/mysite/polls/models.py”,第 8 行,class 问题(models.Model):文件“/usr/local/lib/python3.7/site-packages/django/db/models/base.py”,第 107 行,在的 app_config = apps.get_ contains_app_config(module) 文件“/usr/local/lib/ python3.7/site-packages/django/apps/registry.py”,第 252 行,在 get_ contains_app_config self.check_apps_ready() 文件“/usr/local/lib/python3.7/site-packages/django/apps/registry. py”,第 134 行,在 check_apps_ready settings.INSTALLED_APPS 文件“/usr/local/lib/python3.7/site-packages/django/ conf/ init .py", line 76, in getattr self._setup(name) File "/usr/local/lib/python3.7/site-packages/django/conf/ init .py", line 61, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. conf/ init .py”,第 76 行,在getattr self._setup (name) 文件“/usr/local/lib/python3.7/site-packages/django/conf/init .py”,第 61 行,在 _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置。 You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。

Process finished with exit code 1进程以退出代码 1 结束

Empty suite空套房

PyCharm IDE setup PyCharm IDE 设置

My python interpreter is set correctly我的 python 解释器设置正确在此处输入图像描述

Research研究

I already looked at Pycharm - no tests were found?我已经看过Pycharm - 没有找到测试? . . Unrelated error, my test functions names start by test_*不相关的错误,我的测试函数名称以test_*开头

Based on your comment saying your django dependencies cannot be found, I suspect you didn't setup the python interpreter properly for your project.根据您说找不到 django 依赖项的评论,我怀疑您没有为您的项目正确设置 python 解释器。

Eg.例如。 if you use virtualenv, you need to setup your python interpreter to point to the python bin inside your virtualenv, so Pycharm can find your packages.如果你使用 virtualenv,你需要设置你的 python 解释器指向你 virtualenv 中的 python bin,所以 Pycharm 可以找到你的包。

Eg.例如。 photo below shows the python interpreter for my "pmas" virtualenv.下面的照片显示了我的“pmas”virtualenv 的 python 解释器。

在此处输入图像描述

在此处输入图像描述

can you try你能试一下吗

export DJANGO_SETTINGS_MODULE=mysites.settings

here, mysites is name of Django project where settings.py is available

paste the above line in bash shell before running standalone python script在运行独立 python 脚本之前,将上述行粘贴到 bash shell 中

The reason we use manage.py when working with Django is that it manages environment for us.我们在使用 Django 时使用manage.py的原因是它为我们管理环境。

But working with pycharm, unless you specify things manually, a python file that has test footprints in it (importing unittest and having method names startng with "test_") are just modules we can test without any external dependencies.但是使用 pycharm,除非您手动指定内容,否则其中包含测试足迹的 python 文件(导入 unittest 并具有以“test_”开头的方法名称)只是我们可以在没有任何外部依赖关系的情况下测试的模块。

in cases you use a framework such as Django, then you need to play by their rules.如果您使用诸如 Django 之类的框架,那么您需要遵守他们的规则。 you will either use their tools or set whatever is needed manually.您将使用他们的工具或手动设置所需的任何内容。

I don't know if pycharm had this support at the time but this pycharm/django-support may help.我不知道 pycharm 当时是否有这种支持,但这个pycharm/django-support可能会有所帮助。

if that does not cover your needs, then you will need to work on a few things mentioned in Django Settings (check for other versions if you use an older one, or newer if you are from the future).如果这不能满足您的需求,那么您将需要处理Django 设置中提到的一些事情(如果您使用旧版本,请检查其他版本,如果您来自未来,请检查新版本)。 your error message already mentions about this.您的错误消息已经提到了这一点。

Requested setting INSTALLED_APPS, but settings are not configured.
You must either define the environment variable DJANGO_SETTINGS_MODULE
or call settings.configure() before accessing settings.
  • you can either open project settings in pycharm and add this key-value in environment variables for it: DJANGO_SETTINGS_MODULE=mysite.settings您可以在 pycharm 中打开项目设置,并将此键值添加到环境变量中: DJANGO_SETTINGS_MODULE=mysite.settings

  • or use this in your code wherever it is needed.或在需要的地方在代码中使用它。 (check the settings page for details) (查看设置页面了解详情)

    from django.conf import settings
    settings.configure(DEBUG=True)

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

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