22

I am trying to show some information from a [TestMethod] method.

Usually we use NUnit and a line with Console.WriteLine runs fine and we can see it in 'output' window, but on this project we must to use Testing tools embebed with VS2010 and Console.WriteLine doesn't run because we cannot see anything.

What I want is show trace messages on the 'Output' Window in this way more or less:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace Test1
{
    [TestClass]
    public class TestNum1
    {
        [TestMethod]
        public void Constructors()
        {
            for (int b = 1; b < 99; b++) {
                Console.WriteLine(b.ToString());  // <<<<<<< This don't show on Output.
                Assert.AreEqual(b, b);  // This is only a silly sample.
            }
        }
    }
}
ferpega
  • 3,182
  • 7
  • 45
  • 65
  • Yes, you are right. Sorry my duplicate question but I must add a note: You must execute your Tests in Debug-mode or you cannot see any output. – ferpega Sep 18 '11 at 13:54

3 Answers3

30

you should replace Console.WriteLine with System.Diagnostics.Debug.WriteLine(...)

and you will see the output in the Visual Studio Debug Output Window.

Edit: just found out now this is a duplicated question, look here:

How to write to Console.Out during execution of an MSTest test

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 3
    Hi @Davide Piras, I have changed `Console.WriteLine` with `System.Diagnostics.Debug.WriteLine` but I cannot still see anything in my output window. – ferpega Sep 18 '11 at 13:49
  • 1
    Ok.. I have seen I MUST to execute Tests in debug-mode or I cannot see anything. – ferpega Sep 18 '11 at 13:51
  • You must execute your tests in debug-mode if and only if you are using Debug.WriteLine - if using Trace.WriteLine you may use any build flavour - including release – rudolf_franek Sep 18 '11 at 14:03
  • 1
    Thanks @rudolf_franek but I cannot get it to show anything with `Trace.WriteLine`. It only shows text in Debug mode (like `Debug.WriteLine`). Sure I am doing something wrong. – ferpega Sep 18 '11 at 14:24
  • @Davide Piras is right - use Trace.Listeners.Add to add existing listener type or create your own type if you need something really special. Easiest standard way is: Trace.Listeners.Add(new System.Diagnostics.DefaultTraceListener()); – rudolf_franek Sep 18 '11 at 16:28
  • It seems easy, but it doesn't run for me. I cannot see anything in my Output Window. There, in my Ouput Window, I can only select `Build` and no other option. – ferpega Sep 19 '11 at 08:34
20

You can use the testContextInstance.WriteLine(string s);

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
Peter
  • 249
  • 2
  • 3
  • 3
    Would be very nice to know where that should come from. – Akku Apr 03 '13 at 13:50
  • 7
    @Akku, just add `public TestContext TestContext { get; set; }` to your class. The test runner populates it for you. You can also receive it as a parameter for your `ClassInitialize` and `AssemblyInitialize` methods. – Sam Aug 29 '13 at 00:51
  • Exactly what I was looking for! Outputted text straight to the test results window- perfect! – Shawson Jan 03 '14 at 11:56
  • 1
    This is what I wanted. It shows up in the Test Explorers Output window ( Output link at bottom of VS Test Explorer). – Patrik Lindström Dec 13 '14 at 06:10
  • 1
    @Sam having the TextContext property getter/setter is really an answer. too bad its buried as a comment – BozoJoe Jan 15 '16 at 22:55
5

You can force the display of Console.WriteLine() by running MSTest command line with option /detail:stdout.

for example:

MSTEST /testcontainer:MyDllToTest.dll /testSettings.local.TestSettings /detail:stdout

(note I use /testSettings to prevent DLL copy (deployment) in a testResults directory

eeerahul
  • 1,629
  • 4
  • 27
  • 38