1
votes

I have the following solution in Visual Studio 2017 Update 3 preview, which consists of a Xamarin.Forms project as NetStandard1.4 and a NetStandard1.4 dotnet Core Services.API project and a NetStandard1.6 dotnet Core unit test project.

enter image description here

The Unit Test project references the services project only. The csproj file looks like this, with the MSTest framework added for unit testing. The issue though is that the tests are never discovered.

csproj

<PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
    <RootNamespace>FloatSink.Services</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>True</DebugSymbols>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></PackageReference>
    <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="15.0.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.1.17" />
    <PackageReference Include="MSTest.TestFramework" Version="1.1.17" />
</ItemGroup>

<ItemGroup>
    <ProjectReference Include="..\Services.API\Services.API.csproj" />
</ItemGroup>

Unit Test class

[TestClass]
public class AppBuilderTests
{
    private const string DebugBuild = "Debug build";
    private const string ReleaseBuild = "Release build";

    /// <summary>
    /// Test that the ToString() method returns the string we sent into the constructor
    /// </summary>
    [TestMethod]
    public void ToStringReturnsStringFromCtor()
    {
        // Act
        var build = new AppBuild(DebugBuild);

        // Assert
        Assert.AreEqual(DebugBuild, build.ToString());
    }
}

Finally this is the test output when I build and select Run All from the Test Explorer (which is empty).

[5/26/2017 6:38:23 PM Informational] ------ Load Playlist started ------

[5/26/2017 6:38:23 PM Informational] ========== Load Playlist finished (0:00:00.004501) ==========

[5/26/2017 6:38:24 PM Informational] ------ Discover test started ------

[5/26/2017 6:38:25 PM Informational] ========== Discover test finished: 0 found (0:00:00.5716028) ==========

Why won't the MSTest unit tests show up in the Test Explorer and allow me to discover/run them?

Update

I changed the debug type from Full to portable and I've also tried this with xUnit and have the same issue. So this isn't specific to MSTest.

csproj with xUnit

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

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <AssemblyName>FloatSink.Services.Api.Tests</AssemblyName>
    <RootNamespace>FloatSink.Services</RootNamespace>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>portable</DebugType>
    <DebugSymbols>True</DebugSymbols>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Services.API\Services.API.csproj" />
  </ItemGroup>

</Project>
1

1 Answers

2
votes

The problem is that your TargetFramework is set to a .NET Standard version.

Although test projects look like libraries, they have more characteristics of a runnable output - the 15.0.0 test sdk even changes the output type to Exe to trigger generation of assets needed to run the tests. (In 2.0 / 15.3.0 this will change to a new HasRuntimeOutput property).

The fix here is to change the TargetFramework to either some version of net* or netcoreapp* depending on what framework you want to run the tests on.