0

As widely stated in other posts, ExpectedException does not exist in xUnit. However, is there something that exists to assist in porting legacy tests to xUnit as essentially a polyfill?

I believe that with an accelerated adoption of .Net Core as we will likely see with .Net Standard 2, we will see more and more tests getting ported from other frameworks to those supported most easily out of the box in .Net Core tooling. While ExpectedException is probably not best practice going forward, having a legacy stopgap (even as e.g. a separate NuGet package) would greatly ease porting tests as this is one of the few transformations that is on a per-test basis and cannot be done by a simple search-replace. And when you're porting hundreds of unit tests, the "this can cause problems" doesn't seem like such a satisfying answer.

J Trana
  • 2,150
  • 2
  • 20
  • 32
  • possibly XY problem: if you don't want to port your test code, why do you want to move to xUnit? MSTest 2 and NUnit also support .NET Core. – Martin Ullrich Jun 19 '17 at 15:21
  • Reasonable response, but please consider the recent release of "Visual Studio for Mac". Here xUnit is supported out-of-the-box with IDE integration, NUnit [seems much more cumbersome](https://github.com/nunit/docs/wiki/Getting-Started-in-Visual-Studio-for-Mac). Similarly, for "dotnet test" MSTest and xUnit both seem to be given official-ish support [here](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test) while NUnit is not listed. However, the .Net Core scene seems to have quite a bit of fluctuation in supported/documented/"actually works" ways to write unit tests, so... – J Trana Jun 19 '17 at 18:27
  • I was going to suggest the [xunit converter](https://github.com/dotnet/codeformatter/tree/master/src/XUnitConverter) part of dotnet/codeformatter but it looks like it can't do the `ExpectedException` part. – Martin Ullrich Jun 19 '17 at 18:41
  • Note that IDE support is for VSTest and xUnit, NUnit and MSTest 2 integrate via test adapters for the VSTest framework so it's no different to the IDE. (`dotnet test` also works via VSTest). – Martin Ullrich Jun 19 '17 at 18:42
  • While that makes sense, it is not matching my current experience with the latest Visual Studio for Mac using the NUnit3TestAdapter NuGet package. xUnit works out of the box, and NUnit tests are not being found. (I had NUnit working earlier, but with its own standalone runner rather than IDE integration or dotnet test integration.) Perhaps it's a PEBCAK error of some sort though... – J Trana Jun 19 '17 at 22:04

1 Answers1

1

I may have misunderstood your question, but you can test for exceptions in xUnit by doing something like this:

[Fact]
public void ExceptionTest()
{
    // Arrange
    // Act
    Action act = () => throw new Exception();

    // Assert
    Assert.Throws<Exception>(act);
}

Allowing each ExpectedException test to be converted by adding in the xUnit Assert.Throws.

Ayb4btu
  • 3,208
  • 5
  • 30
  • 42
  • Thanks for reaching out - yes, this is exactly the type of transformation that I was doing. It is not really easily search-replaceable, although it is easily replaceable by hand. I just didn't really feel like doing it several hundred times with a slightly different exception type each time. – J Trana Jun 29 '17 at 01:31
  • You could use regex find and replace to do it, then wrap the entire innards of the 'old' unit test in the `Action`. Though I suspect this would be a tough regex to write, especially for getting the expected exception and putting it into the `Assert`. – Ayb4btu Jun 29 '17 at 05:44