locked
Failing a Test and logging it RRS feed

  • Question

  • Hi,

     

    I have 2 questions:

    1- To fail my test, I am using Assert.Fail("Message") in my catch block. Is it the best method to fail my test?

    2- When using Assert.Fail("Message"), I also use TestContext.WriteLine() ,to log some more information, which I put just below my assert statement. But the test result window only shows the message I had put in the assert statement. How do I see the stuff I had put in my TestContext.WriteLine()?

     

    Thanks,

    Abhijit

     

     

    Thursday, January 20, 2011 2:17 PM

Answers

  • As explained above if need to log any message you need to do it before Assert.Fail as this is last execution point in TestMethod .

    The Other Easy way to maintain is Write TestCLeanup() method.

     [TestCleanup()]
          public void Cleanup()
          {
             MessageBox.Show("TestMethodCleanup");
          }

    This will be called even if There is Assert.Fail in the Test Method . Ideal way to write is Like below. AnyFailure in TestMethod will result in code of TestCleanup to get called up. Any Failure in TestInitialize will result in ClassCleanup to get called .

    [TestClass()]
       public class DivideClassTest
       {
               [ClassInitialize()]
          public static void ClassInit(TestContext context)
          {
             MessageBox.Show("ClassInit");
          }

          [TestInitialize()]
          public void Initialize()
          {
             MessageBox.Show("TestMethodInit");
          }

          [TestCleanup()]
          public void Cleanup()
          {
             MessageBox.Show("TestMethodCleanup");
          }

          [ClassCleanup()]
          public static void ClassCleanup()
          {
             MessageBox.Show("ClassCleanup");
          }

               [TestMethod()]
          [ExpectedException(typeof(System.DivideByZeroException))]
          public void DivideMethodTest()
          {
             DivideClass target = new DivideClass();
             int a = 0;
             int actual;
             actual = target.DivideMethod(a);
          }
       }

     

    Monday, January 24, 2011 6:38 AM

All replies

  • Try the following.

    1. whenever the test fails log the messages(console.writeline, testcontext.write) and then call Assert.fail

    2. use try/catch/finally blocks. the finally block executes after the catch. Even though the test passed or fails the finally block executes.



    ---Mark this post if this Answers or Helps---
    Cheers
    FunnyFox
    Thursday, January 20, 2011 6:34 PM
  • As explained above if need to log any message you need to do it before Assert.Fail as this is last execution point in TestMethod .

    The Other Easy way to maintain is Write TestCLeanup() method.

     [TestCleanup()]
          public void Cleanup()
          {
             MessageBox.Show("TestMethodCleanup");
          }

    This will be called even if There is Assert.Fail in the Test Method . Ideal way to write is Like below. AnyFailure in TestMethod will result in code of TestCleanup to get called up. Any Failure in TestInitialize will result in ClassCleanup to get called .

    [TestClass()]
       public class DivideClassTest
       {
               [ClassInitialize()]
          public static void ClassInit(TestContext context)
          {
             MessageBox.Show("ClassInit");
          }

          [TestInitialize()]
          public void Initialize()
          {
             MessageBox.Show("TestMethodInit");
          }

          [TestCleanup()]
          public void Cleanup()
          {
             MessageBox.Show("TestMethodCleanup");
          }

          [ClassCleanup()]
          public static void ClassCleanup()
          {
             MessageBox.Show("ClassCleanup");
          }

               [TestMethod()]
          [ExpectedException(typeof(System.DivideByZeroException))]
          public void DivideMethodTest()
          {
             DivideClass target = new DivideClass();
             int a = 0;
             int actual;
             actual = target.DivideMethod(a);
          }
       }

     

    Monday, January 24, 2011 6:38 AM