-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2d5be2
commit 3d3b241
Showing
5 changed files
with
170 additions
and
48 deletions.
There are no files selected for viewing
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
85 changes: 85 additions & 0 deletions
85
test/test.xunit.runner.visualstudio/Sinks/VsDiscoverySinkTests.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,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
using Xunit.Runner.VisualStudio; | ||
|
||
public class VsDiscoverySinkTests | ||
{ | ||
public class CreateVsTestCase | ||
{ | ||
readonly SpyLoggerHelper logger = SpyLoggerHelper.Create(); | ||
readonly TestPlatformContext testPlatformContext = new TestPlatformContext { DesignMode = false }; | ||
|
||
[Fact] | ||
public void MustSetTestClassName() | ||
{ | ||
var testCase = TestData.TestCaseDiscovered(testClassName: null); | ||
|
||
var vsTestCase = VsDiscoverySink.CreateVsTestCase("source", testCase, logger, testPlatformContext); | ||
|
||
Assert.Null(vsTestCase); | ||
var message = Assert.Single(logger.Messages); | ||
Assert.Equal("[Error] [xUnit.net 00:00:00.00] source: Error creating Visual Studio test case for test-case-display-name: TestClassName is null", message); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false, null)] | ||
[InlineData(true, "serialization")] | ||
public void StandardData( | ||
bool designMode, | ||
string? expectedSerialization) | ||
{ | ||
var testCase = TestData.TestCaseDiscovered( | ||
sourceFilePath: "/source/file.cs", | ||
sourceLineNumber: 42, | ||
traits: new Dictionary<string, IReadOnlyCollection<string>> | ||
{ | ||
{ "foo", ["baz", "bar"] }, | ||
{ "biff", ["42"] }, | ||
} | ||
); | ||
var testPlatformContext = new TestPlatformContext { DesignMode = designMode }; | ||
|
||
var vsTestCase = VsDiscoverySink.CreateVsTestCase("source", testCase, logger, testPlatformContext); | ||
|
||
Assert.NotNull(vsTestCase); | ||
|
||
// Standard VSTest properties | ||
Assert.Equal("/source/file.cs", vsTestCase.CodeFilePath); | ||
Assert.Equal("test-case-display-name", vsTestCase.DisplayName); | ||
Assert.Equal(Constants.ExecutorUri, vsTestCase.ExecutorUri.OriginalString); | ||
Assert.Equal("test-class-name.test-method", vsTestCase.FullyQualifiedName); | ||
Assert.NotEqual(Guid.Empty, vsTestCase.Id); // Computed at runtime, just need to ensure it's set | ||
Assert.Equal(42, vsTestCase.LineNumber); | ||
Assert.Equal("source", vsTestCase.Source); | ||
Assert.Collection( | ||
vsTestCase.Traits.Select(t => $"'{t.Name}' = '{t.Value}'").OrderBy(x => x), | ||
trait => Assert.Equal("'biff' = '42'", trait), | ||
trait => Assert.Equal("'foo' = 'bar'", trait), | ||
trait => Assert.Equal("'foo' = 'baz'", trait) | ||
); | ||
|
||
// xUnit.net extension properties | ||
Assert.Equal(expectedSerialization, vsTestCase.GetPropertyValue(VsTestRunner.TestCaseSerializationProperty)); | ||
Assert.Equal("test-case-id", vsTestCase.GetPropertyValue(VsTestRunner.TestCaseUniqueIDProperty)); | ||
Assert.Equal(false, vsTestCase.GetPropertyValue(VsTestRunner.TestCaseExplicitProperty)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "test-method")] | ||
[InlineData(new[] { "Type1", "Type2" }, "test-method(Type1,Type2)")] | ||
public void SetsManagedTypeAndMethodProperties( | ||
string[]? parameterTypes, | ||
string expectedManagedMethodName) | ||
{ | ||
var testCase = TestData.TestCaseDiscovered(testMethodParameterTypes: parameterTypes); | ||
|
||
var vsTestCase = VsDiscoverySink.CreateVsTestCase("source", testCase, logger, testPlatformContext); | ||
|
||
Assert.NotNull(vsTestCase); | ||
Assert.Equal("test-class-name", vsTestCase.GetPropertyValue(VsTestRunner.ManagedTypeProperty)); | ||
Assert.Equal(expectedManagedMethodName, vsTestCase.GetPropertyValue(VsTestRunner.ManagedMethodProperty)); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/test.xunit.runner.visualstudio/Utility/SpyLoggerHelper.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,14 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
||
namespace Xunit.Runner.VisualStudio; | ||
|
||
public class SpyLoggerHelper(SpyMessageLogger logger, Stopwatch stopwatch) : | ||
LoggerHelper(logger, stopwatch) | ||
{ | ||
public IReadOnlyCollection<string> Messages => logger.Messages; | ||
|
||
public static SpyLoggerHelper Create() => | ||
new(new(), new()); | ||
} |
13 changes: 13 additions & 0 deletions
13
test/test.xunit.runner.visualstudio/Utility/SpyMessageLogger.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,13 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
||
public class SpyMessageLogger : IMessageLogger | ||
{ | ||
public readonly List<string> Messages = []; | ||
|
||
public void SendMessage( | ||
TestMessageLevel testMessageLevel, | ||
string message) => | ||
Messages.Add($"[{testMessageLevel}] {message}"); | ||
} |
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,53 @@ | ||
using System.Collections.Generic; | ||
using Xunit.Runner.Common; | ||
using Xunit.Sdk; | ||
|
||
internal static class TestData | ||
{ | ||
static readonly IReadOnlyDictionary<string, IReadOnlyCollection<string>> EmptyTraits = new Dictionary<string, IReadOnlyCollection<string>>(); | ||
|
||
public static ITestCaseDiscovered TestCaseDiscovered( | ||
string assemblyUniqueID = "assembly-id", | ||
bool @explicit = false, | ||
string serialization = "serialization", | ||
string? skipReason = null, | ||
string? sourceFilePath = null, | ||
int? sourceLineNumber = null, | ||
string testCaseDisplayName = "test-case-display-name", | ||
string testCaseUniqueID = "test-case-id", | ||
int? testClassMetadataToken = null, | ||
string? testClassName = "test-class-name", | ||
string? testClassNamespace = null, | ||
string? testClassSimpleName = "test-class-simple-name", | ||
string? testClassUniqueID = "test-class-id", | ||
string testCollectionUniqueID = "test-collection-id", | ||
int? testMethodMetadataToken = null, | ||
string? testMethodName = "test-method", | ||
string[]? testMethodParameterTypes = null, | ||
string? testMethodReturnType = null, | ||
string? testMethodUniqueID = "test-method-id", | ||
IReadOnlyDictionary<string, IReadOnlyCollection<string>>? traits = null) => | ||
new TestCaseDiscovered | ||
{ | ||
AssemblyUniqueID = assemblyUniqueID, | ||
Explicit = @explicit, | ||
Serialization = serialization, | ||
SkipReason = skipReason, | ||
SourceFilePath = sourceFilePath, | ||
SourceLineNumber = sourceLineNumber, | ||
TestCaseDisplayName = testCaseDisplayName, | ||
TestCaseUniqueID = testCaseUniqueID, | ||
TestClassMetadataToken = testClassMetadataToken, | ||
TestClassName = testClassName, | ||
TestClassNamespace = testClassNamespace, | ||
TestClassSimpleName = testClassSimpleName, | ||
TestClassUniqueID = testClassUniqueID, | ||
TestCollectionUniqueID = testCollectionUniqueID, | ||
TestMethodMetadataToken = testMethodMetadataToken, | ||
TestMethodName = testMethodName, | ||
TestMethodParameterTypesVSTest = testMethodParameterTypes, | ||
TestMethodReturnTypeVSTest = testMethodReturnType, | ||
TestMethodUniqueID = testMethodUniqueID, | ||
Traits = traits ?? EmptyTraits, | ||
}; | ||
} |