1

I'm running unit tests on my GetModelAsync() and CreateModelAsync() methods in VS17. I then run Assert.Equal checks on the model fields to ensure they match the expected values.

I want to be able to see the final state of my models, which will help me determine why a test is failing or allow me to manually check my models. I'm thinking something similar to tracking variables in the debugger would make sense, although I don't want to actually run the debugger.

Is there a way to do this?

I Stand With Israel
  • 1,971
  • 16
  • 30
  • Generate a text file with in the information you want to see, and use `Process.Start` to open it when the test case finished. – Lex Li Aug 23 '18 at 19:51

1 Answers1

3

You can write to the console in your tests and it will show up in Test Explorer. You may want to serialize complex objects to JSON first before doing this. For example:

Console.WriteLine(JsonConvert.SerializeObject(myObject));

Note, for Visual Studio's built in test runner, you have to go through a few steps to see the console output. In the Test Explorer window, click the name of your test, then in the results panel click the Output link, which will open a separate window to show the console output. It's all very unintuitive.

enter image description here

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
  • I tried that but my Test Explorer window does not show anything. – I Stand With Israel Aug 23 '18 at 19:04
  • Is there a window that I should have expanded, other than my Test Explorer? – I Stand With Israel Aug 23 '18 at 19:05
  • Its a little hidden, sorry. The Test Explorer has two sides, the list of tests and the results of a test, where it will show stacktraces of exceptions. On the test list, select the test that should have logged text. Then you will see a link for `Output` which will show the console data. It's way more hidden than it should be. Resharper's test window doesn't hide it so I forget about that. – JamesFaix Aug 23 '18 at 19:07
  • Yeah. That's exactly the window that I'm looking for and exactly the one that I'm struggling to find. I have my regular Output window open and am showing output from "Tests" which gives me data on when the test started, stopped, and total time elapsed, et cetera... Is that the window that you're referring to? – I Stand With Israel Aug 23 '18 at 19:13
  • No, the window you run tests from. From the top menu Test > Windows > Text Explorer. When a test logs data to the console, it will show an extra link in that window for that test when you have it selected in the list of tests. https://stackoverflow.com/a/38376619/4415493 – JamesFaix Aug 23 '18 at 19:14
  • Unfortunately, Test Explorer of Visual Studio does not show console output. You have to use ReSharper's or other extensions. – Lex Li Aug 23 '18 at 19:50
  • Yeah. I'm realizing that now. I don't have that "Output" link in the picture you sent. I guess it's a Resharper thing, eh? – I Stand With Israel Aug 23 '18 at 20:38