Skip to content

Class with multiple tests using MockedVSCollection and GlobalServiceProvider don't run all on VS #22

@dominoFire

Description

@dominoFire

In a test project in VS, If I 'Run All' Tests from the class in VS, there are some random failing tests with the following message:

image

Message: 
    System.AggregateException : One or more errors occurred.
    ---- System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain.
    ---- The following constructor parameters did not have matching fixture data: GlobalServiceProvider gsp

  Stack Trace: 
    ----- Inner Stack Trace #1 (System.InvalidOperationException) -----
    Application.ctor()
    OleServiceProviderMock.MainThread()
    --- End of stack trace from previous location where exception was thrown ---
    TaskAwaiter.ThrowForNonSuccess(Task task)
    TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    OleServiceProviderMock.ctor()
    GlobalServiceProvider.ctor()
    ----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----

Code

Test class

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

namespace LibForVsSdk.Tests
{
    [Collection("LibForVsSdk.Tests.Test")]
    public class UnitTest1
    {
        public UnitTest1(GlobalServiceProvider gsp)
        {
        }

        [Fact]
        public void TestFalse()
        {
            Assert.False(false);
        }

        [Fact]
        public void TestTrue()
        {
            Assert.True(true);
        }

        [Theory]
        [InlineData(1)]
        [InlineData(2)]
        [InlineData(3)]
        public void Test1(int number)
        {
            Assert.True(number < 10);
        }
    }
}

Collection definition

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

namespace LibForVsSdk.Tests
{
    [CollectionDefinition("LibForVsSdk.Tests.Test")]
    public class CollectionDef : ICollectionFixture<GlobalServiceProvider>
    {
    }
}

Test Project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <Nullable>enable</Nullable>
	  <LangVersion>latest</LangVersion>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
	  <PackageReference Include="Microsoft.VisualStudio.Sdk.TestFramework" Version="17.0.15" />
	  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.1.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions