Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#23543 closed Cleanup/optimization (fixed)

Documentation how to test custom management command missing

Reported by: wittwerch Owned by: Danilo Bargen
Component: Documentation Version: 1.7
Severity: Normal Keywords:
Cc: Lagovas, mail@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation about custom management command misses a few points how to properly test them.
=> https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/

For example the documentation states that you should use self.stdout: "By using these proxies, it becomes much easier to test your custom command"
But how to actually test the output is missing. A complete example with call_command would be great!

Change History (11)

comment:1 Changed 4 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Lagovas

You can test commands like any other function. Just

from django.core.management import call_command
class SomeTest(TestCase):
    def test_command(self):
        call_command('your_command', *args, **kwargs) 
        self.assertTrue(somethingDone())

and test result or behavior of your command. I think no sense for writing how to test all functions or methods from docs.

comment:3 Changed 4 years ago by Lagovas

Cc: Lagovas added

comment:4 Changed 4 years ago by Claude Paroz

I think an example wouldn't hurt, and show how you can catch the command output with six.StringIO (see tests/user_commands/tests.py).

comment:5 Changed 4 years ago by Danilo Bargen

Owner: changed from nobody to Danilo Bargen
Status: newassigned

comment:6 Changed 4 years ago by Danilo Bargen

Cc: mail@… added
Has patch: set

comment:7 Changed 4 years ago by Tim Graham

Patch needs improvement: set

Comments for improvement are on the PR.

comment:8 Changed 4 years ago by Tim Graham

Patch needs improvement: unset

comment:9 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In cdee8659763ee7044c1507bcd2202581b1744f0b:

Fixed #23543 -- Added docs on testing management command output.

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

In 60cf46fdf5176eb24ba4d47441ed24104f1493cc:

[1.7.x] Fixed #23543 -- Added docs on testing management command output.

Backport of cdee8659763ee7044c1507bcd2202581b1744f0b from master

comment:11 Changed 4 years ago by Tim Graham <timograham@…>

In 276332d85c76a4de9fc1cb4bcb2f1636c0322414:

[1.6.x] Fixed #23543 -- Added docs on testing management command output.

Backport of cdee8659763ee7044c1507bcd2202581b1744f0b from master

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