Opened 23 months ago

Closed 23 months ago

Last modified 22 months ago

#33925 closed Bug (invalid)

Testing tools Management Commands example fails

Reported by: Eido Askayo Owned by: nobody
Component: Documentation Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I added the test code in the following link, into my Django 4.0 project tests.py:

from io import StringIO
from django.core.management import call_command
from django.test import TestCase

class ClosepollTest(TestCase):
    def test_command_output(self):
        out = StringIO()
        call_command('closepoll', stdout=out)
        self.assertIn('Expected output', out.getvalue())

The code fails with the following exception:

eido@:django-4.0-playground$ python mysite/manage.py test polls
Found 11 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E..........
======================================================================
ERROR: test_command_output (polls.tests.ClosepollTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "~/development/repositories/django-4.0-playground/mysite/polls/tests.py", line 145, in test_command_output
    call_command('closepoll', stdout=out)
  File "~/.local/share/virtualenvs/django-4.0-playground-KQUU8nfg/lib/python3.8/site-packages/django/core/management/__init__.py", line 176, in call_command
    defaults = parser.parse_args(args=parse_args)
  File "~/.local/share/virtualenvs/django-4.0-playground-KQUU8nfg/lib/python3.8/site-packages/django/core/management/base.py", line 66, in parse_args
    return super().parse_args(args, namespace)
  File "/usr/lib/python3.8/argparse.py", line 1780, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python3.8/argparse.py", line 1812, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python3.8/argparse.py", line 2046, in _parse_known_args
    self.error(_('the following arguments are required: %s') %
  File "~/.local/share/virtualenvs/django-4.0-playground-KQUU8nfg/lib/python3.8/site-packages/django/core/management/base.py", line 72, in error
    raise CommandError("Error: %s" % message)
django.core.management.base.CommandError: Error: the following arguments are required: poll_ids

----------------------------------------------------------------------
Ran 11 tests in 0.024s

FAILED (errors=1)
Destroying test database for alias 'default'...

Change History (2)

comment:1 by Mariusz Felisiak, 23 months ago

Resolution: invalid
Status: newclosed

Thanks for this ticket, however this is only a stub test and doesn't contain a source of closepoll command, it also doesn't refer to the implementation from How-to.

This section is intended to show that the call_command() function is useful for testing management commands. We shouldn't complicate it with the ORM calls and extra arguments.

in reply to:  1 comment:2 by Eido Askayo, 22 months ago

Replying to Mariusz Felisiak:

Thanks for this ticket, however this is only a stub test and doesn't contain a source of closepoll command, it also doesn't refer to the implementation from How-to.

This section is intended to show that the call_command() function is useful for testing management commands. We shouldn't complicate it with the ORM calls and extra arguments.

Thank you for your consideration.

Note: See TracTickets for help on using tickets.
Back to Top