Skip to content

Presentation error

Vladimir Turov edited this page Apr 21, 2020 · 2 revisions

About

Often, when people write tests, it turns out that they did not take into account all kinds of outputs that the user program could print. The most common errors happen:

  • When tests crash with the ArrayOutOfBoundsException error, when the user printed 9 numbers instead of 10, and this was not checked in tests and assumed that user's program will print 10 numbers exactly.
  • When tests crash with the error NumberFormatException, when the user printed not only a number, but the line "The number is: 42" and tests tried to parse user's output as a number.
  • When the user did not use certain characters to divide the output into sections that are easier to parse in the test code.
  • When the user didn't output anything and test didn't take it into account.
  • Etc...

In this case it is better to show that the user program did not output the data as expected by the tests. To automatically check the text for various matches, there is a class called Expectation. This class can automatically extract the necessary information from the text, and if there is no such information, it can show the user the Presentation error with the text that is easy to understand why it happened.

This feature is present in Java library and not in Python library yet.

Examples

Firstly, you need a single static import to use this feature:

import static org.hyperskill.hstest.testing.expect.Expectation.expect;

If you want to automatically extract information from this output and test if for presentation error, you firstly need to specify amount of things you are searching for. Expectation builder provides several methods for that:

Where:

  • toContain() method checks that a least one thing is present in the output (> 0).
  • toContain(int) method checks that exact number of things is present in the output.
  • ..AtLeast, .. AtMost, ..Between, ..LessThan, ..MoreThat methods check that number of things ≥ N, ≤ N, N ≤ .. ≤ M, < N, > N respectively.
  • toContain(Function) that checks for a specific more complex condition.

Then, you need to specify what things you need to search for:

Notice that every method returns a List of things it found in the output. If the number of things is not equal to the specified amount the Presentation error is shown to the user automatically with readable feedback. So, you are guaranteed to get a number of things you actually expect.

  • lines() checks that number of lines in the text is in the expected zone and then returns them.
  • words() method checks that the string contains a correct number of words separated by a space and then returns them.
  • words(String) method checks that the string contains a correct number of words separated by a specific delimiter and then returns them.
  • wordsFrom(String...) method checks that the string contains words from the list. For example "yes" or "no" and also combined with toContain(1) you can achieve only single "yes" or only single "no" in the string, which might be useful because the user program could print them both to cheat tests. Every word in the list can also be a regular expression since that's how it's checked.
  • wordsOnlyFrom(String...) checks that the string contains only words from the list with a space as a delimiter.
  • integers() method searches for integers in the text. Text may contain other data. For example in the output: "Numbers: 12, 23, 34" this method will return a list with 3 integer elements if you expect such amount.
  • integersOnly() method checks that the text contain only integers with space delimiter.
  • integersOnly(String) method checks that the text contain only integers with custom delimiter, for example comma.
  • doubles() is similar to integers() but searches for doubles. And returns list of doubles at the end.
  • doublesOnly() method in similar to integersOnly().
  • doublesOnly(String) method is similar to integersOnly(String).

The other methods is for internal use, but you can try to create a custom checker with them. If you want to check something useful that is not possible with all the methods above (maybe even without specifying amount) you can contact Hyperskill team or create an issue in this repository.

Please consider using this approach to check user's output automatically.