Click here to Skip to main content
15,892,059 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='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)


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