Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running xunit tests included in a .NET Standard 1.6 library

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.

like image 641
Sven-Michael Stübe Avatar asked Mar 08 '17 23:03

Sven-Michael Stübe


People also ask

Is xUnit compatible with .NET framework?

It's an open source unit testing tool for . Net framework that's compatible with ReSharper, CodeRush, TestDriven.Net, and Xamarin. You can take advantage of xUnit.Net to assert an exception type easily.

What dotnet command executes xUnit tests?

The dotnet test command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure of each test.

How to create xUnit Test Project in Visual Studio Code?

let’s right-click on New Solution in our created project. Then Search xUnit and then choose the xUnit test project .NET core option and then click on the next button. Now, set the Project Name and Location, and then click on the Next button. Now, set the Target Framework as .NET 6.0, and then click on the Create button.

What is the use of xUnit runner?

Doesn’t do anything really useful, it is mostly metadata. xunit.runner.visualstudio – this is the xUnit test runner, that connects the xUnit with the .NET Core test runnign platform. Thanks to this, you can discover and run tests in visual studio or by using dotnet test.

Is it possible to use xUnit console x86?

BTW you can’t use xunit.console.x86.exe for .NET Core projects, that runner is for .NET Framework only.

What is the difference between DotNet test and DotNet xUnit?

dotnet is also quite extensible, e.g. xunit is adding its own runner ( dotnet xunit) that has some nicer features that standard dotnet test. ‘dotnet test’ vs. ‘dotnet xunit’ Let’s look at xunit .csproj: TargetFramework: netcoreapp2.0 – Platform the test cases will be run against.


2 Answers

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.

like image 178
Rory Primrose Avatar answered Oct 25 '22 22:10

Rory Primrose


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.

like image 39
MindingData Avatar answered Oct 25 '22 23:10

MindingData