Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support xUnit v3 #199

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
<PackageVersion Include="Microsoft.ServiceHub.Framework.Testing" Version="4.4.22" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="System.ComponentModel.Composition" Version="8.0.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<GlobalPackageReference Include="CSharpIsNullAnalyzer" Version="0.1.593" />
Expand Down
30 changes: 29 additions & 1 deletion Microsoft.VisualStudio.Sdk.TestFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.1.32107.320
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.VisualStudio.Sdk.TestFramework", "src\Microsoft.VisualStudio.Sdk.TestFramework\Microsoft.VisualStudio.Sdk.TestFramework.csproj", "{A62E3D92-0B5C-4D53-9269-EAC253BBCDEE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.VisualStudio.Sdk.TestFramework.Tests", "test\Microsoft.VisualStudio.Sdk.TestFramework.Tests\Microsoft.VisualStudio.Sdk.TestFramework.Tests.csproj", "{94569D82-0C5E-4437-AADF-67738037ED9D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests", "test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.csproj", "{94569D82-0C5E-4437-AADF-67738037ED9D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{824473EA-BB16-47EE-96CA-CD854BF62276}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -36,6 +36,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{3D7D871C-3
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit", "src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.csproj", "{A7FDF327-106E-4627-8D44-AFE7FDF56633}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared", "src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.shproj", "{774B64A2-B1AB-4A09-8307-B87EA0D6D1D2}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared", "test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared.shproj", "{E5D7C833-9946-4415-98E2-3FFC92C10B47}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3", "src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.csproj", "{A384DBA5-D380-41AA-B89A-57D05BD33EA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.Tests", "test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.Tests\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.Tests.csproj", "{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -54,6 +62,14 @@ Global
{A7FDF327-106E-4627-8D44-AFE7FDF56633}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7FDF327-106E-4627-8D44-AFE7FDF56633}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7FDF327-106E-4627-8D44-AFE7FDF56633}.Release|Any CPU.Build.0 = Release|Any CPU
{A384DBA5-D380-41AA-B89A-57D05BD33EA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A384DBA5-D380-41AA-B89A-57D05BD33EA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A384DBA5-D380-41AA-B89A-57D05BD33EA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A384DBA5-D380-41AA-B89A-57D05BD33EA7}.Release|Any CPU.Build.0 = Release|Any CPU
{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,8 +78,20 @@ Global
{A62E3D92-0B5C-4D53-9269-EAC253BBCDEE} = {7750CC1F-2803-4A54-BBF7-5FD05FD49B96}
{94569D82-0C5E-4437-AADF-67738037ED9D} = {3D7D871C-3834-4575-B158-D38FEC07BC6B}
{A7FDF327-106E-4627-8D44-AFE7FDF56633} = {7750CC1F-2803-4A54-BBF7-5FD05FD49B96}
{774B64A2-B1AB-4A09-8307-B87EA0D6D1D2} = {7750CC1F-2803-4A54-BBF7-5FD05FD49B96}
{E5D7C833-9946-4415-98E2-3FFC92C10B47} = {3D7D871C-3834-4575-B158-D38FEC07BC6B}
{A384DBA5-D380-41AA-B89A-57D05BD33EA7} = {7750CC1F-2803-4A54-BBF7-5FD05FD49B96}
{6C1A5941-E2DA-4A98-B80A-8C2F32DD4828} = {3D7D871C-3834-4575-B158-D38FEC07BC6B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27C43278-C8F5-4183-A1E3-712A83362BB5}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared.projitems*{6c1a5941-e2da-4a98-b80a-8c2f32dd4828}*SharedItemsImports = 5
src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems*{774b64a2-b1ab-4a09-8307-b87ea0d6d1d2}*SharedItemsImports = 13
test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared.projitems*{94569d82-0c5e-4437-aadf-67738037ed9d}*SharedItemsImports = 5
src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems*{a384dba5-d380-41aa-b89a-57d05bd33ea7}*SharedItemsImports = 5
src\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems*{a7fdf327-106e-4627-8d44-afe7fdf56633}*SharedItemsImports = 5
test\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Tests.Shared.projitems*{e5d7c833-9946-4415-98e2-3ffc92c10b47}*SharedItemsImports = 13
EndGlobalSection
EndGlobal
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ tend to fail in unit tests without this library installed.

[![NuGet package](https://img.shields.io/nuget/v/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.svg)](https://nuget.org/packages/Microsoft.VisualStudio.Sdk.TestFramework.Xunit)

This package contains functionality applicable when using Xunit as your test framework.
This package contains functionality applicable when using Xunit v2 as your test framework.

[Learn more about this package](src/Microsoft.VisualStudio.Sdk.TestFramework.Xunit/README.md).

## Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3

[![NuGet package](https://img.shields.io/nuget/v/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.svg)](https://nuget.org/packages/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3)

This package contains functionality applicable when using Xunit v3 as your test framework.

[Learn more about this package](src/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3/README.md).

## Contributing

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
Expand Down
4 changes: 3 additions & 1 deletion doc/xunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
These instructions apply when consuming this test framework from an Xunit project.
This is an addendum to general instructions outlined in [our README](../README.md).

1. Install the NuGet package `Microsoft.VisualStudio.Sdk.TestFramework.Xunit` [![NuGet package](https://img.shields.io/nuget/v/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.svg)](https://nuget.org/packages/Microsoft.VisualStudio.Sdk.TestFramework.Xunit)
1. Install the NuGet package.
* When using xUnit v2, install `Microsoft.VisualStudio.Sdk.TestFramework.Xunit` [![NuGet package](https://img.shields.io/nuget/v/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.svg)](https://nuget.org/packages/Microsoft.VisualStudio.Sdk.TestFramework.Xunit)
* When using xUnit v3, install `Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3` [![NuGet package](https://img.shields.io/nuget/v/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3.svg)](https://nuget.org/packages/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3)
1. For *each* of your test classes that rely on VS mocked services, apply the `Collection` attribute and add a parameter and statement to your constructor:

```csharp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
Expand Down Expand Up @@ -79,7 +79,11 @@ public BrokeredServiceContractTestBase(ITestOutputHelper logger, ServiceRpcDescr
protected SourceLevels DescriptorLoggingVerbosity { get; set; } = SourceLevels.Verbose;

/// <inheritdoc/>
#if XUNIT_V3
public virtual async ValueTask InitializeAsync()
#else
public virtual async Task InitializeAsync()
#endif
{
int testId = Interlocked.Increment(ref testCounter);
Func<string, SourceLevels, TraceSource> traceSourceFactory = (name, verbosity) =>
Expand Down Expand Up @@ -162,7 +166,11 @@ public virtual async Task InitializeAsync()
}

/// <inheritdoc/>
#if XUNIT_V3
public virtual async ValueTask DisposeAsync()
#else
public virtual async Task DisposeAsync()
#endif
{
(this.ClientProxy as IDisposable)?.Dispose();

Expand All @@ -185,10 +193,23 @@ public virtual async Task DisposeAsync()
/// <summary>
/// Verifies that all methods on the service interface include a <see cref="CancellationToken"/> as the last parameter.
/// </summary>
#if XUNIT_V3
[Fact]
#else
[SkippableFact]
#endif
public void AllMethodsIncludeCancellationToken()
{
Skip.IfNot(this.DefaultTestsEnabled, $"{nameof(this.DefaultTestsEnabled)} is set to false.");
#if XUNIT_V3
Assert.SkipUnless(
#else
Skip.IfNot(
#endif
#pragma warning disable SA1114 // Parameter list should follow declaration
this.DefaultTestsEnabled,
#pragma warning restore SA1114 // Parameter list should follow declaration
$"{nameof(this.DefaultTestsEnabled)} is set to false.");

AssertAllMethodsIncludeCancellationToken<TInterface>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>774b64a2-b1ab-4a09-8307-b87ea0d6d1d2</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)BrokeredServiceContractTestBase`2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BrokeredServiceContractTestBase`4.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LoggingTestBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MefHostingFixture.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\MockVsActivityLogXunitAdapter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XunitTraceListener.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>774b64a2-b1ab-4a09-8307-b87ea0d6d1d2</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Microsoft.VisualStudio.Sdk.TestFramework</RootNamespace>
<Description>A test library to help in writing unit and integration tests for Visual Studio extensions.</Description>
<Product>Microsoft VisualStudio SDK Test Framework for Xunit v3</Product>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTestProject>false</IsTestProject>
<DefineConstants>$(DefineConstants);XUNIT_V3</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="contentFiles\**\*.cs" />
<Content Include="contentFiles\**\*.cs" BuildAction="compile" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="xunit.v3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.VisualStudio.Sdk.TestFramework\Microsoft.VisualStudio.Sdk.TestFramework.csproj" />
</ItemGroup>
<Import Project="..\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems" Label="Shared" />
</Project>
18 changes: 18 additions & 0 deletions src/Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Microsoft.VisualStudio.Sdk.TestFramework.Xunit.V3

This package contains functionality applicable when using Xunit v3 as your test framework.

For *each* of your test classes that rely on VS mocked services, apply the `Collection` attribute and add a parameter and statement to your constructor:

```csharp
using Microsoft.VisualStudio.Sdk.TestFramework;

[Collection(MockedVS.Collection)]
public class YourTestClass
{
public TestFrameworkTests(GlobalServiceProvider sp)
{
sp.Reset();
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <auto-generated />
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.Sdk.TestFramework
{
using Xunit;

/// <summary>
/// Defines the "MockedVS" xunit test collection.
/// </summary>
[CollectionDefinition(Collection)]
public class MockedVS :
#if NETFRAMEWORK || WINDOWS
ICollectionFixture<GlobalServiceProvider>,
#endif
ICollectionFixture<MefHostingFixture>
{
/// <summary>
/// The name of the xunit test collection.
/// </summary>
public const string Collection = "MockedVS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Microsoft.VisualStudio.Sdk.TestFramework</RootNamespace>
<Description>A test library to help in writing unit and integration tests for Visual Studio extensions.</Description>
<Product>Microsoft VisualStudio SDK Test Framework for Xunit</Product>
<Product>Microsoft VisualStudio SDK Test Framework for Xunit v2</Product>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTestProject>false</IsTestProject>
Expand All @@ -21,4 +21,5 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.VisualStudio.Sdk.TestFramework\Microsoft.VisualStudio.Sdk.TestFramework.csproj" />
</ItemGroup>
<Import Project="..\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared\Microsoft.VisualStudio.Sdk.TestFramework.Xunit.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Microsoft.VisualStudio.Sdk.TestFramework.Xunit

This package contains functionality applicable when using Xunit as your test framework.
This package contains functionality applicable when using Xunit v2 as your test framework.

For *each* of your test classes that rely on VS mocked services, apply the `Collection` attribute and add a parameter and statement to your constructor:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if NETFRAMEWORK || WINDOWS
Expand Down Expand Up @@ -178,6 +178,13 @@ internal void Reset()
{
this.mockBrokeredServiceContainer = new();
this.services = this.baseServices.SetItem(typeof(SVsBrokeredServiceContainer).GUID, this.mockBrokeredServiceContainer);

// The mock activity log might be forwarding to a service that is only available
// within a test. Reset the log so that it does not forward to anything.
if (this.baseServices.GetValueOrDefault(typeof(SVsActivityLog).GUID) is MockVsActivityLog log)
{
log.ForwardTo = null;
}
}

/// <summary>
Expand Down
Loading