Pragmatists/JUnitParams

Test parameters are not properly escaped in test names

dimas opened this issue · 0 comments

dimas commented

The testnames generated by JUnitParams do not escape special characters.
When these parameters

    public static Object[][] escapeSequences() {
        return new Object[][]{
//...
                {"X"},
                {"\b"},  //   \b => backspace
                {"\f"},  //   \f => formfeed

are fed into through

    @Test
    @Parameters(method = "escapeSequences")
    public void testEscaping(final String text) {
    }

The test works as it should but the XML report generated by JUnit fails to be parsed by majority of XML parsers because these characters just go into the test case name. In the file target/surefire-reports/TEST-test.JsonParserTest.xml I can see:

  <testcase name="testEscaping(X) [2]" classname="test.JsonParserTest" time="0"/>
  <testcase name="testEscaping() [3]" classname="test.JsonParserTest" time="0"/>
  <testcase name="testEscaping(^L) [4]" classname="test.JsonParserTest" time="0"/>

I cannot copypaste it properly but there is actually a character with code 08 on the second line and 0A on the last. Each making XML invalid.

You can argue that it is is JUnit that does not do proper escaping when writing XML but it probably just does not expect test names (which used to be just method) names to contain anything like that.

After all, if your parameter is of type String I would expect it to go as a proper Java string into the test name - quoted and with every character that should be escaped in Java string to be escaped:

  <testcase name="testEscaping("X") [2]" classname="test.JsonParserTest" time="0"/>
  <testcase name="testEscaping("\b") [3]" classname="test.JsonParserTest" time="0"/>
  <testcase name="testEscaping("\f") [4]" classname="test.JsonParserTest" time="0"/>

Cheers

PS: reproduced with 1.1.1