Skip to content

Commit

Permalink
Updated to xUnit v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Dec 20, 2024
1 parent 405edbd commit d928e9a
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 3,012 deletions.
2 changes: 0 additions & 2 deletions visual-studio/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<ImplicitUsings>enable</ImplicitUsings>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<RootNamespace>GitWebLinks</RootNamespace>
<RestoreLockedMode>true</RestoreLockedMode>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

</Project>
19 changes: 9 additions & 10 deletions visual-studio/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Community.VisualStudio.Toolkit.17" Version="17.0.527"/>
<PackageVersion Include="Community.VisualStudio.VSCT" Version="16.0.29.6"/>
<PackageVersion Include="coverlet.collector" Version="6.0.2"/>
<PackageVersion Include="Fluid.Core" Version="2.12.0"/>
<PackageVersion Include="Community.VisualStudio.Toolkit.17" Version="17.0.527" />
<PackageVersion Include="Community.VisualStudio.VSCT" Version="16.0.29.6" />
<PackageVersion Include="Fluid.Core" Version="2.12.0" />
<PackageVersion Include="MessagePack" Version="2.5.192" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.VisualStudio.SDK" Version="17.0.31902.203"/>
<PackageVersion Include="Microsoft.VisualStudio.SDK.Analyzers" Version="17.7.47"/>
<PackageVersion Include="Microsoft.VSSDK.BuildTools" Version="17.12.2069"/>
<PackageVersion Include="Microsoft.VisualStudio.SDK" Version="17.0.31902.203" />
<PackageVersion Include="Microsoft.VisualStudio.SDK.Analyzers" Version="17.7.47" />
<PackageVersion Include="Microsoft.VSSDK.BuildTools" Version="17.12.2069" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.msbuild" Version="2.9.2"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
<PackageVersion Include="xunit.v3.runner.msbuild" Version="1.0.0" />
</ItemGroup>
</Project>
1,328 changes: 0 additions & 1,328 deletions visual-studio/source/GitWebLinks/packages.lock.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,18 +12,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.msbuild">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.v3.runner.msbuild">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Xunit.Sdk;
using Xunit.v3;

namespace GitWebLinks;

[XunitTestCaseDiscoverer("GitWebLinks.HandlerCustomTestCaseDiscoverer", "GitWebLinks.UnitTests")]
public sealed class HandlerCustomFactAttribute : FactAttribute {
}
[XunitTestCaseDiscoverer(typeof(HandlerCustomTestCaseDiscoverer))]
public sealed class HandlerCustomFactAttribute : FactAttribute { }
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
using System.ComponentModel;
using System.Text.RegularExpressions;
using Xunit.Abstractions;
using Xunit.Sdk;
using Xunit.v3;

namespace GitWebLinks;

public class HandlerCustomTestCase : HandlerTestCase {

private string _customTestName;


public HandlerCustomTestCase(
IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
TestMethodDisplayOptions defaultMethodDisplayOptions,
ITestMethod testMethod,
string handlerName,
string customTestName
) : base(diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod, handlerName, [customTestName]) {
_customTestName = customTestName;
}


[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Used for deserialization only.")]
public HandlerCustomTestCase() {
_customTestName = "";
CustomTestName = "";
}


protected override string GetDisplayName(IAttributeInfo factAttribute, string displayName) {
string[] nameParts;


// The display name of the underlying test case will be the
// full name of the method (namespace + class + method name).
// Add the handler name before the method name, and replace
// the method name with the name of the custom test.
nameParts = BaseDisplayName.Split('.');
nameParts[nameParts.Length - 1] = $"[{HandlerName}] {_customTestName}";

return string.Join(".", nameParts);
public HandlerCustomTestCase(
string handlerName,
string customTestName,
IXunitTestMethod testMethod,
string testCaseDisplayName,
string uniqueID,
bool @explicit,
string? skipReason = null,
Type? skipType = null,
string? skipUnless = null,
string? skipWhen = null,
Dictionary<string, HashSet<string>>? traits = null,
string? sourceFilePath = null,
int? sourceLineNumber = null,
int? timeout = null
) : base(
handlerName,
testMethod,
testCaseDisplayName,
uniqueID,
@explicit,
skipReason,
skipType,
skipUnless,
skipWhen,
traits,
[customTestName],
sourceFilePath,
sourceLineNumber,
timeout
) {
CustomTestName = customTestName;
}


protected override string GetUniqueID() {
return $"{base.GetUniqueID()}+{Regex.Replace(_customTestName, "\\s\\.", "_")}";
}
public string CustomTestName { get; set; }


public override void Deserialize(IXunitSerializationInfo data) {
_customTestName = data.GetValue<string>("CustomTestName");
protected override void Deserialize(IXunitSerializationInfo data) {
base.Deserialize(data);
CustomTestName = data.GetValue<string>(nameof(CustomTestName))!;
}


public override void Serialize(IXunitSerializationInfo data) {
data.AddValue("CustomTestName", _customTestName);
protected override void Serialize(IXunitSerializationInfo data) {
base.Serialize(data);
data.AddValue(nameof(CustomTestName), CustomTestName);
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
using Xunit.Abstractions;
using System.Text.RegularExpressions;
using Xunit.Internal;
using Xunit.Sdk;
using Xunit.v3;

namespace GitWebLinks;

public class HandlerCustomTestCaseDiscoverer : IXunitTestCaseDiscoverer {

private readonly IMessageSink _diagnosticMessageSink;


public HandlerCustomTestCaseDiscoverer(IMessageSink diagnosticMessageSink) {
_diagnosticMessageSink = diagnosticMessageSink;
}


public IEnumerable<IXunitTestCase> Discover(
public ValueTask<IReadOnlyCollection<IXunitTestCase>> Discover(
ITestFrameworkDiscoveryOptions discoveryOptions,
ITestMethod testMethod,
IAttributeInfo factAttribute
IXunitTestMethod testMethod,
IFactAttribute factAttribute
) {
foreach (HandlerTestDefinition definition in TestDefinitionProvider.GetDefinitions()) {
foreach (CustomTest test in definition.Tests.CreateUrl.Misc) {
yield return new HandlerCustomTestCase(
_diagnosticMessageSink,
discoveryOptions.MethodDisplayOrDefault(),
discoveryOptions.MethodDisplayOptionsOrDefault(),
testMethod,
#pragma warning disable IDE0008 // Use explicit type
var details = TestIntrospectionHelper.GetTestCaseDetails(
discoveryOptions,
testMethod,
factAttribute
);
#pragma warning restore IDE0008 // Use explicit type

return new ValueTask<IReadOnlyCollection<IXunitTestCase>>(
(
from definition in TestDefinitionProvider.GetDefinitions()
from test in definition.Tests.CreateUrl.Misc
select new HandlerCustomTestCase(
definition.Name,
test.Name
);
}
}
test.Name,
details.ResolvedTestMethod,
$"[{definition.Name}] {test.Name}",
$"{details.UniqueID}+{Regex.Replace($"{definition.Name}+{test.Name}", "\\s\\.", "_")}",
details.Explicit,
details.SkipReason,
details.SkipType,
details.SkipUnless,
details.SkipWhen,
testMethod.Traits.ToReadWrite(StringComparer.OrdinalIgnoreCase),
timeout: details.Timeout
)
).ToList()
);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Xunit.Sdk;
using Xunit.v3;

namespace GitWebLinks;

[XunitTestCaseDiscoverer("GitWebLinks.HandlerTestCaseDiscoverer", "GitWebLinks.UnitTests")]
public sealed class HandlerFactAttribute : FactAttribute {
}
[XunitTestCaseDiscoverer(typeof(HandlerTestCaseDiscoverer))]
public sealed class HandlerFactAttribute : FactAttribute { }

This file was deleted.

Loading

0 comments on commit d928e9a

Please sign in to comment.