The VS SDK Test Framework is a library for your unit tests that exercise VS code to use
so that certain core VS functionality works outside the VS process so your unit tests can function.
For example, ThreadHelper
and obtaining global services from the static ServiceProvider
tend to fail in unit tests without this library installed.
Microsoft Internal users: See specific guidance if consuming within the VS
repo.
-
Install the NuGet package Microsoft.VisualStudio.Sdk.TestFramework, or for Xunit test projects, install the more specific Microsoft.VisualStudio.Sdk.TestFramework.Xunit package
-
Make sure your unit test project generates the required binding redirects by adding these two properties to your project file:
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
-
Apply some changes to your test project source as appropriate given the test framework you're already using:
This library will create a mocked up UI thread, such that ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync()
can switch to it. Your unit tests do not start on this mocked up UI thread. If your product code contains checks
that it is invoked on the UI thread (e.g. ThreadHelper.ThrowIfNotOnUIThread()
) your test method should look like this:
[TestMethod] // or [Fact]
public async Task VerifyWeDoSomethingGood()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
MyVSPackage.DoSomethingAwesome();
}
There are a collection of "base services" that the VSSDKTestFx comes with mocks for.
Calling GlobalServiceProvider.AddService
for any of these will result in an InvalidOperationException
being thrown claiming
that the service is already added.
These services include:
SVsActivityLog
Customize behavior by acquiring the service and downcasting to theMockVsActivityLog
type, then setting theForwardTo
property.OLE.Interop.IServiceProvider
SVsTaskSchedulerService
SVsUIThreadInvokerPrivate
More may be added and can be found in source code.