2

I've a method test that does not have a clear assert expression. The returned tested value is a very long string that has to be inspected by a programmer to check if it is correct or not. For this reason, if the code executes without exceptions, I'm calling 'Assert.Inconclusive'.

However, if some kind of Exception is thrown, I want to call 'Assert.Fail' with the exception message. Something like this:

      [TestMethod()]
      public void Test()
      {
        try {
            string toBeChecked = MethodToBeTested();
            //the string is so particular that no clear
            //assertion can be made about it.

            Console.WriteLine(toBeChecked);             
            Assert.Inconclusive("Check the console output.");            
        } catch(Exception e) {
            Assert.Fail(e.Message);
        }
      }

The problem with this code is that if no regular exception is thrown, the Assert.Inconclusive method also throws an exception that is catched, so Assert.Fail is called, and from the IDE test result pane it seems that the test has failed. This is not what I want.

Is there a way to filter the exceptions, such as catching every exception but the Assert-like ones?

(I'm using .NET framework 3.5SP1)

2
  • Does the method call other methods that can be tested discretely? If you can test they all work as expected you can gain some confidence in the calling method. Commented Sep 6, 2011 at 11:07
  • @Greg B: In this case it was a "monolithic" method. But I don't mind about it, all I wanted was other developers being able to see the inconclusive result but not the exception, unless there actually was one. Commented Sep 6, 2011 at 11:53

3 Answers 3

2

Why not just leave out the Assert.Inconclusive()? In fact, why catch any exceptions at all - if the code throws an exception the unit test framework will mark it as failed. Less is more:

  [TestMethod()]
  public void Test()
  {
     string toBeChecked = MethodToBeTested();
     Console.WriteLine(toBeChecked);             
  }

But this is a poor unit test if we can not automatically check the result. All we are checking is that no exception is thrown. Are there no asserts you can make about the resulting string?

For example, that is is not null or empty? Do we expect it to contain a certain sub-string that we can test?

At least give the test a helpful name, which includes something like: ManualAssertRequired

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I didn't know that throwing exceptions means the test has failed. Unfortunately, no extra assertion can be made, so yes, the test isn't as useful as usual.
2

The Assert.Inconclusive method should throw a AssertInconclusiveException so you can either mark the Test as ExcpectedException(typeof(AssertInconclusiveExcpetion)) or use something like this:

  [TestMethod()]
  public void Test()
  {
    try {
        string toBeChecked = MethodToBeTested();
        //the string is so particular that no clear
        //assertion can be made about it.

        Console.WriteLine(toBeChecked);             
        Assert.Inconclusive("Check the console output.");            
    } catch(AsssertInconclusiveException) {
       /* Do nothing */
    }
    } catch(Exception e) {
        Assert.Fail(e.Message);
    }
  }

Comments

1

Try to catch specific exception type instead of Exception or add another catch for nunit exception that is caused by Assert.Inconclusivemethod which is AssertInconclusiveException...

For example modify it like this:

  [TestMethod()]
  public void Test()
  {
    try {
        string toBeChecked = MethodToBeTested();
        //the string is so particular that no clear
        //assertion can be made about it.

        Console.WriteLine(toBeChecked);             
        Assert.Inconclusive("Check the console output.");    
    } catch(AssertInconclusiveException e) {
        // do nothing...
    } catch(Exception e) {
        Assert.Fail(e.Message);
    }
  }

2 Comments

Is there a superclass for AssertInconclusiveException, that catches also every other assert?
There is indeed: UnitTestAssertException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.