What is the most readable way to check a variable equals a string, which contains multiple spaces.
For example if I have a text file (/tmp/a.txt) that contains the following text I wish to test:
String with 4 spaces
I can use the strip_spaces
and collapse_spaces
arguments of Builtin.Should Be Equal
to test that it contains the correct words:
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Check String With Spaces
${a} = Get File /tmp/a.txt
Log ${a} console=True
Should Be Equal ${a} String with 4 spaces strip_spaces=True collapse_spaces=True
But say my tests are space sensitive, I want any text in ${a}
that has more or less than 4 spaces to cause a failure. For example I need to check that the space padding of a date value is correct.
I could store the String with 4 spaces
in a text file as well and call it something like a.txt.expected
.
*** Test Cases ***
Check String With Spaces
${a} = Get File /tmp/a.txt
${expected} = Get File a.txt.expected
Should Be Equal ${a} ${expected}
But my issue with that is it makes the test less readable because you then need to go and look in another file to see what the expected value is. If I have lots of strings to test that’s lots of other files to go and look in.
Similarly I could implement a Python robot library keyword like:
text_contains_expected_value(text: str):
assert text == "String with 4 spaces"
My robot test then would look like:
*** Test Cases ***
Check String With Spaces
${a} = Get File /tmp/a.txt
Log ${a} console=True
Text Contains Expected Value ${a}
But that has the same issue. A casual reader of the .robot
file needs to go and look in another file to see what the expected value is.
ChatGPT tells me I can use a string enclosed in triple quotes like so:
*** Test Cases ***
Check String With Spaces
${a} = Get File /tmp/a.txt
Log ${a} console=True
Should Be Equal ${a} """String with 4 spaces"""
But that gives me """String with
as the first argument and 4 spaces"""
as the second argument, resulting in an error.