Questions tagged [xunit.net]
xUnit.net is a unit testing tool for the .NET Framework. Written by the original inventor of NUnit, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. It works with ReSharper, CodeRush, and TestDriven.NET.
xunit.net
1,018
questions
414
votes
7
answers
142k
views
NUnit vs. MbUnit vs. MSTest vs. xUnit.net [closed]
There are quite a lot of unittesting frameworks out there for .NET. I found this little feature comparison: http://xunit.github.io/docs/comparisons.html
Now I am to choose the best one for us. But how?...
241
votes
28
answers
104k
views
Unable to find testhost.dll. Please publish your test project and retry
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&...
217
votes
12
answers
157k
views
Execute unit tests serially (rather than in parallel)
I am attempting to unit test a WCF host management engine that I have written. The engine basically creates ServiceHost instances on the fly based on configuration. This allows us to dynamically ...
171
votes
7
answers
179k
views
xUnit.net: Global setup + teardown?
This question is about the unit testing framework xUnit.net.
I need to run some code before any test is executed, and also some code after all tests are done. I thought there should be some kind of ...
170
votes
8
answers
63k
views
What's the idiomatic way to verify collection size in xUnit?
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 ...
156
votes
13
answers
152k
views
Pass complex parameters to [Theory]
Xunit has a nice feature: you can create one test with a Theory attribute and put data in InlineData attributes, and xUnit will generate many tests, and test them all.
I want to have something like ...
142
votes
6
answers
70k
views
Test parameterization in xUnit.net similar to NUnit
Are there any means in xUnit.net framework similar to the following features of NUnit?
[Test, TestCaseSource("CurrencySamples")]
public void Format_Currency(decimal value, string expected){}
static ...
138
votes
8
answers
43k
views
Mocking IPrincipal in ASP.NET Core
I have an ASP.NET MVC Core application that I am writing unit tests for. One of the action methods uses User name for some functionality:
SettingsViewModel svm = _context.MySettings(User.Identity....
135
votes
11
answers
114k
views
Run code once before and after ALL tests in xUnit.net
TL;DR - I'm looking for xUnit's equivalent of MSTest's AssemblyInitialize (aka the ONE feature it has that I like).
Specifically I'm looking for it because I have some Selenium smoke tests which I ...
127
votes
2
answers
82k
views
Difference between Fact and Theory? - xUnit.net
I'm new to xUnit.net and AutoFixture.
I'm currently working on a "testproject" to get familiar with xUnit.net and Autofixture.
There is one little thing I don't understand.
What is the difference ...
99
votes
2
answers
39k
views
How do I run specific tests using dotnet test?
I have a large test suite in a .NET Core project. I can use the Test Explorer window to select a few tests to run.
I can also run the whole test suite on the command line with dotnet test. Is there a ...
93
votes
2
answers
56k
views
xunit Assert.ThrowsAsync() does not work properly?
So I have a test like the following:
[Fact]
public void Test1()
{
Assert.ThrowsAsync<ArgumentNullException>(() => MethodThatThrows());
}
private async Task MethodThatThrows()
{
await ...
84
votes
8
answers
52k
views
xUnit.net does not capture console output
I just started testing xUnit.net, but it doesn't seem to capture any output (Console, Debug, Trace), as I would have expected.
Is that possible? I am using a sample .NET 4.0 class-library with xUnit....
82
votes
4
answers
36k
views
Cannot find Assert.Fail and Assert.Pass or equivalent
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(_ =>
...
77
votes
14
answers
113k
views
Is Assert.Fail() considered bad practice?
I use Assert.Fail a lot when doing TDD. I'm usually working on one test at a time but when I get ideas for things I want to implement later I quickly write an empty test where the name of the test ...
75
votes
7
answers
93k
views
XUnit Assertion for checking equality of objects
I am using XUnit framework to test my C# code.
Is there any assert method available in this framework which does the object comparison? My intention is to check for equality of each of the object's ...
74
votes
2
answers
25k
views
Await Tasks in Test Setup Code in xUnit.net?
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 ...
74
votes
11
answers
36k
views
Unit Testing with functions that return random results
I don't think that this is specific to a language or framework, but I am using xUnit.net and C#.
I have a function that returns a random date in a certain range. I pass in a date, and the returning ...
70
votes
8
answers
52k
views
Pass array of string to xunit test method
I want to pass an array of string to one of my XUnit test method, but when I just do the following it doesn't work (array + params mechanism)
[Theory]
[InlineData(new object[] { "2000-01-02",...
69
votes
4
answers
101k
views
How to run all tests in Visual Studio Code
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,...
65
votes
10
answers
69k
views
Why is the xUnit Runner not finding my tests
I have a xUnit.net Test as follows:
static class MyTestClass
{
[Fact]
static void MyTestMethod()
{
}
}
The xUnit plugin for VS 2012 says:
No tests found to run.
TestDriven.net ...
64
votes
3
answers
44k
views
Should GetEnvironmentVariable work in xUnit tests?
If I set environment variables for a .Net Core web project in Visual Studio 2017 using the project properties page, I can read the value of the variable using Environment.GetEnvironmentVariable; ...
61
votes
10
answers
41k
views
Skipping a whole test class in xUnit.net
Is it possible to skip all tests from a specific class like in NUnit
[TestFixture]
[Ignore("Reason")]
public class TestClass {
}
55
votes
5
answers
44k
views
How can I stop the Visual Studio Test Runner when a test hangs
When a test hangs in a loop, the small green progress bar in the test runner does not proceed, but there is no way to stop the test run. Or is there?
VS 2013
Edit: This occured when using the XUnit ...
55
votes
5
answers
62k
views
How to set the test case sequence in xUnit
I have written the xUnit test cases in C#. That test class contains so many methods. I need to run the whole test cases in a sequence. How can I set the test case sequence in xUnit?
55
votes
5
answers
22k
views
MemberData tests show up as one test instead of many
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 ...
53
votes
5
answers
11k
views
Can you mark XUnit tests as Explicit?
I'm making the transition from NUnit to XUnit (in C#), and I was writing some "Integrated Tests" (ITs) that I don't necessarily want the test runner to run as part of my automated build process. I ...
50
votes
2
answers
10k
views
How can XUnit be configured to show just the method name in the Visual Studio 2015 Test Explorer?
When using xunit.runner.visualstudio version 2.0.1 in Visual Studio 2015, the names of the tests show up fully qualified. Is there a way for the tests to show only the method name?
Consider the ...
48
votes
4
answers
36k
views
Xunit 2.3.0 Unable to pass dates as inline params
In xUnit 2.2 and prior versions, we were able to pass date strings as inline data when implementing a Theory.
[Theory]
[InlineData("title 1", "testing 1", 1, "Educational", "2017-3-1", "2018-12-31")]...
44
votes
1
answer
21k
views
ExpectedException xunit .net core
I'm writing unit test for core application. Im trying to check, that my class throws exception. But ExpectedException attribute throws compile exception:
Error CS0246 The type or namespace name '...
43
votes
3
answers
15k
views
dotnet test > is there a way to show list of tests ran in console?
When running dotnet test, is there a way of showing a list of all tests ran in console instead of some output file?
Would be ideal to see a list like this in console:
x test1
🗸 test2
🗸 test3
x ...
43
votes
6
answers
27k
views
Is there an easy way in xunit.net to compare two collections without regarding the items' order?
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. ...
42
votes
5
answers
30k
views
How to refer to test files from Xunit tests in Visual Studio?
We’re using Xunit for testing. We’re running our tests via the built-in Visual Studio 2013 Test Runner, using the Xunit plugin.
The issue is that some of the tests need to refer to files on the ...
41
votes
7
answers
19k
views
xUnit doesn't write message to the output pane
In Visual Studio 2015 Community I have a sample ASP.NET 5 (vNext) project and a project with unit tests (xUnit.net). The version of DNX is 1.0.0-beta5. My goal is to add messages during the test run ...
38
votes
4
answers
21k
views
Having an actual decimal value as parameter for an attribute (example xUnit.net's [InlineData]
I'm trying to do unit testing with xUnit.net. I want a 'Theory' test with '[InlineData]' which includes 'decimals':
[Theory]
[InlineData(37.60M)]
public void MyDecimalTest(decimal number)
{
...
36
votes
5
answers
8k
views
Which is better? Unit-test project per solution or per project?
Is it better to have a unit-test project per solution or a unit-test project per project?
With per solution, if you have 5 projects in the solution you end-up with 1 unit-test project containing ...
35
votes
3
answers
27k
views
How to dynamically skip a test with Xunit 2.0?
Xunit 1.9.x provides the user with the DynamicSkipExample.cs example to help him setting up dynamic skipping of a [Fact].
This has proven to be quite useful when performing some cross-platform ...
31
votes
3
answers
41k
views
How to run setup code only once in an xUnit.net test
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 ...
29
votes
2
answers
44k
views
xUnit not awaiting async test
On VS 2013, I can't get this async test to fail.
I have xUnit 1.8.0.1539 (installed from nuget), with the xUnit Test Runner VS extension (0.99.5). All current, AFAIK.
I happen to also have Moq, ...
29
votes
2
answers
22k
views
Are there any good documentations / books / tutorials for xUnit.NET? [closed]
On my search for a Unit-Testing tool for C# i have found xUnit.NET. Untill now, i read most of the articles on http://xunit.codeplex.com/ and even tried out the examples given at How do I use xUnit....
28
votes
3
answers
14k
views
How can I add an assembly binding redirect to a .net core unit test project?
I'm trying to create a .net core unit test project against framework 4.6.1 which tests an project dependent on Microsoft.SqlServer.Types (10.0.0.0). Prior to .net core I'd add an app.config file with ...
28
votes
3
answers
13k
views
Is there a more appropriate to test if the constructor throws an exception?
Normally you test, if an exception gets thrown in a certain method, as follows.
I use FluentAssertions:
[Fact]
public void Exception_gets_thrown()
{
// Arrange
var foo = new Foo("...
28
votes
2
answers
7k
views
Visual Studio 2017 Unit Test Project vs xUnit Test Project
In .NET Core under Visual Studio 2015 one had to create a unit test project that was based on xunit, though the command line (no dedicated project was available in Visual Studio for .NET Core).
In ...
26
votes
3
answers
36k
views
How to setup Mock of IConfigurationRoot to return value [duplicate]
I have used IConfigurationRoute to access a directory like this.
if (type == "error") directory = _config.GetValue<string>("Directories:SomeDirectory");
_config is IConfigurationRoot injected ...
24
votes
5
answers
14k
views
Is there a way to unit test an async method?
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 ...
24
votes
2
answers
14k
views
ExecutionTimer.cs not found when stepping into code
I am writing some tests using XUnit framework and have to use some external code referenced through a dll.
When I try to step into the method call, helper.GetEntity(), I get the following window and ...
24
votes
2
answers
5k
views
Running unit tests in TFS/VSO Build vNext using xUnit adapter
I am trying to run our xUnit tests using the xUnit test adapter in Visual Studio Online's Build vNext platform.
As stipulated in this article, we need to provide a custom test adapter path pointing ...
23
votes
1
answer
23k
views
How to run xUnit test DLL in Commandline without building the project
I have a project in .NET Core and have built tests using xUnit. Now I wanted to run the test in deployment process. What I have done so far:
I used this command in commandline:
dotnet test [...
23
votes
2
answers
27k
views
xUnit Assert.All() async
I have this example test using xUnit:
[Fact]
public void SomeTest()
{
Assert.All(itemList, async item=>
{
var i = await Something(item);
...
22
votes
4
answers
16k
views
xUnit.net how can I specify a timeout how long a test should maximum need
I have integration tests set up with xUnit.net.
Is there a way to configure how long an integration test should last maximum? I mean a threshold.