291 questions
158
votes
13
answers
168k
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 ...
171
votes
46
answers
126k
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 ...
28
votes
3
answers
32k
views
Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc
I am trying to implement Dependency Injection in Xunit test for AppService. Ideal goal is to run the original application program Startup/configuration, and use any dependency injection that was in ...
194
votes
10
answers
191k
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 the ...
35
votes
4
answers
45k
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 ...
51
votes
5
answers
85k
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, ...
87
votes
8
answers
82k
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 = ...
31
votes
2
answers
19k
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
{
...
17
votes
2
answers
17k
views
How can I use IConfiguration from my integration tests?
I have an API, and I'm trying to make some integration tests for it with XUnit. Here's my API controller constructor:
public class MyController : Controller
{
readonly IMyRepository _myRepository;...
2
votes
1
answer
482
views
VSTO Outlook Addin Unit Testing with xUnit
I was having some trouble getting unit testing with xUnit to work in Visual Studio (2019) for my VSTO Outlook addin project. I had reviewed the following SO questions and answers:
VSTO Unit Testing ...
70
votes
9
answers
53k
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);
...
26
votes
1
answer
19k
views
.net core 2.0 ConfigureLogging xunit test
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 ...
18
votes
1
answer
10k
views
xUnit Async Test Not Working Properly
We have been using xUnit Framework in our project as test framework since the begining. Currently there are 2200+ unit tests in project and everything seems fine.
But yesterday i decided to run unit ...
9
votes
5
answers
12k
views
How to overwrite a scoped service with a decorated implementation?
I'm trying to write an ASP.NET Core 2.2 integration test, where the test setup decorates a specific service that would normally be available to the API as a dependency. The decorator would give me ...
3
votes
1
answer
3k
views
Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db. How to reload the collection?
What I can't understand fully is exemplified in a unit test below. Basically I want to know why DbContext overrides collection values currently stored in the database even if Reload() and a new load ...
2
votes
1
answer
2k
views
How to use Microsoft.Extensions.Configuration.IConiguration in my XUnit unit testing
In my Asp.net Core 2.0 application, I am trying to unit test my data service layer (.Net Standard Class Library) that uses the Microsoft.Extensions.Configuration.IConfiguration dependency injection.
...
95
votes
5
answers
48k
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 ...
56
votes
3
answers
117k
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 built-in. I'm accustomed to the Enterprise Edition and all I can find is paid options for the Community ...
51
votes
10
answers
70k
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 ...
42
votes
2
answers
42k
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 ...
32
votes
3
answers
45k
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 ...
25
votes
1
answer
26k
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
1
answer
37k
views
Mocking MediatR 3 with Moq
We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit ...
20
votes
4
answers
10k
views
What is the community-preferred Ruby unit testing framework?
In the Java space, JUnit is predominately used and in .NET I believe nUnit is very popular. Is a community agreed upon unit testing framework for the Ruby world?
Background: I ask because I am new ...
15
votes
6
answers
5k
views
In F# how do you pass a collection to xUnit's InlineData attribute
I would like to be about to use a list, array, and/or seq as a parameter to xUnit's InlineData.
In C# I can do this:
using Xunit; //2.1.0
namespace CsTests
{
public class Tests
{
[...