Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

How to quickly debug a NUnit test in Visual Studio

, 30 Sep 2009
An article on quickly debugging NUnit tests.
Jonno_SimpleTestRunner_demo.zip
Jonno.Extensions.dll
Jonno.AddIns.NunitHelper.dll
Jonno.AddIns.Logic.VisualStudioSpecific.dll
Jonno.AddIns.Entities.dll
Jonno.SimpleTestRunner.exe
Jonno Nunit Helper.AddIn
Jonno.Utilities.dll
Jonno.SimpleTestRunner.Logic.dll
Jonno.SimpleTestRunner.Entities.dll
Jonno_SimpleTestRunner_src.zip
Jonno.Extensions.UnitTests
Properties
vssver2.scc
Jonno.Logic.SimpleTestRunner
Properties
vssver2.scc
vssver2.scc
Jonno.SimpleTestRunner
Properties
vssver2.scc
vssver2.scc
Jonno.SimpleTestRunner.Entities
Properties
vssver2.scc
vssver2.scc
Jonno.Utilities
Properties
vssver2.scc
vssver2.scc
Jonno Nunit Helper.AddIn
Jonno.AddIns.Entities
Properties
vssver2.scc
vssver2.scc
Jonno.AddIns.Entities.UnitTests
Properties
vssver2.scc
vssver2.scc
Jonno.AddIns.Logic.EnvDTE
Properties
vssver2.scc
vssver2.scc
Jonno.AddIns.NunitHelper
bin
Properties
vssver2.scc
vssver2.scc
Jonno.Extensions
Properties
vssver2.scc
//-------------------------------------------------------------------------------------
// <copyright file='KeyValueXML.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.Utilities
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;

    internal class KeyValueXML : KeyValueOptions
    {
        public KeyValueXML(string name, string folderPath)
        {
            this.Name = name;
            this.FolderPath = folderPath;
        }

        public string Name { private get; set; }

        public string FolderPath { private get; set; }

        public Dictionary<string, string> Load()
        {
            return this.LoadElements();
        }

        public void Save(Dictionary<string, string> options)
        {
            this.SaveElements(options);
        }

        private Dictionary<string, string> LoadElements()
        {
            var fileName = this.FolderPath + this.Name + ".xml";
            var list = new Dictionary<string, string>();

            if (!File.Exists(fileName))
            {
                return list;
            }

            var lastElementName = string.Empty;

            using (var reader = new XmlTextReader(fileName))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                lastElementName = reader.Name;
                            }

                            break;

                        case XmlNodeType.Text:
                            {
                                list.Add(lastElementName, reader.Value);
                            }

                            break;

                        default:
                            break;
                    }
                }
            }

            return list;
        }

        private void SaveElements(Dictionary<string, string> list)
        {
            var fileName = this.FolderPath + this.Name + ".xml";
            this.DeleteFile(fileName);

            var xmlDoc = this.CreateXmlDocument();

            this.CreateRootKey(xmlDoc, this.Name, string.Empty);

            foreach (var item in list)
            {
                var xmlElement = xmlDoc.CreateElement(string.Empty, item.Key, string.Empty);
                var xmltext = xmlDoc.CreateTextNode(item.Value);

                xmlElement.AppendChild(xmltext);
                xmlDoc.ChildNodes.Item(1).AppendChild(xmlElement);
            }

            this.SaveXMLDocument(xmlDoc, fileName);
        }

        private void CreateRootKey(XmlDocument xmlDoc, string name, string value)
        {
            var xmlElement = xmlDoc.CreateElement(string.Empty, name, string.Empty);
            var xmlText = xmlDoc.CreateTextNode(value);
            xmlElement.AppendChild(xmlText);
            xmlDoc.AppendChild(xmlElement);
        }

        private void SaveXMLDocument(XmlDocument document, string fileName)
        {
            try
            {
                document.Save(fileName);
            }
            catch (Exception)
            {
            }
        }

        private void DeleteFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                try
                {
                    File.Delete(fileName);
                }
                catch (Exception)
                {
                }
            }
        }

        private XmlDocument CreateXmlDocument()
        {
            var xmlDoc = new XmlDocument();
            var xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, string.Empty, string.Empty);
            xmlDoc.AppendChild(xmlNode);

            return xmlDoc;
        }
    }
}

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)

Share

About the Author

Paul "Jonno" Johnson
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!

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Sep 2009
Article Copyright 2009 by Paul "Jonno" Johnson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid