`call_command` raises ValueError during testing

345 views
Skip to first unread message

Ferran Jovell

unread,
Oct 2, 2022, 7:49:26 AM10/2/22
to django-d...@googlegroups.com
Hello everyone,

I was working on a admin command today, and when setting up tests and fixtures for running the command during testing I found this error:

Traceback (most recent call last):
  File "/home/fjm/code/django-stuff/tutorial/polls/tests.py", line 10, in test_command_output
    call_command("closepoll", poll_ids=1, stdout=out)
  File "/home/fjm/.local/share/virtualenvs/tutorial/lib/python3.10/site-packages/django/core/management/__init__.py", line 168, in call_command
    parse_args.append(min(opt.option_strings), default=0)
ValueError: min() arg is an empty sequence

However, what is weird is that when run from with the manage.py script, everything seems to be working just fine.

This is the command code

from django.core.management.base import BaseCommand


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument("poll_ids", nargs="+", type=int)

    def handle(self, *args, **options):
        pass

And this is the test code:
from io import StringIO

from django.core.management import call_command
from django.test import SimpleTestCase


class ClosepollTest(SimpleTestCase):
    def test_command_output(self):
        out = StringIO()
        call_command("closepoll", poll_ids=1, stdout=out)
        self.assertIn("Expected output", out.getvalue())

I set up a repo with the minimal code to reproduce this:https://github.com/mrswats/django-polls-test
Is this a bug? Am I doing something wrong? Have I missed anything?

I checked the bug tracker but could not find anything related to this and before opening an issue I figured I'd ask. If it is indeed a bug I will happily write a ticket in the tracker.

Thanks in advance.

Tim Graham

unread,
Oct 2, 2022, 3:39:23 PM10/2/22
to Django developers (Contributions to Django itself)
Hi Ferran,

It looks like opt.option_strings is empty because "poll_ids" (in parser.add_argument()) isn't prefixed with a dash or double dash.

You could instead use call_command() like this: call_command("closepoll", 1)

But I think your invocation could be fixed (or at least not fail so obscurely).

Ferran Jovell

unread,
Oct 3, 2022, 3:06:26 AM10/3/22
to django-d...@googlegroups.com
Hi Tim,

As far as I understand, when adding an argument with `argparser` without dashes, argparse interprets it as a positional argument and not an option, which is how it works when you call it from the terminal.
However, calling `call_command` with args instead of keyword arguments seems to skip that step, so thanks!

And checking again the documentation for call_command it is true that the examples reflect that although this error message is indeed very obscure.
How can we improve this error reporting on the code side so that it's not as obscure?

Thanks a lot,

Ferran

Missatge de Tim Graham <timog...@gmail.com> del dia dg., 2 d’oct. 2022 a les 21:39:
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/da3b35c5-eaed-49e8-9b61-eec4b3fc4736n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages