Reputation: 145
So this is a bit of a strange setup. I am moving our tests from MSTest (Visual Studio Unit Tests) to NUnit 3+.
In my original test framework, I added a test utility called Verify, in which asserts were made, but the exception suppressed/ignored, and we simply wait until the end of the test to assert whether any failures occurred.
public class Verify {
public static int NumExceptions = 0;
public static void AreEqual(int expected, int actual) {
try {
Assert.AreEqual(expected, actual);
} catch (AssertFailedException) {
NumExceptions++;
}
}
public static void AssertNoFailures() {
Assert.AreEqual(0, _numExceptions);
}
}
So the test code for this might be:
[TestMethod]
public void VerifyPassesCorrectly() {
int x = 2;
int y = 3;
Verify.AreEqual(3, y);
Verify.AreEqual(2, x);
Verify.AreEqual(5, x + y);
Verify.AssertNoFailures();
}
[TestMethod]
[ExpectedException(typeof(AssertFailedException))]
public void VerifyCountsFailuresCorrectly() {
Verify.AreEqual(3, 2);
Assert.AreEqual(1, Verify.NumExceptions);
}
Both these tests pass, even though an AssertFailedException was thrown
As I move to NUnit, it seems like there are better ways to go about this (Warn, MultipleAssert). Eventually, we will build our new tests to make use of these improvements. However, in the meantime, I need to supply some backward compatibility for existing tests.
My original plan was to simply swap out libraries and change the exception type:
public static void AreEqual(int expected, int actual) {
try {
Assert.AreEqual(expected, actual);
} catch (AssertionException) {
NumExceptions++;
}
}
This requires no substantive changes to the existing test code, nor really to the structure of the Verify class. However, when I execute tests like this from within Visual Studio using NUnit Adapter, that second test runs as expected (doesn't exception out), but still fails the test, listing the exceptions that were found during the Verify steps.
The broader solution is to simply remove the Verify class, since it is no longer needed thanks to NUnit. But until that happens, is there a way to use the NUnit API inside Verify so that the Assertions in the Verify class aren't "stored" by NUnit and used to fail the tests?
Upvotes: 2
Views: 5678
Reputation: 23796
You won't be able to tell NUnit that Asserts should somehow not make a test fail. So, what you can do is change your AreEqual method to just do the equality test yourself.
if (expected != actual) {
NumExceptions++;
}
This seems to be the simplest solution.
The second option is to do exactly what NUnit is doing in its Assert statements. If you want to do that (but without causing the test to fail of course). The code would look like this:
public static void AreEqual(int expected, int actual) {
var equalToConstraint = Is.EqualTo(expected);
var result = equalToConstraint.ApplyTo(actual);
if (!result.IsSuccess) {
NumExceptions++;
}
}
The Is
class is part of NUnit, but it is public and you could use it like this if you want.
Upvotes: 2