Django testing views - getting error - DiscoverRunner.run_tests() takes 2 positional arguments but 3 were given
Asked Answered
M

2

5

I use Django framework to create basic web application. I started to write tests for my views. I followed the django documentation and fixed some issues along the way. But now I am stuck - I don't know why I get this error even after 30 minutes of searching for answer.

    C:\Osobni\realityAuctionClient\venv\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm 2022.3.3/plugins/python/helpers/pycharm/django_test_manage.py" test realityAuctionClientWeb.test_views.TestViews.test_start_view C:\Osobni\realityAuctionClient 
Testing started at 7:41 ...
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pycharm\django_test_manage.py", line 168, in <module>
    utility.execute()
  File "C:\Program Files\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pycharm\django_test_manage.py", line 142, in execute
    _create_command().run_from_argv(self.argv)
  File "C:\Osobni\realityAuctionClient\venv\Lib\site-packages\django\core\management\commands\test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "C:\Osobni\realityAuctionClient\venv\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Osobni\realityAuctionClient\venv\Lib\site-packages\django\core\management\base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pycharm\django_test_manage.py", line 104, in handle
    failures = TestRunner(test_labels, **options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pycharm\django_test_runner.py", line 254, in run_tests
    return DjangoTeamcityTestRunner(**options).run_tests(test_labels,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm 2022.3.3\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

Empty suite

My tests.py looks like this:

 import django
from django.test import TestCase
from django.urls import reverse


class TestViews(TestCase):

    def test_start_view(self):
        """
        Test that the start view returns a 200 response and uses the correct template
        """
        django.setup()
        url = reverse("start")
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)

I use python 3.11 and django 5.0a1. Anybody who has a clue? I think it is somehow connected with settings but I do not know how.

Monaural answered 4/10, 2023 at 5:51 Comment(1)
JetBrains officially released a fix for Django 5 test, just update your IDE version (PyCharm 2023.3.2)Redhot
B
11

Django removed the parameter extra_tests in Django 5.0 and PyCharm's test runner is still providing it.

See django's release notes:

The extra_tests argument for DiscoverRunner.build_suite() and DiscoverRunner.run_tests() is removed.

You could downgrade to django 4.2 until this is fixed by Jetbrains.

A Pycharm issue regarding this deprecation exists, see PY-53355.

Beslobber answered 22/10, 2023 at 21:52 Comment(1)
It is now fixed in version 2023.3.2 (youtrack.jetbrains.com/issue/PY-53355/…)Beslobber
B
2

To not downgrade to previous Django version, you can make small fix to your django package:

Example path:

some_path_to/virtualenv/lib/python3.11/site-packages/django/test/runner.py (probably at line 1044)

def run_tests(self, test_labels, some=None, **kwargs):

add any parameter like some=None

Brittabrittain answered 26/6 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.