在 PyCharm 中配置 Django 测试

问题描述 投票:0回答:1

我有一个在 pycharm 中制作的简单 django 项目。目录结构如下:

zelda_botw_cooking_simulator
|-- cooking_simulator_project
|---- manage.py
|---- botw_cooking_simulator   # django app
|------ init.py
|------ logic.py
|------ tests.py
|------ all_ingredients.py
|------ other standard django app files
|---- cooking_simulator_project  # django project

当我在 PyCharm 终端中运行

python manage.py test
时,一切都运行良好。

但是,当我单击 PyCharm 中测试旁边的小三角形图标来运行该测试时,我收到一条错误消息:

File ".../zelda_botw_cooking_simulator/cooking_simulator_proj/botw_cooking_simulator/tests.py", line 5, in <module>
    from .all_ingredients import all_ingredients
ImportError: attempted relative import with no known parent package

我该如何解决这个问题?

我已经尝试在 PyCharm 中配置运行环境和测试环境 30 分钟,但我没有得到它。问题/答案herehere很接近,但没有足够的细节让我修复它。我到底应该在每个窗口的每个字段中放置什么? “目标”、“工作目录”是什么,我需要环境变量吗?设置部分包含哪些内容以及配置部分包含哪些内容?

ChatGPT 推荐了一堆不起作用的东西,而且我似乎找不到显示执行此操作的正确方法的 YouTube 视频。谢谢!






**** 扩大对评论/问题的回答 ****

这是当我单击tests.py中第一个测试旁边的小三角形图标时收到的错误的全文:

/opt/homebrew/anaconda3/envs/zelda_botw_cooking_simulator/bin/python /Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py test botw_cooking_simulator.tests.TestAllIngredients.test_hearty_durian /Users/brendenmillstein/Dropbox (Personal)/BSM_Personal/Coding/BSM_Projects/zelda_botw_cooking_simulator/cooking_simulator_proj 
Testing started at 10:05 PM ...
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py", line 168, in <module>
    utility.execute()
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py", line 142, in execute
    _create_command().run_from_argv(self.argv)
  File "/opt/homebrew/anaconda3/envs/zelda_botw_cooking_simulator/lib/python3.10/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "/opt/homebrew/anaconda3/envs/zelda_botw_cooking_simulator/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/homebrew/anaconda3/envs/zelda_botw_cooking_simulator/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py", line 104, in handle
    failures = TestRunner(test_labels, **options)
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_runner.py", line 254, in run_tests
    return DjangoTeamcityTestRunner(**options).run_tests(test_labels,
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_runner.py", line 156, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
TypeError: DiscoverRunner.run_tests() takes 2 positional arguments but 3 were given

Process finished with exit code 1

这是tests.py代码:

from datetime import timedelta

from django.test import TestCase

from .all_ingredients import all_ingredients
from .data_structures import MealType, MealResult, MealName, SpecialEffect, EffectLevel
from .logic import (
    determine_meal_type,
    simulate_cooking,
    check_if_more_than_one_effect_type,
    calculate_sell_price,
)

# Create your tests here.
class TestAllIngredients(TestCase):
    def test_hearty_durian(self):
        ingredient = all_ingredients["Hearty Durian"]
        self.assertEqual(ingredient.effect_type.value, "Hearty")
        self.assertEqual(ingredient.category.value, "Fruit")
        self.assertEqual(ingredient.price, 15)
        self.assertEqual(ingredient.base_hp, 12)
        self.assertEqual(ingredient.bonus_hp, 0)
        self.assertEqual(ingredient.base_time, timedelta(seconds=00))
        self.assertEqual(ingredient.bonus_time, timedelta(seconds=00))
        self.assertEqual(ingredient.potency, 4)

这是配置的屏幕截图: Configuration for first test method (not class)

最后,这也可能有帮助:我设置了一个名为

scratchy.py
的简单文件,并且我在 PyCharm 中进行了配置来运行它。下面是终端中的配置和完整脚本/输出的屏幕截图,因此这只是我无法正确配置的测试。

Scratchy.py

PyCharm Configuration for Scratchy

谢谢!!

python django pycharm django-testing django-tests
1个回答
0
投票

这个错误只有2种可能:

  1. 要么没有环境变量,要么配置不正确
  2. all_ingredients 的路径是相对路径,尝试将其更改为绝对路径,将
    from .all_ingredients import all_ingredients
    更改为
    from botw_cooking_simulator.all_ingredients import all_ingredients

我非常愿意尝试并检查第二种解决方案。您还可以检查错误:

File ".../zelda_botw_cooking_simulator/cooking_simulator_proj/botw_cooking_simulator/tests.py", line 5, in <module>
    from .all_ingredients import all_ingredients
ImportError: attempted relative import with no known parent package

这也指向同一件事:

ImportError: attempted relative import with no known parent package

© www.soinside.com 2019 - 2024. All rights reserved.