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
Filter by
Sorted by
Tagged with
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 ...
wandermonk's user avatar
  • 7,096
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 ...
Vlad Krylov's user avatar
  • 2,724
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 ...
Ruslan's user avatar
  • 2,828
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 ...
Fred Kleuver's user avatar
  • 7,877
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 ...
Tomas Aschan's user avatar
  • 59.4k
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 ...
Petar Petrov's user avatar
  • 2,809
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 ...
metdos's user avatar
  • 13.7k
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?
erikbstack's user avatar
  • 13.1k
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 ...
zchpit's user avatar
  • 3,121
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 ...
monty's user avatar
  • 8,265
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 ...
Andrew Harmel-Law's user avatar
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 ...
der_michael's user avatar
  • 3,211
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 ...
Fabian Barney's user avatar
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?
sduplooy's user avatar
  • 14.5k
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 = ...
Rookian's user avatar
  • 20.1k
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 ...
inquisitive's user avatar
  • 1,630
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",...
Serge Intern's user avatar
  • 2,899
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); ...
Richard Collette's user avatar
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 ...
g.pickardou's user avatar
  • 33.9k
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) { ...
Question3r's user avatar
  • 2,844
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 ...
Muhammad Rehan Saeed's user avatar
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: ...
MePengusta's user avatar
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?
Dmitry's user avatar
  • 789
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 ...
citykid's user avatar
  • 10.4k
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 ...
chronodekar's user avatar
  • 2,676
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")]...
Wijitha's user avatar
  • 1,289
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 "...
natemcmaster's user avatar
  • 26.3k
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 ...
Mojtaba Khooryani's user avatar
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'
Alexey's user avatar
  • 9,307
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, ...
codeape's user avatar
  • 99.1k
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 '...
Timur Lemeshko's user avatar
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 ...
zlZimon's user avatar
  • 2,498
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 ...
CMR's user avatar
  • 559
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 ...
Matt Johnson-Pint's user avatar
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 ...
Julie C.'s user avatar
  • 639
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 ...
Jeremy Thompson's user avatar
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 ...
esgaldir's user avatar
  • 863
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 ...
rjlopes's user avatar
  • 2,460
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() ...
Vishweshwar Kapse's user avatar
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 ...
chandra sekhar's user avatar
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 ...
Bob5421's user avatar
  • 8,473
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)); } ...
Ayb4btu's user avatar
  • 3,298
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 ...
Ross Gustafson's user avatar
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 ...
Chris's user avatar
  • 8,184
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 ...
user avatar
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 ...
Perez Lamed van Niekerk's user avatar
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 ...
user avatar
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 { ...
yokomizor's user avatar
  • 1,567
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 ...
Taufik Shaikh's user avatar
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 ...
Reft's user avatar
  • 2,383

1
2 3 4 5
65