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?

1
  • Generate a text file with in the information you want to see, and use Process.Start to open it when the test case finished. Commented Aug 23, 2018 at 19:51

1 Answer 1

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

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

7 Comments

I tried that but my Test Explorer window does not show anything.
Is there a window that I should have expanded, other than my Test Explorer?
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.
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?
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. stackoverflow.com/a/38376619/4415493
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.