Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/JUnit.Xml.TestLogger/JunitXmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ private XElement CreateTestCaseElement(TestResultInfo result)
// seconds should be low enough it won't interfere with anyone monitoring test duration.
testcaseElement.SetAttributeValue("time", Math.Max(0.0000001f, result.Duration.TotalSeconds).ToString("0.0000000", CultureInfo.InvariantCulture));

if (!string.IsNullOrEmpty(result.CodeFilePath))
{
testcaseElement.SetAttributeValue("file", result.CodeFilePath);
}

if (result.LineNumber > 0)
{
testcaseElement.SetAttributeValue("line", result.LineNumber);
}

if (result.Outcome == TestOutcome.Failed)
{
var failureBodySB = new StringBuilder();
Expand Down
53 changes: 49 additions & 4 deletions test/JUnit.Xml.TestLogger.UnitTests/JUnitXmlTestSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,40 @@ public void TestSuiteShouldIncludeSystemErrElementWhenContentExists()
Assert.IsTrue(systemErrElement.Value.Contains("Error - Error message"));
}

[TestMethod]
public void TestCaseShouldIncludeFileAndLineAttributesWhenSourceIsPresent()
{
var serializer = new JunitXmlSerializer();
var result = CreateTestResultInfo();

var testCase = SerializeAndGetTestCaseElement(serializer, result);

Assert.AreEqual(TestCsPath, testCase.Attribute("file")?.Value);
Assert.AreEqual("42", testCase.Attribute("line")?.Value);
}

[TestMethod]
public void TestCaseShouldOmitFileAttributeWhenCodeFilePathIsMissing()
{
var serializer = new JunitXmlSerializer();
var result = CreateTestResultInfo(codeFilePath: null);

var testCase = SerializeAndGetTestCaseElement(serializer, result);

Assert.IsNull(testCase.Attribute("file"));
}

[TestMethod]
public void TestCaseShouldOmitLineAttributeWhenLineNumberIsMissing()
{
var serializer = new JunitXmlSerializer();
var result = CreateTestResultInfo(lineNumber: 0);

var testCase = SerializeAndGetTestCaseElement(serializer, result);

Assert.IsNull(testCase.Attribute("line"));
}

private static LoggerConfiguration CreateTestLoggerConfiguration()
{
return new LoggerConfiguration(new Dictionary<string, string>
Expand All @@ -237,7 +271,11 @@ private static TestRunConfiguration CreateTestRunConfiguration()
return new TestRunConfiguration { StartTime = DateTime.Now };
}

private static TestResultInfo CreateTestResultInfo(TestOutcome outcome = TestOutcome.Passed, List<TestResultMessage> messages = null)
private static TestResultInfo CreateTestResultInfo(
TestOutcome outcome = TestOutcome.Passed,
List<TestResultMessage> messages = null,
string codeFilePath = TestCsPath,
int lineNumber = 42)
{
return new TestResultInfo(
TestNamespace,
Expand All @@ -248,8 +286,8 @@ private static TestResultInfo CreateTestResultInfo(TestOutcome outcome = TestOut
TestDisplayName,
TestDisplayName,
TestDllPath,
TestCsPath,
42,
codeFilePath,
lineNumber,
DateTime.Now,
DateTime.Now.AddSeconds(1),
TimeSpan.FromSeconds(1),
Expand All @@ -262,7 +300,7 @@ private static TestResultInfo CreateTestResultInfo(TestOutcome outcome = TestOut
null);
}

private static string SerializeAndExtractElementContent(JunitXmlSerializer serializer, TestResultInfo result, string elementName)
private static XElement SerializeAndGetTestCaseElement(JunitXmlSerializer serializer, TestResultInfo result)
{
var xml = serializer.Serialize(
CreateTestLoggerConfiguration(),
Expand All @@ -272,6 +310,13 @@ private static string SerializeAndExtractElementContent(JunitXmlSerializer seria

var doc = XDocument.Parse(xml);
var testCaseElement = doc.XPathSelectElement("//testcase");
Assert.IsNotNull(testCaseElement, "testcase element not found");
return testCaseElement;
}

private static string SerializeAndExtractElementContent(JunitXmlSerializer serializer, TestResultInfo result, string elementName)
{
var testCaseElement = SerializeAndGetTestCaseElement(serializer, result);
var targetElement = testCaseElement.Element(elementName);

Assert.IsNotNull(targetElement, $"Element '{elementName}' not found in testcase");
Expand Down
2 changes: 2 additions & 0 deletions test/assets/JUnit.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Modifications to original xsd for this repo:
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="classname" type="xs:string" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
<xs:attribute name="file" type="xs:string" use="optional"/>
<xs:attribute name="line" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

Expand Down
Loading