Skip to content

Commit

Permalink
Migrate to xunit v3
Browse files Browse the repository at this point in the history
Migrate to xunit v3.
  • Loading branch information
martincostello committed Dec 20, 2024
1 parent a487408 commit 00efec0
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 163 deletions.
1 change: 0 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,5 @@
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<Using Include="Shouldly" />
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
</Project>
7 changes: 3 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
Expand All @@ -22,9 +22,8 @@
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="ReportGenerator" Version="5.3.11" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
Expand Down
5 changes: 2 additions & 3 deletions samples/TodoApp.Tests/TodoApp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="GitHubActionsTestLogger" NoWarn="RT0003" />
<PackageReference Include="MartinCostello.Logging.XUnit" />
<PackageReference Include="MartinCostello.Logging.XUnit.v3" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Xunit.SkippableFact" />
<PackageReference Include="xunit.v3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TodoApp\TodoApp.csproj" />
Expand Down
21 changes: 10 additions & 11 deletions samples/TodoApp.Tests/TodoRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public class TodoRepositoryTests(ITestOutputHelper outputHelper)
{
private ILoggerFactory LoggerFactory { get; } = outputHelper.ToLoggerFactory();

[SkippableFact]
[Fact]
public async Task Can_Create_Update_And_Delete_Todo_Items()
{
// Arrange
Skip.IfNot(
OperatingSystem.IsWindows(),
"This test can only be run on Windows.");
Assert.SkipUnless(OperatingSystem.IsWindows(), "This test can only be run on Windows.");

var cancellationToken = TestContext.Current.CancellationToken;
var now = new DateTimeOffset(2018, 08, 12, 10, 41, 0, TimeSpan.Zero);
var clock = new FakeTimeProvider(now);

Expand All @@ -38,12 +37,12 @@ public async Task Can_Create_Update_And_Delete_Todo_Items()
.UseSqlServer(instance.ConnectionString);

using var context = new TodoContext(builder.Options);
await context.Database.EnsureCreatedAsync();
await context.Database.EnsureCreatedAsync(cancellationToken);

var target = new TodoRepository(clock, context);

// Act - Verify the repository is empty
IList<TodoItem> items = await target.GetItemsAsync();
IList<TodoItem> items = await target.GetItemsAsync(cancellationToken);

// Assert
Assert.NotNull(items);
Expand All @@ -53,7 +52,7 @@ public async Task Can_Create_Update_And_Delete_Todo_Items()
string text = "Buy cheese";

// Act
TodoItem item = await target.AddItemAsync(text);
TodoItem item = await target.AddItemAsync(text, cancellationToken);

// Assert
Assert.NotNull(item);
Expand All @@ -66,13 +65,13 @@ public async Task Can_Create_Update_And_Delete_Todo_Items()
Guid id = item.Id;

// Act
bool? completeResult = await target.CompleteItemAsync(id);
bool? completeResult = await target.CompleteItemAsync(id, cancellationToken);

// Assert
Assert.True(completeResult);

// Act - Verify the repository has one item that is completed
items = await target.GetItemsAsync();
items = await target.GetItemsAsync(cancellationToken);

// Assert
Assert.NotNull(items);
Expand All @@ -86,13 +85,13 @@ public async Task Can_Create_Update_And_Delete_Todo_Items()
Assert.Equal(now, item.CompletedAt);

// Act - Delete the item
bool deleteResult = await target.DeleteItemAsync(id);
bool deleteResult = await target.DeleteItemAsync(id, cancellationToken);

// Assert
Assert.True(deleteResult);

// Act - Verify the repository is empty again
items = await target.GetItemsAsync();
items = await target.GetItemsAsync(cancellationToken);

// Assert
Assert.NotNull(items);
Expand Down
2 changes: 0 additions & 2 deletions src/SqlLocalDb/SqlLocalDbErrors.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2012-2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;

namespace MartinCostello.SqlLocalDb;

/// <summary>
Expand Down
31 changes: 0 additions & 31 deletions tests/SqlLocalDb.Tests/DelayedMessageBus.cs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/SqlLocalDb.Tests/ISqlLocalDbApiExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class ISqlLocalDbApiExtensionsTests(ITestOutputHelper outputHelper)
{
private readonly ILoggerFactory _loggerFactory = outputHelper.ToLoggerFactory();

[SkippableTheory]
[Theory]
[InlineData(unchecked((int)0x89c50112))]
[InlineData(unchecked((int)0x89c50108))]
public static void TemporaryInstance_Ignores_Exception_If_Delete_Fails(int errorCode)
{
// Arrange
Skip.IfNot(SqlLocalDbApi.IsWindows);
Assert.SkipWhen(SqlLocalDbApi.IsWindows, "Not expected to fail on Windows.");

// Arrange
var api = Substitute.For<ISqlLocalDbApi>();
Expand Down
5 changes: 2 additions & 3 deletions tests/SqlLocalDb.Tests/MartinCostello.SqlLocalDb.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" NoWarn="RT0003" />
<PackageReference Include="MartinCostello.Logging.XUnit" />
<PackageReference Include="MartinCostello.Logging.XUnit.v3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Xunit.SkippableFact" />
<PackageReference Include="xunit.v3" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion tests/SqlLocalDb.Tests/NotWindowsFactAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public sealed class NotWindowsFactAttribute : FactAttribute
public NotWindowsFactAttribute()
: base()
{
Skip = !OperatingSystem.IsWindows() ? string.Empty : "This test can only be run on Windows.";
Skip = !OperatingSystem.IsWindows() ? null : "This test cannot be run on Windows.";
}
}
26 changes: 0 additions & 26 deletions tests/SqlLocalDb.Tests/RetryFactDiscoverer.cs

This file was deleted.

71 changes: 0 additions & 71 deletions tests/SqlLocalDb.Tests/RetryTestCase.cs

This file was deleted.

4 changes: 1 addition & 3 deletions tests/SqlLocalDb.Tests/RunAsAdminFactAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Security.Principal;
using Xunit.Sdk;

namespace MartinCostello.SqlLocalDb;

Expand All @@ -11,13 +10,12 @@ namespace MartinCostello.SqlLocalDb;
/// if the user account running the test has administrative privileges. This class cannot be inherited.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("MartinCostello.SqlLocalDb.RetryFactDiscoverer", "MartinCostello.SqlLocalDb.Tests")]
public sealed class RunAsAdminFactAttribute : FactAttribute
{
public RunAsAdminFactAttribute()
: base()
{
Skip = IsCurrentUserAdmin(out string name) ? string.Empty : $"The current user '{name}' does not have administrative privileges.";
Skip = IsCurrentUserAdmin(out string name) ? null : $"The current user '{name}' does not have administrative privileges.";
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/SqlLocalDb.Tests/WindowsCIOnlyFactAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public WindowsCIOnlyFactAttribute()
OperatingSystem.IsWindows() &&
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI"));

Skip = isWindowsCI ? string.Empty : "This test can only be run on Windows CI.";
Skip = isWindowsCI ? null : "This test can only be run on Windows CI.";
}
}
5 changes: 1 addition & 4 deletions tests/SqlLocalDb.Tests/WindowsOnlyFactAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
// Copyright (c) Martin Costello, 2012-2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Xunit.Sdk;

namespace MartinCostello.SqlLocalDb;

/// <summary>
/// Attribute that is applied to a method to indicate that it is a fact that should be run by the
/// test runner if the current operating system is Windows. This class cannot be inherited.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[XunitTestCaseDiscoverer("MartinCostello.SqlLocalDb.RetryFactDiscoverer", "MartinCostello.SqlLocalDb.Tests")]
public sealed class WindowsOnlyFactAttribute : FactAttribute
{
public WindowsOnlyFactAttribute()
: base()
{
Skip = OperatingSystem.IsWindows() ? string.Empty : "This test can only be run on Windows.";
Skip = OperatingSystem.IsWindows() ? null : "This test can only be run on Windows.";
}

/// <summary>
Expand Down

0 comments on commit 00efec0

Please sign in to comment.