6

I just created a netstandard library in Visual Studio 2017 and added references to xunit and xunit.runner.visualstudio, but the VS Test Explorer and Resharper 2017 EAP 3 are not recognizing any tests. I've seen: Unit testing a .NET Standard 1.6 library but project.json is gone and csproj is back in place.

What do I have to do, to be able to run the unit tests included in a netstandard library?

Library.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>
</Project>

Test.cs

namespace ClassLibrary2
{
    public class Class1
    {
        [Fact]
        public void RescharperShouldRunTest()
        {
            Assert.True(true);
        }
    }
}

enter image description here

Edit

Thanks to the answers I made some progress.

Adding

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />  
<!-- ... and ...  --> 
<ItemGroup>
    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

did have no impact. Only if I change the TargetFramework to netcoreapp1.1 VS discovered the test and could run them. With netstandard1.6 the Test Explorer remains empty. But I don't want a netcore app. I want a .NET standard library.

Athari
  • 33,702
  • 16
  • 105
  • 146
Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103
  • Rarely people include unit test cases in their class libraries. If you put test cases in a separate class library in the past, then changing it to a console app for .NET Core is also of almost no effort at all. I don't understand why you emphasize on "I don't want a netcore app". That's not an attitude for discussions. – Lex Li Mar 10 '17 at 04:42
  • .NET core is not .netstandard. See: https://msdnshared.blob.core.windows.net/media/2016/09/dotnet-tomorrow.png netcore is not compatible with Xamarin. – Sven-Michael Stübe Mar 10 '17 at 08:16

4 Answers4

1

If you run dotnet new xunit you will see an additional reference included.

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

I have found the same outcome in the current IDE tooling. You can however run the tests on the commandline using

dotnet test -l "trx"

I'm trying to target netstandard1.5;net452 but only the net452 tests are run, not the netstandard1.5 tests.

Rory Primrose
  • 633
  • 4
  • 10
1

Unfortunately you are correct in that class libraries (Or .net standard libraries) are not made to run unit tests. Infact many tutorials you find on the web (Such as this one : http://dotnetcoretutorials.com/2017/01/30/running-unit-tests-dotnet-test/) will talk you into creating a CONSOLE application and then deleting what you don't need.

I think the issue likely is why does your unit tests need to be in a .net standard library? If you are distributing a library that has associated unit tests, the tests themselves don't need to be in .net standard as nothing will be referencing them. And within your own solution, test assemblies that are to be run shouldn't be referenced from elsewhere.

MindingData
  • 11,924
  • 6
  • 49
  • 68
  • Though this should be a comment, I fully agree on your points. Who posted this question should strictly follow Microsoft's design, not against it. – Lex Li Mar 10 '17 at 04:40
  • 4
    Because they were in a PCL 259, before. That enables me, to run the unit tests on different platforms like Windows, Android and iOS. And .Net standard is the PCL successor. I don't agree that this is an anti pattern. What is the correct library type for my scenario in your opinion? – Sven-Michael Stübe Mar 10 '17 at 08:13
1

I know this is an older question, but I have had the same problem - writing xunit tests that run in the Visual Studio test runner and Xamarin.iOS/Droid . For netstandard what I ended up doing was creating a Unit test project that was .NET Core 2.0, creating the Xamarin.* test applications (and configuring them for unit), and creating a Shared project instead of netstandard library. This worked for me.

My test project structure is:

  • Project.Tests.Droid (Droid UI project)
  • Project.Tests.iOS (iOS UI project)
  • Project.Tests.Unit (VS Unit test project)
  • Project.Tests (shared project)

All my tests are in Project.Tests.

valdetero
  • 4,624
  • 1
  • 31
  • 46
0

It is tricky, but doable, if you refer to the other answer as well as the sample project below,

https://github.com/lextm/sharpsnmplib/blob/master/Tests/Tests.NetStandard.csproj

Of course, some NuGet packages used in the sample have already been updated to stable.

The following are critical, <PackageReference Include="Microsoft.NET.Test.Sdk"> <Version>15.0.0-preview-20170125-04</Version> </PackageReference> <PackageReference Include="xunit"> <Version>2.2.0-beta5-build3474</Version> </PackageReference> <PackageReference Include="xunit.runner.visualstudio"> <Version>2.2.0-beta5-build1225</Version> </PackageReference> The project that contains unit test cases must be a console app targeting .NET Core.

Lex Li
  • 60,503
  • 9
  • 116
  • 147