Skip to content

"sfdx force:apex:test:run" and "sfdx force:apex:test:report" generate invalid XML when using JUNIT output #280

@jverelst

Description

@jverelst

Hello,

I encounterd an error with the force:apex:test:run and force:apex:test:report commands, where the output creation of a JUNIT xml generates an invalid XML. This is because messages containing a double-quote don't get escaped.

We are using this command in an automated pipeline in Azure, where we want to publish the testresults, but Azure cannot parse the XML because it is not valid.

I have first updated SFDX before submitting this issue:

# sfdx update
sfdx-cli: Updating CLI from 7.132.0-6621068 to 7.142.1-05bbd4a... done
sfdx-cli: Updating CLI... done
sfdx-cli: Updating sf... done

Steps to reproduce

Create the following test class:

@IsTest
public class TestJunit {

    @IsTest
    public static void generateIncorrectOutput() {
        throw new CustomException('This message contains "Double quotes"');
    }
    
    
    class CustomException extends Exception {
        
    }
}

Run the testclass with JUNIT output

sfdx force:apex:test:run -n TestJunit -r junit

This generates:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
    <testsuite name="force.apex" timestamp="2022-03-14T07:10:00.000Z" hostname="https://force-site-2505.cs189.my.salesforce.com" tests="1" failures="1"  errors="0"  time="0.02">
        <properties>
            <property name="outcome" value="Failed"/>
            <property name="testsRan" value="1"/>
            <property name="passing" value="0"/>
            <property name="failing" value="1"/>
            <property name="skipped" value="0"/>
            <property name="passRate" value="0%"/>
            <property name="failRate" value="100%"/>
            <property name="testStartTime" value="Mon Mar 14 2022 08:10:00"/>
            <property name="testExecutionTime" value="0.02 s"/>
            <property name="testTotalTime" value="0.02 s"/>
            <property name="commandTime" value="0.11 s"/>
            <property name="hostname" value="https://.... "/>
            <property name="orgId" value="00D7a0000001AdvEAE"/>
            <property name="username" value="test-......@example.com"/>
            <property name="testRunId" value="7077a000038XYJ8"/>
            <property name="userId" value="0057a....."/>
        </properties>
        <testcase name="generateIncorrectOutput" classname="TestJunit" time="0.02">
            <failure message="TestJunit.CustomException: This message contains "Double quotes""><![CDATA[Class.TestJunit.generateIncorrectOutput: line 6, column 1]]></failure>
        </testcase>
    </testsuite>
</testsuites>

If you try to parse this XML (for instance using sfdx force:apex:test:run -n TestJunit -r junit | xmllint -) you will get:

-:23: parser error : attributes construct error
            <failure message="TestJunit.CustomException: This message contains "
                                                                               ^
-:23: parser error : Couldn't find end of Start Tag failure line 23
            <failure message="TestJunit.CustomException: This message contains "
                                                                               ^
-:23: parser error : Opening and ending tag mismatch: testcase line 22 and failure
"><![CDATA[Class.TestJunit.generateIncorrectOutput: line 6, column 1]]></failure
                                                                               ^
-:24: parser error : Opening and ending tag mismatch: testsuite line 3 and testcase
        </testcase>
                   ^
-:25: parser error : Opening and ending tag mismatch: testsuites line 2 and testsuite
    </testsuite>
                ^
-:26: parser error : Extra content at the end of the document
</testsuites>
^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions