Skip to content

Commit

Permalink
TRX logger Started Time fix (#253)
Browse files Browse the repository at this point in the history
* Fix for Started time for the run, in case the start time for the first test case is default(DateTime).

* Making changes as per discussion with codito.
We want to include the start up cost in the duration and remove the dependency on the StartTime stamped by the adapter.
  • Loading branch information
singhsarab authored and codito committed Dec 2, 2016
1 parent 80e841b commit a678579
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 17 additions & 4 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ internal class TrxLogger : ITestLogger

private int totalTests, passTests, failTests;

private DateTime testRunStartTime;

#endregion

/// <summary>
Expand Down Expand Up @@ -128,6 +130,11 @@ internal string GetRunLevelInformationalMessage()
return this.runLevelErrorsAndWarnings;
}

internal DateTime TestRunStartTime
{
get { return this.testRunStartTime; }
}

internal TestRun LoggerTestRun
{
get { return this.testRun; }
Expand Down Expand Up @@ -223,7 +230,12 @@ internal void TestResultHandler(object sender, ObjectModel.Logging.TestResultEve
Guid runId = Guid.NewGuid();

this.testRun = new TestRun(runId);
this.testRun.Started = e.Result.StartTime.UtcDateTime;

// We cannot rely on the StartTime for the first test result
// In case of parallel, first test result is the fastest test and not the one which started first.
// Setting Started to DateTime.Now in Intialize will make sure we include the startup cost, which was being ignored earlier.
// This is in parity with the way we set this.testRun.Finished
this.testRun.Started = this.testRunStartTime;

// Save default test settings
string runDeploymentRoot = FileHelper.ReplaceInvalidFileNameChars(this.testRun.Name);
Expand Down Expand Up @@ -295,7 +307,7 @@ internal void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
helper.SaveSingleFields(rootElement, this.testRun, parameters);

// Save test settings
helper.SaveObject(this.testRun.RunConfiguration, rootElement, "TestSettings", parameters);
helper.SaveObject(this.testRun.RunConfiguration, rootElement, "TestSettings", parameters);

// Save test results
helper.SaveIEnumerable(this.results, rootElement, "Results", ".", null, parameters);
Expand Down Expand Up @@ -340,8 +352,8 @@ internal void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
this.failTests,
this.testRunOutcome,
this.runLevelErrorsAndWarnings,
this.runLevelStdOut.ToString(),
resultFiles,
this.runLevelStdOut.ToString(),
resultFiles,
collectorEntries);

helper.SaveObject(runSummary, rootElement, "ResultSummary", parameters);
Expand Down Expand Up @@ -401,6 +413,7 @@ private void InitializeInternal()
this.passTests = 0;
this.failTests = 0;
this.runLevelStdOut = new StringBuilder();
this.testRunStartTime = DateTime.Now;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,17 @@ public void TestMessageHandlerShouldAddMessageInListIfItIsError()
}

[TestMethod]
public void TestResultHandlerShouldCaptureStartTimeInSummaryAsTestMethodStartTime()
public void TestResultHandlerShouldCaptureStartTimeInSummaryWithTimeStampDuringIntialize()
{
DateTime actualTime = new DateTime(2008, 6, 19, 7, 0, 0);
actualTime = DateTime.SpecifyKind(actualTime, DateTimeKind.Utc);

ObjectModel.TestCase testCase = new ObjectModel.TestCase("dummy string", new Uri("some://uri"), "DummySourceFileName");
ObjectModel.TestResult testResult = new ObjectModel.TestResult(testCase);

testResult.StartTime = (DateTimeOffset)actualTime;

Mock<TestResultEventArgs> e = new Mock<TestResultEventArgs>(testResult);

trxLogger.TestResultHandler(new object(), e.Object);

Assert.AreEqual((DateTimeOffset)actualTime, this.trxLogger.LoggerTestRun.Started);
Assert.AreEqual(this.trxLogger.TestRunStartTime, this.trxLogger.LoggerTestRun.Started);
}

[TestMethod]
public void TestResultHandlerKeepingTheTrackOfPassedAndFailedTests()
{
Expand Down

0 comments on commit a678579

Please sign in to comment.