1

I have a unit test in Visual Studio that I run by doing the standard right-click -> Run Selected Tests. I'm able to see all errors and output inside the unit test explorer window.

Is it possible to pop open a command window when I start the unit test to view data being printed via Console.WriteLine()? I'm creating a publisher/subscriber style application and normally I would open up two command prompts and run it via command line arguments. I'm basically trying to recreate that behavior but during the unit test execution. I.e., when the unit test creates a publisher, open a command window for it and display any information it prints, and do the same for whenever a subscriber is created.

Roka545
  • 3,404
  • 20
  • 62
  • 106
  • 1
    Those tests are supposed to be automated, if you're watching output then it's no longer automated... – DavidG Jan 24 '17 at 00:14

1 Answers1

1

You can run the test from command line prompt, but you won't be able to debug. If you're using MSTest, here's MSDN article on using command line

You can also call the test method from a Console App project, which lets you set breakpoints and debug your code. Here's what you need to do:

  1. Create new Console App project
  2. Add reference to the test project
  3. Create the test class and invoke the test method

There are some limitations if you use advanced aspects of the test framework, but generally you'll be fine if you just recreate the chain of methods invoked by the test framework. For example, MSTest calls methods in the following order:

    var test = new YourMSTestClass();
    test.ClassInitialize(); // use if needed
    test.TestInitialize(); // use if needed

    test.YourTestMethod();

    test.TestCleanup(); // use if needed
    test.Dispose(); // use if needed

    Console.ReadLine(); // so the console window doesn't close
Amadeusz Wieczorek
  • 3,139
  • 2
  • 28
  • 32