Skip to content

cmd2.transcript

cmd2.transcript

Machinery for running and validating transcripts.

If the user wants to run a transcript (see docs/transcript.rst), we need a mechanism to run each command in the transcript as a unit test, comparing the expected output to the actual output.

This file contains the class necessary to make that work. This class is used in cmd2.py::run_transcript_tests()

Cmd2TestCase

Bases: TestCase

A unittest class used for transcript testing.

Subclass this, setting CmdApp, to make a unittest.TestCase class that will execute the commands in a transcript file and expect the results shown.

See transcript_example.py

cmdapp class-attribute instance-attribute

cmdapp = None

setUp

setUp()

Instructions that will be executed before each test method.

Source code in cmd2/transcript.py
def setUp(self) -> None:
    """Instructions that will be executed before each test method."""
    if self.cmdapp:
        self._fetch_transcripts()

        # Trap stdout
        self._orig_stdout = self.cmdapp.stdout
        self.cmdapp.stdout = cast(TextIO, utils.StdSim(self.cmdapp.stdout))

tearDown

tearDown()

Instructions that will be executed after each test method.

Source code in cmd2/transcript.py
def tearDown(self) -> None:
    """Instructions that will be executed after each test method."""
    if self.cmdapp:
        # Restore stdout
        self.cmdapp.stdout = self._orig_stdout

runTest

runTest()

Override of the default runTest method for the unittest.TestCase class.

Source code in cmd2/transcript.py
def runTest(self) -> None:  # was testall  # noqa: N802
    """Override of the default runTest method for the unittest.TestCase class."""
    if self.cmdapp:
        its = sorted(self.transcripts.items())
        for fname, transcript in its:
            self._test_transcript(fname, transcript)