Skip to content

Commit

Permalink
Small nits in code convention / standards (#36)
Browse files Browse the repository at this point in the history
* Removed contributors from ReadMe.md

* Fixed small nits in JUnitBuilder

* JUnitBuilderTests/Build.cs changed a bit

* Removed unused usings in samples/XUnitSample/SimpleTests.cs
  • Loading branch information
gfoidl authored Feb 20, 2019
1 parent 55e34d7 commit 49ba5dc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 95 deletions.
6 changes: 0 additions & 6 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,3 @@ export PATH="$PATH:/root/.dotnet/tools"
### Prequisites

[.NET Core 2.1 SDK](https://aka.ms/DotNetCore21)


## Contributors

* [gfoidl](https://github.com/gfoidl)
* [ryepup](https://github.com/ryepup)
5 changes: 1 addition & 4 deletions samples/XUnitSample/SimpleTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Xunit;

namespace XUnitSample
Expand Down
8 changes: 4 additions & 4 deletions source/trx2junit/Internal/JUnitBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Build()
//---------------------------------------------------------------------
private void AddTestSuite(string testSuiteName, IEnumerable<KeyValuePair<Guid, TestDefinition>> tests)
{
ResetCounters();
this.ResetCounters();

var xTestSuite = new XElement("testsuite");

Expand Down Expand Up @@ -94,10 +94,10 @@ private void AddTest(XElement xTestSuite, KeyValuePair<Guid, TestDefinition> tes
//---------------------------------------------------------------------
private void ResetCounters()
{
_errors = 0;
_errors = 0;
_testCount = 0;
_failures = 0;
_time = TimeSpan.Zero;
_failures = 0;
_time = TimeSpan.Zero;
_timeStamp = null;
}
}
Expand Down
179 changes: 98 additions & 81 deletions tests/trx2junit.Tests/Internal/JUnitBuilderTests/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,139 @@
using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
using System.Collections.Generic;

namespace trx2junit.Tests.Internal.JUnitBuilderTests
{
[TestFixture]
public class Build
{
private Models.Test _testData = new Models.Test();

private readonly Models.Test _testData = new Models.Test();
//---------------------------------------------------------------------
public Build()
{
AddTestResult("Class1", "Method1", Outcome.NotExecuted, new TimeSpan(0, 0, 1));
AddTestResult("Class1", "Method2", Outcome.Failed, new TimeSpan(0, 0, 3));
AddTestResult("Class1", "Method3", Outcome.Failed, new TimeSpan(0, 0, 2));
AddTestResult("Class2", "Method1", Outcome.Passed, new TimeSpan(0, 0, 3));
AddTestResult("Class3", "Method1", Outcome.Failed, new TimeSpan(0, 0, 8));
AddTestResult("Class3", "Method2", Outcome.Passed, new TimeSpan(0, 0, 12));
AddTestResult("Class4", "Method1", Outcome.Completed, new TimeSpan(0, 0, 1));
AddTestResult("Class4", "Method2", Outcome.Warning, new TimeSpan(0, 0, 1));
}
AddTestResult("Class1", "Method1", Outcome.NotExecuted, new TimeSpan(0, 0, 1));
AddTestResult("Class1", "Method2", Outcome.Failed , new TimeSpan(0, 0, 3));
AddTestResult("Class1", "Method3", Outcome.Failed , new TimeSpan(0, 0, 2));
AddTestResult("Class2", "Method1", Outcome.Passed , new TimeSpan(0, 0, 3));
AddTestResult("Class3", "Method1", Outcome.Failed , new TimeSpan(0, 0, 8));
AddTestResult("Class3", "Method2", Outcome.Passed , new TimeSpan(0, 0, 12));
AddTestResult("Class4", "Method1", Outcome.Completed , new TimeSpan(0, 0, 1));
AddTestResult("Class4", "Method2", Outcome.Warning , new TimeSpan(0, 0, 1));
//-----------------------------------------------------------------
void AddTestResult(string testClass, string testMethod, Outcome testResult, TimeSpan? testDuration)
{
var testGuid = Guid.NewGuid();
var testExecGuid = Guid.NewGuid();

_testData.TestDefinitions.Add(testGuid,
new TestDefinition
{
Id = testGuid,
TestClass = testClass,
TestMethod = testMethod,
ExecutionId = testExecGuid,
});

_testData.UnitTestResults.Add(testExecGuid,
new UnitTestResult
{
ExecutionId = testExecGuid,
TestId = testGuid,
Outcome = testResult,
Duration = testDuration,
StartTime = DateTime.Now,
StackTrace = "",
Message = "",
});
}
}
//---------------------------------------------------------------------
[Test]
public void Build_Builds___OK()
public void Builds___OK()
{
RunJUnitBuilder(_testData);
}
var sut = new JUnitBuilder(_testData);

sut.Build();
}
//---------------------------------------------------------------------
[Test]
public void Build_Correct_Test_Suites()
public void Correct_Test_Suites()
{
var result = RunJUnitBuilder(_testData);
var testsuiteList = result.Elements("testsuite").ToList();
List<XElement> testsuiteList = this.GetTestSuites();

Assert.AreEqual("Class1", testsuiteList[0].Attribute("name").Value);
Assert.AreEqual("Class2", testsuiteList[1].Attribute("name").Value);
Assert.AreEqual("Class3", testsuiteList[2].Attribute("name").Value);
Assert.AreEqual("Class4", testsuiteList[3].Attribute("name").Value);
Assert.Multiple(() =>
{
Assert.AreEqual("Class1", testsuiteList[0].Attribute("name").Value);
Assert.AreEqual("Class2", testsuiteList[1].Attribute("name").Value);
Assert.AreEqual("Class3", testsuiteList[2].Attribute("name").Value);
Assert.AreEqual("Class4", testsuiteList[3].Attribute("name").Value);
});
}

//---------------------------------------------------------------------
[Test]
public void Build_Correct_Test_Suite_Test_Counts()
public void Correct_Test_Suite_Test_Counts()
{
var result = RunJUnitBuilder(_testData);
var testsuiteList = result.Elements("testsuite").ToList();
List<XElement> testsuiteList = this.GetTestSuites();

Assert.AreEqual(3, int.Parse(testsuiteList[0].Attribute("tests").Value));
Assert.AreEqual(1, int.Parse(testsuiteList[1].Attribute("tests").Value));
Assert.AreEqual(2, int.Parse(testsuiteList[2].Attribute("tests").Value));
Assert.AreEqual(2, int.Parse(testsuiteList[3].Attribute("tests").Value));
Assert.Multiple(() =>
{
Assert.AreEqual(3, int.Parse(testsuiteList[0].Attribute("tests").Value));
Assert.AreEqual(1, int.Parse(testsuiteList[1].Attribute("tests").Value));
Assert.AreEqual(2, int.Parse(testsuiteList[2].Attribute("tests").Value));
Assert.AreEqual(2, int.Parse(testsuiteList[3].Attribute("tests").Value));
});
}

//---------------------------------------------------------------------
[Test]
public void Build_Correct_Test_Suite_Failure_Counts()
public void Correct_Test_Suite_Failure_Counts()
{
var result = RunJUnitBuilder(_testData);
var testsuiteList = result.Elements("testsuite").ToList();
List<XElement> testsuiteList = this.GetTestSuites();

Assert.AreEqual(2, int.Parse(testsuiteList[0].Attribute("failures").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[1].Attribute("failures").Value));
Assert.AreEqual(1, int.Parse(testsuiteList[2].Attribute("failures").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[3].Attribute("failures").Value));
Assert.Multiple(() =>
{
Assert.AreEqual(2, int.Parse(testsuiteList[0].Attribute("failures").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[1].Attribute("failures").Value));
Assert.AreEqual(1, int.Parse(testsuiteList[2].Attribute("failures").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[3].Attribute("failures").Value));
});
}

//---------------------------------------------------------------------
[Test]
public void Build_Correct_Test_Suite_Error_Counts()
public void Correct_Test_Suite_Error_Counts()
{
var result = RunJUnitBuilder(_testData);
var testsuiteList = result.Elements("testsuite").ToList();
List<XElement> testsuiteList = this.GetTestSuites();

Assert.AreEqual(0, int.Parse(testsuiteList[0].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[1].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[2].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[3].Attribute("errors").Value));
Assert.Multiple(() =>
{
Assert.AreEqual(0, int.Parse(testsuiteList[0].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[1].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[2].Attribute("errors").Value));
Assert.AreEqual(0, int.Parse(testsuiteList[3].Attribute("errors").Value));
});
}

//---------------------------------------------------------------------
[Test]
public void Build_Correct_Test_Suite_Times()
public void Correct_Test_Suite_Times()
{
var result = RunJUnitBuilder(_testData);
var testsuiteList = result.Elements("testsuite").ToList();
List<XElement> testsuiteList = this.GetTestSuites();

Assert.AreEqual(6.0, decimal.Parse(testsuiteList[0].Attribute("time").Value));
Assert.AreEqual(3.0, decimal.Parse(testsuiteList[1].Attribute("time").Value));
Assert.AreEqual(20.0, decimal.Parse(testsuiteList[2].Attribute("time").Value));
Assert.AreEqual(2.0, decimal.Parse(testsuiteList[3].Attribute("time").Value));
Assert.Multiple(() =>
{
Assert.AreEqual(6.0 , decimal.Parse(testsuiteList[0].Attribute("time").Value));
Assert.AreEqual(3.0 , decimal.Parse(testsuiteList[1].Attribute("time").Value));
Assert.AreEqual(20.0, decimal.Parse(testsuiteList[2].Attribute("time").Value));
Assert.AreEqual(2.0 , decimal.Parse(testsuiteList[3].Attribute("time").Value));
});
}

private XElement RunJUnitBuilder(Models.Test test)
//---------------------------------------------------------------------
private List<XElement> GetTestSuites()
{
var builder = new JUnitBuilder(test);
var builder = new JUnitBuilder(_testData);

builder.Build();
return builder.Result;
}

private void AddTestResult(string testClass, string testMethod, Outcome testResult, TimeSpan? testDuration)
{
var testGuid = Guid.NewGuid();
var testExecGuid = Guid.NewGuid();
_testData.TestDefinitions.Add(testGuid,
new TestDefinition
{
Id = testGuid,
TestClass = testClass,
TestMethod = testMethod,
ExecutionId = testExecGuid,
});
_testData.UnitTestResults.Add(testExecGuid,
new UnitTestResult
{
ExecutionId = testExecGuid,
TestId = testGuid,
Outcome = testResult,
Duration = testDuration,
StartTime = DateTime.Now,
StackTrace = "",
Message = "",
});
return builder.Result.Elements("testsuite").ToList();
}
}
}

0 comments on commit 49ba5dc

Please sign in to comment.