7
    [TestInitialize]
    public void SetUp()
    {
    //Do required actions before every test
    }

    [TestMethod]
    public void Test1()
    {
         //Actual test
         Assert.AreEqual(1, 0);
    }

[TestCleanup]
public void TearDown()
{
//If TestMethod has failed - Get the exeception thrown by the TestMethod. So based on this I can take some action?
}

I am able to get TestMethod Name, Test outcome from TestContext. However I want to get the stacktrace of TestMethod in TestCleanup.

Secondly I know one way of achiving this is by wraping test method steps in a try / catch block and setting the exception to a variable or a property and acessing it in tear down.

However I do not want to wrap each and every testmethod in try/catch block. Is there any other cleaner approach available?

I would appriciate a little detailed explaination or an example as I am a novice in MSTest and programming.

Nagesh Nagaraja
  • 165
  • 1
  • 11
  • Have you found a solution for this yet? – FDM Mar 12 '16 at 09:06
  • The only possibility seems to implement your own `TestExecutionExtension`. This alone is easy, however you must also create a separate assembly, which's path needs to be added somewhere in the system's registry (https://blogs.msdn.microsoft.com/vstsqualitytools/2009/09/04/extending-the-visual-studio-unit-test-type-part-1/), bleh... – BartoszKP Sep 20 '16 at 12:54
  • Five years later, if you still have this issue, the psuedo-answer can be found here. What you're wanting isn't natively supported, but can be achieved through appdomain events. It's ugly but it works and can be shoved into a base class where no one ever needs to know about it. https://stackoverflow.com/a/39208343/2254971 – Sidney Feb 21 '20 at 16:49

0 Answers0