Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / C#

How to quickly debug a NUnit test in Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.67/5 (6 votes)
30 Sep 2009CPOL5 min read 46.1K   445   21  
An article on quickly debugging NUnit tests.
//-------------------------------------------------------------------------------------
// <copyright file='Program.cs' company='Jonno'>
//     Copyright (c) Paul Johnson 2009 (email:paulmichael.johnson@gmail.com)
//     Code is released under The Code Project Open License (CPOL).
// </copyright>
//-------------------------------------------------------------------------------------

namespace Jonno.SimpleTestRunner
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;
    using Jonno.SimpleTestRunner.Logic;
    
    public static class Program
    {
        /// <summary>
        /// Runs a test using reflection so that Visual Studio can debug it.
        /// The args must be in order:        
        ///     args[0] = assembly path
        ///     args[1] = class name (without namespace)
        ///     args[2] = test method name
        ///     args[3] = framework to run on such as nunit        
        ///     args[4] and above will be the assembly paths of the projects
        /// </summary>
        /// <param name="args">The arguments as outlined above.</param>
        [STAThread]
        public static void Main(string[] args)
        {
            try
            {
                // load all the projects into memory so that the debugger will enter them if required to
                var list = new List<Assembly>();

                for (int i = 4; i < args.Length; i++)
                {
                    var assembly = Assembly.LoadFrom(args[i]);

                    if (assembly != null)
	                {
                        list.Add(assembly);
	                }                    
                }
                
                // set up the test run
                var runner = new TestRunner(args[3]);

                if (!runner.CreateTestClassInstance(args[0], args[1]))
                {
                    MessageBox.Show(string.Format("Could not create instance of {0}.{1}", args[0], args[1]));
                    return;
                }

                int j = 0;

                // wait for Visual Studio to attach to this process 
                // wait for three seconds before deciding that it won't happen
                while (!Debugger.IsAttached)
                {
                    j++;
                    Thread.Sleep(100);

                    if (j == 30)
                    {
                        MessageBox.Show("Could not attach to debugger.\r\nTry again.", "Try again.", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
                        return;
                    }
                }

                Thread.Sleep(100);
               
                // now a debugger is attached we can run the test
                runner.RunTest(args[2], true);                                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception of type {0} was thrown.\r\n{1}\r\n\r\n{2}", ex.GetType(), ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I have over 15 years of development experience, in many different languages, programming styles and platforms. Currently working as a C# coder, and residing in north Herts in the UK. I love lean software development and anything that reduces a grind to leave more time for useful coding!

Comments and Discussions