Skip to content

Commit cc8861b

Browse files
committed
Refactor: use string builder for error messages, update test name, and add attachment test
1 parent a62564c commit cc8861b

File tree

9 files changed

+56
-11
lines changed

9 files changed

+56
-11
lines changed

src/LiquidTestReports.Core/Mappers/JUnitMapper.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
using LiquidTestReports.Core.Models;
44
using System;
55
using System.Collections.Generic;
6+
using System.Text;
67

78
namespace LiquidTestReports.Core.Mappers
89
{
9-
public class JUnitMapper
10+
public static class JUnitMapper
1011
{
1112
public static void Map(Testsuites source, TestRunDrop destination, ReportInput inputConfiguration = null)
1213
{
@@ -73,22 +74,20 @@ public static void Map(Testsuites source, TestRunDrop destination, ReportInput i
7374
drop.ExecutedTestsCount++;
7475
drop.Results.Add(resultDrop);
7576
}
76-
77-
7877
}
7978
}
8079
private static void MapOutputToResult(Testcase test, TestResultDrop drop)
8180
{
8281
var messages = new List<TestResultMessageDrop>();
83-
var errorMessage = string.Empty;
84-
var errorStackTrace = string.Empty;
82+
var errorMessage = new StringBuilder();
83+
var errorStackTrace = new StringBuilder();
8584

8685
if (test.FailureSpecified)
8786
foreach (var failure in test.Failure)
8887
if (!string.IsNullOrEmpty(failure.Message))
8988
{
90-
errorMessage += failure.Message;
91-
errorStackTrace += string.Join(Environment.NewLine, failure.Text);
89+
errorMessage.Append(failure.Message);
90+
errorStackTrace.Append(string.Join(Environment.NewLine, failure.Text));
9291
}
9392

9493
if (test.System_OutSpecified)
@@ -102,8 +101,8 @@ private static void MapOutputToResult(Testcase test, TestResultDrop drop)
102101
messages.Add(new TestResultMessageDrop { Text = stdErr, Category = "Error" });
103102

104103
drop.Messages = messages;
105-
drop.ErrorMessage = errorMessage;
106-
drop.ErrorStackTrace = errorStackTrace;
104+
drop.ErrorMessage = errorMessage.ToString();
105+
drop.ErrorStackTrace = errorStackTrace.ToString();
107106
}
108107

109108
private static string MapOutcome(Testcase test, TestResultSetDrop setDrop, TestRunStatisticsDrop testRunStatistics)

test/LiquidTestReports.Cli.Tests/ProgramTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void Main_WithTitle_GeneratesReport()
9898
public void Main_JUnitWithTitle_GeneratesReport()
9999
{
100100
//Arrange
101-
var title = "My JUnit Tests";
101+
var title = "My JUnit + TRX Tests";
102102
var titleTest = "junitTest.md";
103103
var destinationReport = new FileInfo(Path.Combine(_outputFolder, titleTest));
104104
var files = new List<ReportInput>();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.IO;
3+
using System.Reflection;
4+
5+
namespace SampleProject.MSTest
6+
{
7+
[TestClass]
8+
public class AttachmentTest
9+
{
10+
[TestMethod]
11+
[TestProperty("ReportName", "Image")]
12+
public void AttachImage()
13+
{
14+
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
15+
TestContext.WriteLine("Attach an image");
16+
TestContext.AddResultFile(Path.Combine(basePath, "resources", "attachmentSample.png"));
17+
Assert.IsTrue(true);
18+
}
19+
public TestContext TestContext { get; set; }
20+
}
21+
}
9.47 KB
Loading

test/SampleProject/SampleProject.Tests.MSTest/SampleProject.MSTest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22+
<None Update="Resources\attachmentSample.png">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
2225
<None Update="Resources\TemplateExample.txt">
2326
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2427
</None>

test/SampleProject/SampleProject.Tests.MSTest/TestServiceTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace SampleProject.MSTest
55
[TestClass]
66
public class TestServiceTests
77
{
8-
98
/// <summary>
109
/// Gets or sets the test context which provides
1110
/// information about and functionality for the current test run.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.IO;
4+
using System.Reflection;
5+
6+
namespace SampleProject.NUnit
7+
{
8+
public class Attachment
9+
{
10+
[Test]
11+
[Category("Image")]
12+
public void AttachImage()
13+
{
14+
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
15+
TestContext.WriteLine("Attach an image");
16+
TestContext.AddTestAttachment(Path.Combine(basePath, "resources", "attachmentSample.png"), "screenshot");
17+
Assert.True(true);
18+
}
19+
}
20+
}
9.47 KB
Loading

test/SampleProject/SampleProject.Tests.NUnit/SampleProject.NUnit.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25+
<None Update="Resources\attachmentSample.png">
26+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
27+
</None>
2528
<None Update="Resources\TemplateExample.txt">
2629
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2730
</None>

0 commit comments

Comments
 (0)