@@ -3,9 +3,10 @@ Transcript based testing
33========================
44
55A transcript is both the input and output of a successful session of a
6- ``cmd2 ``-based app which is saved to a text file. The transcript can be played
7- back into the app as a unit test. You can embed regular expressions into the
8- transcript to accomodate commands that produce dynamic or variable output.
6+ ``cmd2 ``-based app which is saved to a text file. With no extra work on your
7+ part, your app can play back these transcripts as a unit test. Transcripts can
8+ contain regular expressions, which provide the flexibility to match responses
9+ from commands that produce dynamic or variable output.
910
1011
1112Creating a transcript
@@ -24,13 +25,19 @@ Here's a transcript created from ``python examples/example.py``:
2425 (Cmd) mumble maybe we could go to lunch
2526 well maybe we could like go to er lunch right?
2627
27- This transcript has three commands: you can see them on the lines that begin
28- with the prompt, which in this case is ``(Cmd) ``. Following each command is
29- the output generated by that command.
28+ This transcript has three commands: they are on the lines that begin with the
29+ prompt. The first command looks like this:
3030
31- Any lines in the transcript before the first line that begins with the prompt
32- are ignored. You can take advantage of this by using the first lines of the
33- transcript as comments.
31+ .. code-block :: none
32+
33+ (Cmd) say -r 3 Goodnight, Gracie
34+
35+
36+ Following each command is the output generated by that command.
37+
38+ The transcript ignores all lines in the file until it reaches the first line
39+ that begins with the prompt. You can take advantage of this by using the first
40+ lines of the transcript as comments.
3441
3542.. code-block :: none
3643
@@ -51,12 +58,18 @@ transcript as comments.
5158
5259 In this example I've used several different commenting styles, and even bare
5360text. It doesn't matter what you put on those beginning lines. Everything before
54- the first line that starts with ``(Cmd) `` will be ignored.
61+
62+ .. code-block :: none
63+
64+ (Cmd) say -r 3 Goodnight, Gracie
65+
66+ will be ignored.
5567
5668If we used this transcript as-is, it would likely fail. As you can see, the
5769``mumble `` command doesn't always return the same thing. The ``mumble `` command
58- inserts random words into the input. Transcripts can include regular
59- expressions as a way to check for output that can change.
70+ inserts random words into the input.
71+
72+
6073
6174Regular expressions can be included in the response portion of a transcript,
6275and are surrounded by slashes.
@@ -69,38 +82,52 @@ and are surrounded by slashes.
6982 /.*\bmaybe\b.*\bcould\b.*\blunch\b.*/
7083
7184 Without creating a tutorial on regular expressions, this one matches anything
72- that has the words `maybe `, `could `, and `lunch ` in that order. It doesn't
73- ensure that `we ` or `go ` or `to ` appear in the output, but it does work if
85+ that has the words `` maybe `` , `` could `` , and `` lunch ` ` in that order. It doesn't
86+ ensure that `` we `` or `` go `` or `` to ` ` appear in the output, but it does work if
7487mumble happens to add words to the beginning or the end of the output.
7588
7689Since the output could be multiple lines long, ``cmd2 `` uses multiline regular
77- expression matching, and also uses the ``DOTALL `` flag, which subtly changes the behavior of commonly
78- used special characters like `. `, `^ ` and `$ `, so you may want to double check the
79- `Python regular expression documentation
80- <https://docs.python.org/3/library/re.html> `_.
90+ expression matching, and also uses the ``DOTALL `` flag, which subtly changes
91+ the behavior of commonly used special characters like ``. ``, ``^ `` and ``$ ``,
92+ so you may want to double check the `Python regular expression documentation
93+ <https://docs.python.org/3/library/re.html> `_. You also need to be careful when
94+ using ``\Z ``, it matches after the newline at the end of the string.
8195
8296If your output has slashes in it, you will need to escape those slashes so the
83- stuff between them is not interpred as a regular expression. In this transcript::
97+ stuff between them is not interpred as a regular expression. In this transcript:
98+
99+ .. code-block :: none
84100
85101 (Cmd) say cd /usr/local/lib/python3.6/site-packages
86102 /usr/local/lib/python3.6/site-packages
87103
88104 the output contains slashes. The text between the first slash and the second
89- slash, (`` usr ``) will be interpreted as a regular expression, and those two
105+ slash, will be interpreted as a regular expression, and those two
90106slashes will not be included in the comparison. When replayed, this transcript
91107would therefore fail. To fix it, we could either write a regular expression to
92- match the path instead of specifying it verbatim, or we can escape the slashes::
108+ match the path instead of specifying it verbatim, or we can escape the slashes:
109+
110+ .. code-block :: none
93111
94112 (Cmd) say cd /usr/local/lib/python3.6/site-packages
95113 \/usr\/local\/lib\/python3.6\/site-packages
96114
97- .. note ::
115+ .. warning ::
98116
99- Be careful with trailing spaces and newlines. Some terminal emulators strip
100- trailing space when you copy text from them. This could make the actual data
101- generated by your app different than the text you pasted into the
102- transcript, and it might not be readily obvious why the transcript is not
103- passing.
117+ Be aware of trailing spaces and newlines. Your commands might output
118+ trailing spaces which are impossible to see.
119+
120+ Some terminal emulators strip trailing space when you copy text from them.
121+ This could make the actual data generated by your app different than the
122+ text you pasted into the transcript, and it might not be readily obvious why
123+ the transcript is not passing.
124+
125+ If you aren't using regular expressions, make sure the newlines at the end
126+ of your transcript exactly match the output of your commands. A common cause
127+ of a failing transcript is an extra or missing newline.
128+
129+ If you are using regular expressions, be aware that depending on how you
130+ write your regex, the newlines after the regex may or may not matter.
104131
105132
106133Running a transcript
@@ -119,8 +146,8 @@ back and check the output. From within the ``examples/`` directory:
119146 OK
120147
121148 The output will look familiar if you use ``unittest ``, because that's exactly
122- what happens. Each command in the transcript is run, and the output is
123- `` asserted `` to match expected result from the transcript.
149+ what happens. Each command in the transcript is run, and we `` assert `` the
150+ output matches the expected result from the transcript.
124151
125152.. note ::
126153
0 commit comments