Testing and Debugging

From C# Online.NET (CSharp-Online.NET)—your free C# and .NET encyclopedia


Jump to: navigation, search
CSharp-Online.NET:Tutorials
C# Tutorials
Testing and Debugging
edit

TAYC - Test As You Code

If you want to be sure that your code works as expected then you’ll want to test and debug all the time that you are developing your code. There are two possible methods for doing this:

  • Tracepoints
  • Debug class usage


Contents

Tracepoints

Using tracepoints is by far the cleanest way to test and debug your code without adding to your code. They simply appear on the side of your code much like a breakpoint. However, they are not available on the Express Edition of Visual Studio.

Debug Class

The debug class allows you to do what tracepoints can do but require an actual line of code to be added. However, they only add to your code in the development build. Release builds do not include any debug by default.

Add using System.Diagnostics; to the namespaces. Then just reference Debug


  • Default - By default (C# Express 2005) Debug is disabled but Trace is enabled in the Release build.


Basic

  • Assert - Is it what I expect? (And proof that debug is not put in release builds)
  • Fail - Use the Debug.Fail method instead when you find yourself using MessageBox.Show statements for those 'quick/small tests'.
  • Write, WriteLine - Simple Debug output
  • WriteIf, WriteLineIf - Debug Output dependent on result


Advanced

When the output window is just not enough the next place to turn is to external files. There are numerous ways to write to external files. However, many of these will leave unwanted code segments behind in the Release Build.


The quickest and easiest way to write to a debug file is to add an Application Configuration File to your project.


Once you have a .Config file setup in your project just paste the following code inside the <configuration> tags.

<system.diagnostics>
    <trace autoflush="false">
      <listeners>
        <add name="debugListener" 
           type="System.Diagnostics.TextWriterTraceListener"
           initializeData="debug.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

When autoflush="false" you must ensure that you place a Debug.Flush(); statement before your application closes, otherwise nothing will be seen in the specified file. The easiest place to add this is at the end of the Main() method.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace debugTesting {
 
  static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
      System.Diagnostics.Debug.Flush();
    }
  }
}


Note: By default the file path is set to your project/bin/Debug folder.


With autoflush="false" your program must reach a Debug.Close(); or Debug.Flush(); statement in order to save the Debug information to the file.

It is possible to set autoflush="true" so that if your program crashes you will have a full log up until that point. However, this may have a negative impact on the performance of your application due to the file stream.

Saving debug to file

Saving debug information to a file allows you to use your application outside of the development environment yet still keep a debug log. To do this an XML program configuration file must be created.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
	<add name="debugListener" 
           type="System.Diagnostics.TextWriterTraceListener" 
           initializeData="debug.txt" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Personal tools
C# Entry Points