Why is the xUnit Runner not finding my tests

TL;DR your Test Classes must be public (but your Test Methods can be private and/or static) For reasons of efficiency, the xUnit authors have opted to not use BindingFlags.NonPublic when searching for Test Classes in the runner (the MSIL metadata tables don’t index private(/internal) classes to the same degree hence there is a significant performance … Read more

Unit Testing with functions that return random results

Mock or fake out the random number generator Do something like this… I didn’t compile it so there might be a few syntax errors. public interface IRandomGenerator { double Generate(double max); } public class SomethingThatUsesRandom { private readonly IRandomGenerator _generator; private class DefaultRandom : IRandomGenerator { public double Generate(double max) { return (new Random()).Next(max); } … Read more

Is Assert.Fail() considered bad practice?

For this scenario, rather than calling Assert.Fail, I do the following (in C# / NUnit) [Test] public void MyClassDoesSomething() { throw new NotImplementedException(); } It is more explicit than an Assert.Fail. There seems to be general agreement that it is preferable to use more explicit assertions than Assert.Fail(). Most frameworks have to include it though … Read more

Cannot find Assert.Fail and Assert.Pass or equivalent

The documentation includes a comparison chart including this: Fail – xUnit.net alternative: Assert.True(false, “message”) (It doesn’t show Assert.Pass, and I’ve never used that myself, but I suspect the alternative is just to return from the test. Of course that doesn’t help if you want to throw it in a nested method call. My suspicion is … Read more

xunit Assert.ThrowsAsync() does not work properly?

You’re supposed to await the result (see xunit’s acceptance tests for examples and alternate forms). [Fact] async void Test1() { await Assert.ThrowsAsync<ArgumentNullException>(() => MethodThatThrows()); } In this specific degenerate case, you could just return the Task that Assert.ThrowsAsync yields without using await; but in that case, the key thing is to yield the Task back … Read more

Difference between Fact and Theory? – xUnit.net

Assuming that your [AutoMoqData] attribute looks something like this: public class AutoMoqDataAttribute : AutoDataAttribute { internal AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization())) { } } Then, yes, those two tests are equivalent. Both [Fact] and [Theory] attributes are defined by xUnit.net. The [Fact] attribute is used by the xUnit.net test runner to identify a ‘normal’ unit … Read more

Run code once before and after ALL tests in xUnit.net

As of Nov 2015 xUnit 2 is out, so there is a canonical way to share features between tests. It is documented here. Basically you’ll need to create a class doing the fixture: public class DatabaseFixture : IDisposable { public DatabaseFixture() { Db = new SqlConnection(“MyConnectionString”); // … initialize data in the test database … … Read more