xunit

.net core 2.0 ConfigureLogging xunit test

柔情痞子 提交于 2019-12-03 03:03:49
In my xUnit integration test in my .NET Core 2.0 project I cannot see log messages in the terminal that also prints the test results. When the code is run in WebHost environment, the logs are printed out to the console. This is how I configure logging in the test's constructor: var config = new ConfigurationBuilder() .AddJsonFile("appsettings.Tests.json") .Build(); var services = new ServiceCollection(); services.AddLogging(options => { options.AddConfiguration(config.GetSection("Logging")); options.AddConsole(); options.AddDebug(); }); var serviceProvider = services.BuildServiceProvider();

XUnit DI through overridden Startup file (.net core)

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have build a WebAPI and apart from my tests running on Postman I would like to implement some Integration/Unit tests. Now my business logic is very thin, most of the time its more of CRUD actions, therefore I wanted to start with testing my Controllers. I have a basic setup. Repository pattern (interfaces), Services (business logic) and Controllers. The flow goes Controller (DI Service) -> Service (DI Repo) -> Repo Action! So what I did was override my Startup file to change into a in memory database and the rest should be fine (I would

Xunit The following constructor parameters did not have matching fixture data

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I keep getting this error while using xunit for .NET 1.0 framework net46. The following constructor parameters did not have matching fixture data I have seen this post: Collection fixture won't inject and followed the instructions regarding collection fixture closely as described here: http://xunit.github.io/docs/shared-context.html#collection-fixture Nothing seems to work. Any suggestions to what might cause this? 回答1: In my case it turned out to be a matter of doing it right according to the instructions. By mistake I had annotated the

Unable to find testhost.dll. Please publish your test project and retry

匿名 (未验证) 提交于 2019-12-03 01:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a simple dotnet core class library with a single XUnit test method: TestLib.csproj: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.SDK" Version="15.9.0" /> <PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit.runner.console" Version="2.4.1"> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference>

Xunit multiple IClassFixtures

匿名 (未验证) 提交于 2019-12-03 01:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My question is How to setup multiple fixtures in one test class? But the constructor of Zoo class can not handle multiple fixtures. For exemple: public class Zoo : IClassFixture<Tiger>, IClassFixture<Wolf>, IClassFixure<Bird> { private IFixture fixture; public Zoo(IFixture fixture) { this.fixture = fixture; } [Fact] public void TestAnimal() { //Arrange int actualBonesCount = this.fixture.BonesCount; int expectedBonesCount = 2; //Act & Assert Assert.Equal(expectedBonesCount, actualBonesCount ); } } A tiger class public class Tiger :

xunit add info to output

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Does anyone know how to add additional info to the output console when running xUnit tests? I'm using testdriven.net, but I don't think that is where my answer lies. I am using a IUseFixture (actually IClassFixture from 2.0) to maintain data between tests. When a test fails I want to output some of that contextual data along with the failure and the stack trace that you usually get. Does anyone know of a hook I can use? 回答1: You can use ITestOutputHelper to write any output to the test result view. Just let xUnit inject it into your

How to run xUnit 2.1.0-beta-* for DNX projects with ReSharper

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How to run xUnit 2.1.0-beta-* for DNX projects with ReSharper? When the ReSharper do find the tests, it fails on running them with System.IO.FileNotFoundException that indicates the unit test assembly is not found. Due to my understanding, DNX projects do NOT generate assemblies in \bin folder when they are compiled with VisualStudio 2015. Is there a way to force DNX projects generate assemblies like the tranditional class libraries projects? Any ideas? 回答1: DNX tests aren't currently supported by ReSharper or the xunit plugin. It's a whole

使用 Moq 测试.NET Core - Why Moq?

匿名 (未验证) 提交于 2019-12-02 22:10:10
什么是Mock 当对代码进行测试的时候, 我们经常需要用到一些模拟(mock)技术. 绿色 的是需要被测试的类, 黄色 是它的依赖项, 灰色 的无关的类 在一个项目里, 我们经常需要把某一部分程序独立出来以便我们可以对这部分进行测试. 这就要求我们不要考虑项目其余部分的复杂性, 我们只想关注需要被测试的那部分. 这里就需要用到模拟(Mock)技术. 因为, 请仔细看. 我们想要隔离测试的这部分代码对外部有一个或者多个依赖. 所以编写测试代码的时候, 我们需要提供这些依赖. 而针对隔离测试, 并不应该使用生产时用的依赖项, 所以我们使用模拟版本的依赖项, 这些模拟版依赖项只能用于测试时, 它们会使隔离更加容易. 绿色 的是需要被测试的类, 黄色 是 Mock 的依赖项 Mock技术带来的优点 使用Mock技术, 可以有如下的优点: 提高测试运行速度 , 例如可以模拟DB, Web Service等比较慢的服务, 以及算法等. 支持并行开发 , 例如实际的依赖项还没有完成开发, 或者等待其他团队开发依赖项. 提高测试可靠性 , 例如有时这个依赖项的bug太多了, 经常由于依赖项的原因导致测试失败, 那么就应该使用mock版本来验证我们自己写的代码. 减少开发/测试成本 , 有时程序可能依赖一些云服务, 这些服务是按调用次数收费的, 那么就可以使用Mock版本来节省这方面的开资,

Using ReSharper, how to show debug output during a long-running unit test?

删除回忆录丶 提交于 2019-12-02 20:11:24
I'm using xUnit with the ReSharper test runner and the xUnitContrib resharper plugin. When I have a long-running test, I'd like to be able to output some progress indicator to the Unit Test Output window. I've tried Debug.WriteLines , Trace.WriteLine and Console.WriteLine . All of which have the same behavior - nothing shows in the output window until the test has completed. For example: [Fact] public void Test() { Debug.WriteLine("A"); Trace.WriteLine("B"); Console.WriteLine("C"); Thread.Sleep(10000); } The test shows no output until the 10 seconds have elapsed and the test completes. How do

How to attach message to rspec check?

99封情书 提交于 2019-12-02 19:01:51
In rspec: Can I attach a message to a check the same way as I would do in xUnit style test frameworks? How? assert_equal value1, value2, "something is wrong" Chris Johnsen should and should_not take a second argument ( message ) that overrides the matcher’s default message. 1.should be(2), 'one is not two!' The default messages are usually pretty useful though. update: for RSpec 3: expect(1).to eq(2), "one is not two!" Jörg W Mittag In RSpec, it's the matcher's job to print a sensible failure message. The generic matchers that ship with RSpec can obviously only print generic non-descript