xUnit framework: Equivalent of [TestFixtureSetUp] of NUnit in XUnit?

There is no direct equivalent of [TestFixtureSetUp] in XUnit, but you can achieve similar functionality. This page lays out the translation between NUnit and XUnit (as well as a couple other C#/.NET test frameworks). However, XUnit largely got rid of setups/teardowns (this article explains why that decision was made). Instead, you need the test suite … Read more

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

Following the guidance in this xUnit discussion topic, it looks like you need to implement a constructor on the Fixture and also implement IDisposable. Here’s a complete sample that behaves the way you want: using System; using System.Diagnostics; using Xunit; using Xunit.Sdk; namespace xUnitSample { public class SomeFixture : IDisposable { public SomeFixture() { Console.WriteLine(“SomeFixture … Read more

xUnit theory test using generics

You can simply include Type as an input parameter instead. E.g.: [Theory] [MemberData(SomeTypeScenario)] public void TestMethod(Type type) { Assert.Equal(typeof(double), type); } public static IEnumerable<object[]> SomeScenario() { yield return new object[] { typeof(double) }; } There is no need to go with generics on xunit. Edit (if you really need generics) 1) You need to subclass … Read more

How to get Code Coverage from Unit Tests in Visual Studio 2022 Community Edition?

You have Fine Code Coverage working with VS 2022, you can access to it here https://github.com/FortuneN/FineCodeCoverage/releases and click on the 2022 file. After that, it´s just a plugin that you install on your computer and it´s available to every single project without the need to add it project by project. Update: Now it’s available directly … Read more

Dependency injection in Xunit project

You can implement your own service provider to resolve DbContext. public class DbFixture { public DbFixture() { var serviceCollection = new ServiceCollection(); serviceCollection .AddDbContext<SomeContext>(options => options.UseSqlServer(“connection string”), ServiceLifetime.Transient); ServiceProvider = serviceCollection.BuildServiceProvider(); } public ServiceProvider ServiceProvider { get; private set; } } public class UnitTest1 : IClassFixture<DbFixture> { private ServiceProvider _serviceProvider; public UnitTest1(DbFixture fixture) { _serviceProvider … Read more

Unable to Mock HttpClient PostAsync() in unit tests

Non-overridable members (here: HttpClient.PostAsync) may not be used in setup / verification expressions. I also tried to mock the HttpClient the same way you did, and I got the same error message. Solution: Instead of mocking the HttpClient, mock the HttpMessageHandler. Then give the mockHttpMessageHandler.Object to your HttpClient, which you then pass to your product … Read more

Comparing Two objects using Assert.AreEqual()

These answers are far too complicated for the issue. There are no overrides necessary to compare two Lists, and you do not need to break out multiple asserts. Microsoft uses the following class, CollectionAssert. CollectionAssert.AreEqual(expectedList, actualList) This works for Lists, Dictionaries, and whatever else implements ICollection interface. The microsoft documentation is at the following location … Read more

Running BenchmarkDotNet within XUnit

I have created a wrapper test fixture that runs the benchmarks, collects the output and prints the summary at the end: public class Benchmarks { private readonly ITestOutputHelper output; public Benchmarks(ITestOutputHelper output) { this.output = output; } [Fact] public void Run_Benchmarks() { var logger = new AccumulationLogger(); var config = ManualConfig.Create(DefaultConfig.Instance) .AddLogger(logger) .WithOptions(ConfigOptions.DisableOptimizationsValidator); BenchmarkRunner.Run<FooBenchmarks>(config); // … Read more

xunit test Fact multiple times

You’ll have to create a new DataAttribute to tell xunit to run the same test multiple times. Here’s is a sample following the same idea of junit: public class RepeatAttribute : DataAttribute { private readonly int _count; public RepeatAttribute(int count) { if (count < 1) { throw new ArgumentOutOfRangeException(nameof(count), “Repeat count must be greater than … Read more