简体   繁体   English

运行包含在.NET Standard 1.6库中的xunit测试

[英]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. 我只是在Visual Studio 2017年创建了netstandard库并添加引用xunitxunit.runner.visualstudio ,但VS测试浏览器和ReSharper的2017年3 EAP不承认任何测试。 I've seen: Unit testing a .NET Standard 1.6 library but project.json is gone and csproj is back in place. 我见过: 单元测试.NET Standard 1.6库但是project.json已经不见了,csproj又回来了。

What do I have to do, to be able to run the unit tests included in a netstandard library? 我需要做什么才能运行netstandard库中包含的单元测试?

Library.csproj 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 test.cs中

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

在此输入图像描述

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. 只有当我将TargetFramework更改为netcoreapp1.1 VS netcoreapp1.1发现测试并运行它们。 With netstandard1.6 the Test Explorer remains empty. 使用netstandard1.6 ,测试资源管理器仍为空。 But I don't want a netcore app. 但我不想要一个netcore应用程序。 I want a .NET standard library. 我想要一个.NET标准库。

If you run dotnet new xunit you will see an additional reference included. 如果您运行dotnet new xunit,您将看到包含的其他参考。

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

I have found the same outcome in the current IDE tooling. 我在当前的IDE工具中找到了相同的结果。 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. 我试图针对netstandard1.5; net452,但只运行net452测试,而不是netstandard1.5测试。

Unfortunately you are correct in that class libraries (Or .net standard libraries) are not made to run unit tests. 不幸的是,你是正确的,因为类库(或.net标准库)不能运行单元测试。 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. 事实上你在网上找到的很多教程(比如这篇教程: http//dotnetcoretutorials.com/2017/01/30/running-unit-tests-dotnet-test/ )会告诉你创建一个CONSOLE应用程序然后删除你不需要什么。

I think the issue likely is why does your unit tests need to be in a .net standard library? 我认为问题可能是为什么你的单元测试需要在.net标准库中? 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. 如果您正在分发具有关联单元测试的库,则测试本身不需要符合.net标准,因为没有任何内容可以引用它们。 And within your own solution, test assemblies that are to be run shouldn't be referenced from elsewhere. 在您自己的解决方案中,不应从其他地方引用要运行的测试程序集。

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 . 我知道这是一个较老的问题,但我遇到了同样的问题 - 编写在Visual Studio测试运行器和Xamarin.iOS / Droid中运行的xunit测试。 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. 对于netstandard,我最终要做的是创建一个.NET Core 2.0的单元测试项目,创建Xamarin。*测试应用程序(并为单元配置它们),并创建一个共享项目而不是netstandard库。 This worked for me. 这对我有用。

My test project structure is: 我的测试项目结构是:

  • Project.Tests.Droid (Droid UI project) Project.Tests.Droid(Droid UI项目)
  • Project.Tests.iOS (iOS UI project) Project.Tests.iOS(iOS UI项目)
  • Project.Tests.Unit (VS Unit test project) Project.Tests.Unit(VS单元测试项目)
  • Project.Tests (shared project) Project.Tests(共享项目)

All my tests are in Project.Tests. 我的所有测试都在Project.Tests中。

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 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. 当然,样本中使用的一些NuGet包已经更新为稳定版。

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. 以下是至关重要的, <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>包含单位的项目测试用例必须是针对.NET Core的控制台应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM