Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Console.WriteLine from [TestMethod]? [duplicate]

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.
            }
        }
    }
}
like image 766
ferpega Avatar asked Sep 18 '11 13:09

ferpega


People also ask

Where does the console WriteLine () output go?

It goes to the console (standard output) or to the stream that the console is set to.

How do I print a console WriteLine?

To print a message to the console, we use the WriteLine method of the Console class. The class represents the standard input, output, and error streams for console applications. Note that Console class is part of the System namespace. This line was the reason to import the namespace with the using System; statement.

What is use of console WriteLine () method?

WriteLine(String, Object, Object)Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

What can I use instead of console WriteLine?

You can use a logging library. log4net seems to be the most popular choice.


1 Answers

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

like image 126
Davide Piras Avatar answered Oct 02 '22 19:10

Davide Piras