0

I want to debug the launch of several tests and see the messages in the console from them. However, I can not determine to which test each message belongs. There is an method whith [AssemblyInitialise] attribute, but it is executed only once. There is method with attribute of [TestInitialize], but it is performed only for tests of one class, and I do not know how to get the name of the current test in this handler.

[Clarification]

I use MsTest for integration tests. I can switch to Nunit if it is necessary for the solution.

  • Welcome to [https://stackoverflow.com/](https://stackoverflow.com/) ! At this site you are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve).I suggest reading [How to Ask](https://stackoverflow.com/help/how-to-ask). Good Luck! – MorganFreeFarm Jul 11 '17 at 12:38
  • If you go to the top menu and look in Test => Windows => Test Explorer, the new window will appear, and show you all tests that run, what their results were, and the details of any failures. Typically, your tests should run in a couple milliseconds, so writing them to the Console in Visual Studio won't help too much. – krillgar Jul 11 '17 at 12:42
  • @krillgar Thanks. I use integration tests. They are very long. – Святослав Sep 06 '17 at 14:09

1 Answers1

1

You should check this answer How to get the name of the current method from code

I think it does what you need. In order to know where your logged output is coming from, simply prepend the output of the GetCurrentMethod listed there. I'll re-post it here for convenience.

[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod ()
{
    StackTrace st = new StackTrace ();
    StackFrame sf = st.GetFrame (1);

    return sf.GetMethod().Name;
}

If what you need is simply to see what made the test fail, note that selecting a failed test in Test Explorer will show you the stack trace of the failure.

kkirk
  • 352
  • 1
  • 7