pythondjangopycharmdjango-testingdjango-tests

Configuring Django Testing in PyCharm


I have a simple django project that I'm making in pycharm. The directory structure is the following:

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
|------ manage.py
|------ other standard django project files

When I run python manage.py test in the PyCharm terminal, everything works great.

When I click the little triangle icon in PyCharm next to a test to run that test, however, I get one of two errors depending on how I've tried to configure the configuration for testing in PyCharm:

Error 1:

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

Error 2:

/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

How can I fix this?

I have tried configuring run environments and test environments in PyCharm for 2 hours now and I'm not getting it. The questions/answers here and here are close, but there's not quite enough detail for me to fix it. Exactly what do I put in each field in each window? What's the 'target', the 'working directory', do I need an environment variable? What goes in the settings part and what goes in the configuration?

ChatGPT recommended a bunch of stuff that didn't work, and I can't seem to find a YouTube video showing the right way to do this. Thank you!






**** Expanding Answer in Response to Comments/Questions **** Here is my tests.py code:

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)
etc.

Here is a screenshot of the configuration: Configuration for first test method (not class)

Thank you!!


Solution

  • Figured it out! The problem lay in how PyCharm was interpreting the test. The question and answer here was super helpful, but I had to do the opposite and add a working directory:

    1. Replace from django.test import TestCase with from unittest import TestCase in my tests.py file.

    2. Add the working directory to the templates. Note I had to update the autodetect template, not only the unittest template:

    First: select Edit Configurations from the Run menu Make sure to select 'Edit Configuration Templates', don't just update one.

    Second: select autodetect within the Python tests menu option. Python tests autodetect Then update the working directory to be the one with your manage.py file in it.

    Now the little green buttons work!