Questions tagged [xunit]
xUnit is the collection name for unit-testing compliant frameworks following a specific architecture. Implementation differs from language to language but the framework should consist of a Test Runner, Test Case, Test Fixture (Context), Test Suite, Test Execution, Test Result Formatter and Assertions
xunit
3,237
questions
266
votes
5
answers
246k
views
Assert an Exception using XUnit
I am a newbie to XUnit and Moq. I have a method which takes string as an argument.How to handle an exception using XUnit.
[Fact]
public void ...
212
votes
8
answers
271k
views
What is the JUnit XML format specification that Hudson supports?
I have Hudson as continuous integration server and I want to use option 'Publish JUnit test result report'. But I don't use xUnit tools for testing, instead of that i have shell scripts which run ...
198
votes
10
answers
139k
views
NUnit vs. xUnit
What are the differences between NUnit and xUnit.net?
What's the point of developing two of them, not only one?
I've read that xUnit is being developed by inventor of NUnit:
xUnit.net is a unit ...
171
votes
46
answers
125k
views
Visual Studio 2015 or 2017 does not discover unit tests
EDIT 2016-10-19:
The original question was about an issue specific to VS2015 CTP6 with the XUnit test runner. It's clear from the answers that there is a much broader issue with unit test discovery ...
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 ...
169
votes
6
answers
121k
views
xUnit : Assert two List<T> are equal?
I'm new to TDD and xUnit so I want to test my method that looks something like:
List<T> DeleteElements<T>(this List<T> a, List<T> b);
Is there any Assert method that I can ...
161
votes
9
answers
151k
views
How do I skip specific tests in xUnit based on current platform
I have an assembly that I've built on Windows
I want to run the xUnit tests on mono in Linux.
However, I have found that while 400 of these tests can run (in order), that certain tests either hang ...
160
votes
6
answers
118k
views
Python unittests in Jenkins?
How do you get Jenkins to execute python unittest cases?
Is it possible to JUnit style XML output from the builtin unittest package?
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 ...
111
votes
7
answers
79k
views
Populate IConfiguration for unit tests
.NET Core configuration allows so many options to add values (environment variables, json files, command line args).
I just can't figure out and find an answer how to populate it via code.
I am ...
93
votes
5
answers
45k
views
Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?
I'm expanding my Ruby understanding by coding an equivalent of Kent Beck's xUnit in Ruby. Python (which Kent writes in) has an assert() method in the language which is used extensively. Ruby does ...
90
votes
3
answers
45k
views
What happened to Assert.DoesNotThrowAsync() in xUnit?
I migrated my unit test project from version 2.0.0-beta-{something} to 2.0.0 (stable) through NuGet. It seems like Assert.DoesNotThrowAsync() is not available anymore.
For Example:
[Fact]
public ...
87
votes
5
answers
11k
views
Is there a set of "Lorem ipsums" files for testing character encoding issues?
For layouting we have our famous "Lorem ipsum" text to test how it looks like.
What I am looking for is a set of files containing Text encoded with several different encodings that I can use in my ...
80
votes
8
answers
31k
views
Unit test exception messages with xUnit
I'm currently converting my MsTest unit tests to xUnit. With xUnit, is there a way to test exception messages? Is it correct to test exception messages as opposed just the exception type?
77
votes
8
answers
70k
views
Is it possible to use Dependency Injection with xUnit?
I have a test class with a constructor that needs an IService.
public class ConsumerTests
{
private readonly IService _service;
public ConsumerTests(IService servie)
{
_service = ...
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 ...
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",...
70
votes
8
answers
48k
views
How to Unit Test with ActionResult<T>?
I have a xUnit test like:
[Fact]
public async void GetLocationsCountAsync_WhenCalled_ReturnsLocationsCount()
{
_locationsService.Setup(s => s.GetLocationsCountAsync("123")).ReturnsAsync(10);
...
68
votes
4
answers
47k
views
How to implement XUnit descriptive Assert message?
Context
in XUnit github I found this: Add Assert.Equal(expected, actual, message) overload #350 (so a developer ask for a non existing overload see below)
Quote from the answer:
We are a believer ...
67
votes
6
answers
37k
views
How to verify ILogger<T>.Log extension method has been called using Moq?
I created a xUnit project to test this sample code
public class ClassToTest
{
private readonly ILogger<ClassToTest> _logger;
public ClassToTest(ILogger<ClassToTest> logger)
{
...
61
votes
3
answers
10k
views
xUnit Equivalent of MSTest's Assert.Inconclusive
What is the xUnit equivalent of the following MSTest code:
Assert.Inconclusive("Reason");
This gives a yellow test result instead of the usual green or red. I want to assert that the test ...
60
votes
4
answers
121k
views
The following constructor parameters did not have matching fixture data
I'm trying to test my controllers using xUnit but getting the following error during execution of Customer Controller:
"The following constructor parameters did not have matching fixture
data: ...
59
votes
2
answers
56k
views
xUnit or NUnit? What advantages and disadvantages of each other? [duplicate]
What are the pluses and minuses of each framework, comparing to each other?
How well they work with ASP.NET MVC?
How well they support mocking?
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 ...
54
votes
11
answers
32k
views
how to debug with xUnit?
I'm learning xUnit and so far, have found it to be a most useful tool. It's making me rethink some of my coding tactics to TDD instead.
However, I've come across an interesting problem. My test case ...
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")]...
48
votes
2
answers
22k
views
How do you filter xunit tests by trait with "dotnet test"?
I have a .NET Core test project that uses Xunit 2.2. Some of my tests are marked with traits.
[Fact]
[Trait("Color", "Blue")]
public void TestBlue()
{
}
What is the right command line syntax for "...
47
votes
1
answer
87k
views
xunit constructor runs before each test
I have a test class where the constructor is called before each test execution. The data initialized by the constructor is currently not shared among the following tests. I want this initialized data ...
46
votes
3
answers
13k
views
How to attach a message to RSpec check?
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'
46
votes
5
answers
74k
views
Entity Framework Core: Log queries for a single db context instance
Using EF Core (or any ORM for that matter) I want to keep track of the number of queries the ORM makes to the database during some operation in my software.
I've used SQLAlchemy under Python earlier, ...
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 '...
44
votes
2
answers
12k
views
How to combine AutoDataAttribute with InlineData
I heavily use the Autofixture AutoData Theories for creating my data and mocks. However this prevents me from using the InlineData Attributes from XUnit to pipe in a bunch of different data for my ...
42
votes
9
answers
58k
views
Xunit Unit Tests will not run
I am completely stuck on this issue. So my team has a unit test project in a services test project. The tests are discovered in the test explorer pane however when I try and run the tests I get these ...
41
votes
8
answers
19k
views
Using ReSharper, how to show debug output during a long-running unit test?
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 ...
41
votes
2
answers
38k
views
How to get content value in Xunit when result returned in IActionResult type
I have a unit test project using Xunit and the method we are testing returns IActionResult.
I saw some people suggest using "NegotiatedContentResult" to get the content of the IActionResult but that ...
39
votes
3
answers
71k
views
How to get Code Coverage from Unit Tests in Visual Studio 2022 Community Edition?
I've downloaded the latest VS2022 v17.1 Community Edition and it doesn't come with Code Coverage in-built. I'm accustomed to the Enterprise Edition and all I can find is paid options for the Community ...
39
votes
2
answers
8k
views
Running BenchmarkDotNet within XUnit
I am using .NET Core 3.1 in my project (web api, VS2019) and XUnit 2.4.1. Recently I was thinking about adding some performance tests and I came accross this library - BenchmarkDotNet. Since I've ...
38
votes
6
answers
23k
views
xunit test Fact multiple times
I have some methods that rely on some random calculations to make a suggestion and I need to run the Fact several times to make sure is ok.
I could include a for loop inside the fact i want to test ...
37
votes
5
answers
90k
views
Comparing Two objects using Assert.AreEqual()
I 'm writing test cases for the first time in visual studio c# i have a method that returns a list of objects and i want to compare it with another list of objects by using the Assert.AreEqual() ...
35
votes
5
answers
41k
views
Unable to Mock HttpClient PostAsync() in unit tests
I am writing test cases using xUnit and Moq.
I am trying to mock PostAsync() of HttpClient, but I get an error.
Below is the code used for mocking:
public TestADLS_Operations()
{
var ...
35
votes
4
answers
42k
views
Dependency injection in Xunit project
I am working on an ASP.Net Core MVC Web application.
My Solution contains 2 projects:
One for the application and
A second project, dedicated to unit tests (XUnit).
I have added a reference to the ...
35
votes
1
answer
20k
views
xUnit theory test using generics
In xUnit I can have a Theory test that uses generics in this form:
[Theory]
[MemberData(SomeScenario)]
public void TestMethod<T>(T myType)
{
Assert.Equal(typeof(double), typeof(T));
}
...
32
votes
2
answers
12k
views
How do I create .NET framework 4.6 version of XUnit project in Visual Studio 2019?
I notice when I start up Visual Studio 2019, I am unable to create a .NET Framework version of XUnit or NUnit (only MSTests). We have been mandated to use XUnit tests, but our solution is all .NET ...
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 ...
31
votes
1
answer
31k
views
xUnit framework: Equivalent of [TestFixtureSetUp] of NUnit in XUnit?
What is xUnit's equivalent of NUnit's [TestFixtureSetUp]?
We have explored and found that IUseFixture<T> is the equivalent of [TestFixtureSetUp], but it's not working as expected.
As we have ...
30
votes
4
answers
26k
views
.Net core library: How to test private methods using xUnit
The latest xunit framework does not allow test runners in library code when compiled with .Net Core framework (this is allowed for normal Visual Studio code). The solution is to create a separate ...
29
votes
3
answers
65k
views
Issues in Xunit.Assert.Collection - C#
I have a Class Library, it contains the following Model and Method
Model:
public class Employee {
public int EmpId { get; set; }
public string Name { get; set; }
}
Method:
public class ...
29
votes
2
answers
18k
views
How to mock ActionExecutingContext with Moq?
I am trying to test the following filter:
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;
namespace Hello
{
public class ValidationFilter : ActionFilterAttribute
{
...
28
votes
3
answers
28k
views
System.InvalidOperationException: Relational-specific methods can only be used when the context is using a relational database provider
System.InvalidOperationException:
Relational-specific methods can only be used when the context is using a relational database provider.
Getting the above mentioned error while using InMemoryDatabase ...
28
votes
5
answers
19k
views
xUnit - Display test names for theory memberdata (TestCase)
I've been using NUnit for testing and I'm really fond of test cases. In NUnit you can easily set each test name in the test case using the SetName function in a TestCaseData class.
Does xUnit have a ...