xunit.net

Is there an easy way in xunit.net to compare two collections without regarding the items' order?

丶灬走出姿态 提交于 2019-12-03 23:29:58
问题 In one of my tests, I want to ensure that a collection has certain items. Therefore, I want to compare this collection with the items of an expected collection not regarding the order of the items . Currently, my test code looks somewhat like this: [Fact] public void SomeTest() { // Do something in Arrange and Act phase to obtain a collection List<int> actual = ... // Now the important stuff in the Assert phase var expected = new List<int> { 42, 87, 30 }; Assert.Equal(expected.Count, actual

What's the idiomatic way to verify collection size in xUnit?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 14:15:50
问题 I have in my test suite a test that goes something like this: [Fact] public void VerifySomeStuff() { var stuffCollection = GetSomeStuff(); Assert.Equal(1, stuffCollection.Count()); } This test works as I expect, but when I run it xUnit prints a warning: warning xUnit2013: Do not use Assert.Equal() to check for collection size. However, no alternative is suggested in the warning, and a google search takes me to the source code in xUnit for the test that verifies this warning is printed. If

MemberData tests show up as one test instead of many

▼魔方 西西 提交于 2019-12-03 10:38:33
问题 When you use [Theory] together with [InlineData] it will create a test for each item of inline data that is provided. However, if you use [MemberData] it will just show up as one test. Is there a way to make [MemberData] tests show up as multiple tests? 回答1: I spent a lot of time trying to figure this one out in my project. This related Github discussion from @NPadrutt himself helped a lot, but it was still confusing. The tl;dr is this: [MemberInfo] will report a single group test unless the

Is there a way to unit test an async method?

百般思念 提交于 2019-12-03 09:46:26
I am using Xunit and NMock on .NET platform. I am testing a presentation model where a method is asynchronous. The method creates an async task and executes it so the method returns immediately and the state I need to check aren't ready yet. I can set a flag upon finish without modifying the SUT but that would mean I would have to keep checking the flag in a while loop for example, with perhaps timeout. What are my options? Does your object feature any sort of signal that the asynchronous method is finished, such as an event? If that is the case, you can use the following approach: [Test]

Cannot find Assert.Fail and Assert.Pass or equivalent

只谈情不闲聊 提交于 2019-12-03 09:18:02
I used to use these in NUnit and they are really useful. Any idea how to do something like that? EDIT, CODE SAMPLE: bool condition = false;//would be nice not to have this observable.Subscribe(_ => { if (real test) condition= true;//Assert.Pass() }); StartObservable(); Assert.True(condition);//Assert.Fail() The documentation includes a comparison chart including this: Fail - xUnit.net alternative: Assert.True(false, "message") (It doesn't show Assert.Pass , and I've never used that myself, but I suspect the alternative is just to return from the test. Of course that doesn't help if you want to

Await Tasks in Test Setup Code in xUnit.net?

断了今生、忘了曾经 提交于 2019-12-03 05:39:52
The exact situation is I'm doing E2E tests with Protractor.NET (.NET port of AngularJS's Protractor E2E framework) and I would like to make some web requests (and the API -- System.Net.Http.HttpClient -- has all Async/ Task methods) to Arrange my test before I Act/Assert, only I need to do this same Arrange-ing for several tests. I'm using xUnit.net as my test runner they use an interface ( IUseFixture<T> ) for per-fixture setup code. It would be nice if there was a IAsyncUseFixture<T> that had a Task SetFixtureAsync(T t); or something. I don't think such a thing exists. Additionally I don't

How to run all tests in Visual Studio Code

柔情痞子 提交于 2019-12-03 05:25:49
问题 The latest version of VS Code already provides an easy way of running a single test as pointed on Tyler Long's answer to the question Debugging xunit tests in .NET Core and Visual Studio Code. However, I am looking how can I run all tests contained in a test suite class in VS Code (without debug)? The only way I found was adding to launch.json a specific configuration as the following one, but which I can only run in debug (I would like to run it without debug): { "name": ".NET Core Xunit

How to run setup code only once in an xUnit.net test

一世执手 提交于 2019-12-03 04:35:35
I'm trying to setup my tests using Xunit. I have a requirement to delete all images in a folder start of the tests, and then each method does some image resizing and saves a copy of it's output to the folder. The folder should only be emptied once, and then each method will save their own image into the folder. When I use IUseFixture<T> , the ClearVisualTestResultFolder function is still being called before every test, so I only end up with one image in the folder. public class Fixture { public void Setup() { ImageHelperTest.ClearVisualTestResultFolder(); } } public class ImageHelperTest :

MemberData tests show up as one test instead of many

大兔子大兔子 提交于 2019-12-03 04:22:47
When you use [Theory] together with [InlineData] it will create a test for each item of inline data that is provided. However, if you use [MemberData] it will just show up as one test. Is there a way to make [MemberData] tests show up as multiple tests? Nate Barbettini I spent a lot of time trying to figure this one out in my project. This related Github discussion from @NPadrutt himself helped a lot, but it was still confusing. The tl;dr is this: [MemberInfo] will report a single group test unless the provided objects for each test can be completely serialized and deserialized by implementing

What's the idiomatic way to verify collection size in xUnit?

隐身守侯 提交于 2019-12-03 04:08:11
I have in my test suite a test that goes something like this: [Fact] public void VerifySomeStuff() { var stuffCollection = GetSomeStuff(); Assert.Equal(1, stuffCollection.Count()); } This test works as I expect, but when I run it xUnit prints a warning: warning xUnit2013: Do not use Assert.Equal() to check for collection size. However, no alternative is suggested in the warning, and a google search takes me to the source code in xUnit for the test that verifies this warning is printed. If Assert.Equal() isn't the correct way to verify the lenght of a collection, what is? To clarify: I realize