0

This is a simple question, I came across a scenario which got me thinking about the way I have been debugging NUnit tests.

I have a class similar to this

public class SomeClass {
   public static bool SomeMethod(){
      return true;
   }
}

Now I have a NUnit test like so

[TestFixture]
public class UnitTests
{
   [Test]
   public void TestOne()
   {
      var retval = SomeClass.SomeMethod();
      Assert.IsFalse(retval, "Test Failed"); 
   }
}

When I run the test in Debug I get this exception

AssertException

Part of me is saying this is how it should be, in that NUnit would normally catch this exception as the failure, whereas the other part of me is saying no there should not be an exception here the test should just fail?

Deviland
  • 3,324
  • 7
  • 32
  • 53

2 Answers2

2

Using Assert.IsFalse is expecting False. You can use Assert.IsTrue or Assert.AreEqual(true, retval).

From MSDN.

Assert.IsFalse Method - Verifies that a specified condition is false.

Jonas W
  • 3,200
  • 1
  • 31
  • 44
  • 1
    this is a test case to catch the exception I am getting, this case is the simplest way I could demonstrate the exception – Deviland Aug 20 '12 at 12:47
  • Hmm.. If you get a exception it will mark the test as failed. This is how Assert works, or am i missunderstanding you at the moment? – Jonas W Aug 20 '12 at 12:50
  • Oh ok. Well when using the Assert.IFalse(true), this will generate a exception, this will not let you continue. This is by design, if the exception is thrown the test will be marked as failed. It's the same for MSTest and all the test-frameworks that i know about. But i can't quite understand your question. Do you want a Assert.ExceptionIsThrown() or something simular? Or only a explanation about why in the debugger you see the error? – Jonas W Aug 20 '12 at 12:57
  • [Here](http://nunit.com/index.php?p=conditionAsserts&r=2.6.1) is the link to the _NUnit_ documentation on `Assert.IsFalse` etc. When an `Assert` is not fulfilled, NUnit always throws `AssertionException`. Then it should be up to your test runner to handle this exception or not. – Anders Gustafsson Aug 20 '12 at 13:03
  • thanks jonas I just couldn't find why the exception was being thrown as I said in the question I suspected that it was the way it was meant to work but had doubts. Thanks for your answer you have solved my problem – Deviland Aug 20 '12 at 13:11
  • No problem, glad i could help you figureing this out, thought you ment something else from the beginning. – Jonas W Aug 20 '12 at 13:16
1

Are you running this Nunit tests inside VS? Try compile and run your tests through Nunit test runner, outside VS.

If you want run inside VS, try some plugins like Resharper, TestDriven.NET.

Take a look at here too: How do I run NUnit in debug mode from Visual Studio?

Community
  • 1
  • 1
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90