-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-79582 Failed test report for automatic test retries
Merge-request: TC-MR-8638 Merged-by: Vladislav Ma-iu-shan <Vladislav.Ma-iu-shan@jetbrains.com>
- Loading branch information
Vlad Ma-iu-shan
authored and
Space Team
committed
Jan 29, 2024
1 parent
49ac43a
commit 5118e7c
Showing
18 changed files
with
204 additions
and
21 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
TeamCity.VSTest.TestLogger.Tests/FailedTestsReportWriterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace TeamCity.VSTest.TestLogger.Tests; | ||
|
||
using System; | ||
using System.Text; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
using Moq; | ||
using Xunit; | ||
|
||
public class FailedTestsReportWriterTests | ||
{ | ||
private readonly Mock<IOptions> _optionsMock = new(); | ||
private readonly Mock<IBytesWriterFactory> _bytesWriterFactoryMock = new(); | ||
private readonly Mock<IBytesWriter> _bytesWriterMock = new(); | ||
|
||
public FailedTestsReportWriterTests() | ||
{ | ||
_bytesWriterFactoryMock | ||
.Setup(x => x.Create(It.IsAny<string>())) | ||
.Returns(_bytesWriterMock.Object); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotReportWhenDisabled() | ||
{ | ||
// Arrange | ||
_optionsMock | ||
.SetupGet(x => x.FailedTestsReportSavePath) | ||
.Returns(string.Empty); | ||
var writer = new FailedTestsReportWriter(_optionsMock.Object, _bytesWriterFactoryMock.Object); | ||
|
||
// Act | ||
writer.ReportFailedTest(CreateTestCase("Test")); | ||
|
||
// Assert | ||
_bytesWriterFactoryMock.Verify(x => x.Create(It.IsAny<string>()), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotReportSameParameterizedTestTwice() | ||
{ | ||
// Arrange | ||
_optionsMock | ||
.SetupGet(x => x.FailedTestsReportSavePath) | ||
.Returns("path-to-report"); | ||
var writer = new FailedTestsReportWriter(_optionsMock.Object, _bytesWriterFactoryMock.Object); | ||
|
||
// Act | ||
writer.ReportFailedTest(CreateTestCase("SameTest(1)")); | ||
writer.ReportFailedTest(CreateTestCase("SameTest(2)")); | ||
|
||
// Assert | ||
var expected = Encoding.UTF8.GetBytes("SameTest" + Environment.NewLine); | ||
_bytesWriterMock.Verify(x => x.Write(expected), Times.Once); | ||
_bytesWriterMock.Verify(x => x.Flush(), Times.Once); | ||
_bytesWriterMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
private static TestCase CreateTestCase(string testName) => new(testName, new Uri("executor://NUnit3TestExecutor"), "Tests"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TeamCity.VSTest.TestLogger; | ||
|
||
internal class BytesWriterFactory : IBytesWriterFactory | ||
{ | ||
public IBytesWriter Create(string fileName) => new BytesWriter(fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
namespace TeamCity.VSTest.TestLogger; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
||
internal class FailedTestsReportWriter : IFailedTestsReportWriter | ||
{ | ||
private readonly IOptions _options; | ||
private readonly IBytesWriterFactory _bytesWriterFactory; | ||
private readonly IBytesWriter? _reportWriter; | ||
private readonly HashSet<string> _reportedTests; | ||
|
||
public FailedTestsReportWriter(IOptions options, IBytesWriterFactory bytesWriterFactory) | ||
{ | ||
_options = options; | ||
_bytesWriterFactory = bytesWriterFactory; | ||
_reportWriter = InitFileMessageWriter(); | ||
_reportedTests = new HashSet<string>(); | ||
} | ||
|
||
|
||
private bool EnsureFailedTestsFileSavePathDirectoryExists() | ||
{ | ||
if (string.IsNullOrEmpty(_options.FailedTestsReportSavePath)) | ||
return false; | ||
|
||
try | ||
{ | ||
if (!Directory.Exists(_options.FailedTestsReportSavePath)) | ||
Directory.CreateDirectory(_options.FailedTestsReportSavePath); | ||
|
||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public void ReportFailedTest(TestCase testCase) | ||
{ | ||
if (_reportWriter == null) | ||
return; | ||
|
||
if (string.IsNullOrWhiteSpace(testCase.FullyQualifiedName)) | ||
return; | ||
|
||
var testName = GetTestNameForRetry(testCase.FullyQualifiedName); | ||
if (!_reportedTests.Add(testName)) | ||
return; | ||
|
||
var bytesToWrite = Encoding.UTF8.GetBytes(testName + Environment.NewLine); | ||
_reportWriter.Write(bytesToWrite); | ||
_reportWriter.Flush(); | ||
} | ||
|
||
/// <summary> | ||
/// For MSTest and XUnit FullyQualifiedName is supported as is | ||
/// In case of NUnit we have to remove arguments from FullyQualifiedName | ||
/// </summary> | ||
private string GetTestNameForRetry(string fullyQualifiedName) | ||
{ | ||
var name = fullyQualifiedName.Trim(); | ||
var argsPosition = name.IndexOf("(", StringComparison.Ordinal); | ||
var hasArgs = argsPosition >= 0; | ||
return hasArgs ? name.Substring(0, argsPosition) : name; | ||
} | ||
|
||
private IBytesWriter? InitFileMessageWriter() | ||
{ | ||
if (!EnsureFailedTestsFileSavePathDirectoryExists()) | ||
return null; | ||
|
||
var messagesFilePath = Path.Combine(_options.FailedTestsReportSavePath, | ||
Guid.NewGuid().ToString("n")) + ".txt"; | ||
return _bytesWriterFactory.Create(messagesFilePath); | ||
} | ||
|
||
public void Dispose() => _reportWriter?.Dispose(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TeamCity.VSTest.TestLogger; | ||
|
||
internal interface IBytesWriterFactory | ||
{ | ||
IBytesWriter Create(string fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace TeamCity.VSTest.TestLogger; | ||
|
||
using System; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
||
internal interface IFailedTestsReportWriter : IDisposable | ||
{ | ||
void ReportFailedTest(TestCase testName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
namespace TeamCity.VSTest.TestLogger; | ||
|
||
internal interface IMessageWriter | ||
using System; | ||
|
||
internal interface IMessageWriter : IDisposable | ||
{ | ||
void Write(string message); | ||
|
||
void Flush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters