简体   繁体   中英

Why I can't see the debug output when I run a test in Visual Studio 2012?

I am very new in C# and Visual Studio (I came from Java) and I have the following doubts about how debug work in Visual Studio.

So I have this unit test:

    [TestMethod]
    public void GetVulnerability()
    {
        DataModel.VulnerabilityManager manager = new DataModel.VulnerabilityManager("DefaultConnection");

        try
        {
            manager.openConnection();


            List<DataModel.Vulnerability.VulnSmall> risultato;

            DataModel.Vulnerability.SearchVuln model = new DataModel.Vulnerability.SearchVuln();
            model.PageSize = 20;
            /*model.Filter.CVE = "CVE-2013-2902(Candidate)";*/

            int numeroRecordsTotali;

            risultato = manager.getList(model, out numeroRecordsTotali);

            foreach (DataModel.Vulnerability.VulnSmall v in risultato ){
                Debug.WriteLine("MY CVE: " + v.CVE + "**");
            }

        }
        finally
        {

            manager.closeConnection();
        }

So, into the foreach I print the test output by the line

Debug.WriteLine("MY CVE: " + v.CVE + "**")

The thing that I can't understand is that when I go to execute the test clicking on Run selected test voice I can't see the test output that have to be written by my previous line but I can see it if I click on the Debug selected test voice.

Why? What can I do to show the desidered output (and not only if the test is passed) in Visual Studio debug?

I think this has to do with the fact that Debug.WriteLine() is tagged with [ConditionalAttribute("DEBUG")] .

For all intents and purposes, you may think of it being equivalent to this:

public void WriteLine(string message)
{
#if DEBUG
    Console.WriteLine(message);
#endif
}

Now you probably already guessed it, but your tests don't run in debug mode.

Solution

Use the Trace class instead of the Debug class.

foreach ( DataModel.Vulnerability.VulnSmall v in risultato )
{
    Trace.WriteLine("MY CVE: " + v.CVE + "**");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM