test_output_contains¶
-
test_output_contains
(text, pattern=True, no_output_msg=None, state=None)¶ Test the output.
Tests if the output contains a (pattern of) text.
Parameters: - text (str) – the text that is searched for
- pattern (bool) – if True, the text is treated as a pattern. If False, it is treated as plain text. Defaults to False.
- no_output_msg (str) – feedback message to be displayed if the output is not found.
test_output_contains(text,
pattern=True,
no_output_msg=None)
We can test the output of the student contains with test_output_contains()
. This function will compare the given text with the text in the student’s output and see if we have a match. You can use regular expressions or not, that’s completely up to you.
Here’s an example of an exercise with test_student_typed()
, suppose the solution looks like this:
*** =solution
```{python}
# Print the "This is some ... stuff" to the shell
print("This is some weird stuff")
```
The following SCT tests whether the student outputs This is some weird stuff
:
*** =sct
```{python}
test_output_contains("This is some weird stuff", pattern = False)
success_msg("Great job!")
```
Notice that we set pattern
to False
, this will cause test_output_contains()
to search for the pure string, no patterns are used. This SCT is not robust, because it won’t be accepted if the student submits print("This is some cool stuff")
, for example. Therefore, it’s a good idea to use regular expressions. pattern=True
by default, so there’s no need to specify this:
*** =sct
```{python}
test_output_contains(/This is some \w* stuff/,
no_output_msg = "Print out `This is some ... stuff` to the output, fill in `...` with a word you like.")
success_msg("Great job!")
```
Now, different printouts will be accepted. Notice that we also specified no_output_msg
here. If the pattern is not found in the output generated, this message will be shown instead of a message that’s automatically generated by pythonwhat
.