Click here to Skip to main content
15,896,912 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.2K   445   21  
An article on quickly debugging NUnit tests.
//-------------------------------------------------------------------------------------
// <copyright file='NunitProjectTests.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.AddIns.Entities.UnitTests
{
    using System.Collections.Generic;
    using System.IO;    
    using Jonno.Utilities;
    using NUnit.Framework;
    using Rhino.Mocks;

    public class NunitProjectTests
    {
        private const string NunitLocation = @"C:\Program Files\NUnit 2.5.2\bin\net-2.0\nunit.exe";

        private const string TestFolder = @"C:\LogicTestsUnlikelyFolderNameToExistAAAAAAAAA\";

        private const string NonTestFolder = @"D:\LogicTestsUnlikelyFolderNameToExistAAAAAAAAA\";

        private TestProjectFactory factory;

        [SetUp]
        public void Setup()
        {
            this.CreateTestFolder();
            this.DeleteAnyFileFromTestFolder();

            factory = new TestProjectFactory();
        }

        [TearDown]
        public void TearDown()
        {
            this.DeleteAnyFileFromTestFolder();

            try
            {
                Directory.Delete(TestFolder);
            }
            catch
            {
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunIt()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
            list.Add(TestFolder + @"Jonno.AddIns.ugh\bin\Debug\Jonno.AddIns.ugh.dll");
            list.Add(TestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
            list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                foreach (var path in list)
                {
                    Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", path)));
                }

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunItDifferentDirectoriesShouldRetrunThosebeforeTheDifferentOne()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
            list.Add(TestFolder + @"Jonno.AddIns.ugh\bin\Debug\Jonno.AddIns.ugh.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
            list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[0])));
                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[1])));
                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[3])));

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunItDifferentDirectoriesShouldRetrunThoseWithCommonPaths()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
            list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.ugh.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
            list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[0])));
                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[1])));
                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[3])));

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunItCommonPathDoesNotExistShouldDoNothing()
        {
            // check that the folder is at first empty
            Assert.That(Directory.Exists(NonTestFolder), Is.EqualTo(false));

            var list = new List<string>();
            list.Add(NonTestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.ugh.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // recheck
            Assert.That(Directory.Exists(NonTestFolder), Is.EqualTo(false));
        }

        [Test]
        public void CreateNunitProjectFileAndRunItWithOneEntry()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                foreach (var path in list)
                {
                    Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", path)));
                }

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunItWithTwoEntrys()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");
            list.Add(TestFolder + @"Jonno.AddIns.Blah.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                foreach (var path in list)
                {
                    Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", path)));
                }

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunItWithTwoDifferentEntrys()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();
            list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");
            list.Add(NonTestFolder + @"Jonno.AddIns.Blah.dll");

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            Expect.Call(() => processMock.Kill("nunit"));
            Expect.Call(() => processMock.Start(
                NunitLocation,
                "\"" + TestFolder + sut.FileName + "\" /run"));

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there now is a file
            var files = Directory.GetFiles(TestFolder);

            Assert.That(files.GetLength(0), Is.EqualTo(1));
            Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));

            using (var sr = File.OpenText(files[0]))
            {
                Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Settings activeconfig=\"NunitHelper\" />"));
                Assert.That(sr.ReadLine(), Is.EqualTo("  <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));

                Assert.That(sr.ReadLine(), Is.EqualTo(string.Format("    <assembly path=\"{0}\" />", list[0])));

                Assert.That(sr.ReadLine(), Is.EqualTo("  </Config>"));
                Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));

                sr.Close();
            }
        }

        [Test]
        public void CreateNunitProjectFileAndRunIt_NullListDoesNothing()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            List<string> list = null;

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there is not a file
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
        }

        [Test]
        public void CreateNunitProjectFileAndRunIt_EmptyListDoesNothing()
        {
            // check that the folder is at first empty
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));

            var list = new List<string>();

            var mocks = new MockRepository();
            var processMock = mocks.StrictMock<Processes>();

            var sut = this.factory.Create("nunit.framework");
            sut.Process = processMock;

            mocks.ReplayAll();

            sut.CreateAndRun(list, NunitLocation);

            mocks.VerifyAll();

            // check that there is not a file
            Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
        }

        private void CreateTestFolder()
        {
            try
            {
                Directory.CreateDirectory(TestFolder);
            }
            catch
            {
            }
        }

        private void DeleteAnyFileFromTestFolder()
        {
            var files = Directory.GetFiles(TestFolder);

            foreach (string file in files)
            {
                File.Delete(file);
            }
        }
    }
}

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