Click here to Skip to main content
15,881,139 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 46K   445   21  
An article on quickly debugging NUnit tests.
//-------------------------------------------------------------------------------------
// <copyright file='CodeFileTests.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 Jonno.AddIns.Entities;
    using NUnit.Framework;

    [TestFixture]
    public class CodeFileTests
    {        
        /// <summary>
        /// The system under test;
        /// </summary>
        private CodeFile sut;

        [Test]
        public void IfFileIsAssemblyInfoShouldSetFlag()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.cs");
            Assert.That(this.sut.IsAssemblyInfo, Is.EqualTo(true), "true");

            this.sut = new MockCodeFile(@"c:\swsws\sws\Someclass.cs");
            Assert.That(this.sut.IsAssemblyInfo, Is.EqualTo(false), "false");
        }

        [Test]
        public void IfFileIsCSharpShouldSetFlag()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.cs");
            Assert.That(this.sut.IsCSharpFile, Is.EqualTo(true), "true");

            this.sut = new MockCodeFile(@"c:\swsws\sws\Someclass.vb");
            Assert.That(this.sut.IsCSharpFile, Is.EqualTo(false), "false");
        }

        [Test]
        public void IfFileIsVBShouldSetFlag()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.vb");
            Assert.That(this.sut.IsVBFile, Is.EqualTo(true), "true");

            this.sut = new MockCodeFile(@"c:\swsws\sws\Someclass.cs");
            Assert.That(this.sut.IsVBFile, Is.EqualTo(false), "false");
        }

        [Test]
        public void ShouldSetExtension()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.vb");
            Assert.That(this.sut.Extension, Is.EqualTo(".vb"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.cs");
            Assert.That(this.sut.Extension, Is.EqualTo(".cs"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeXML.xml");
            Assert.That(this.sut.Extension, Is.EqualTo(".xml"));
        }

        [Test]
        public void ShouldSetFilenameWithoutExtension()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.vb");
            Assert.That(this.sut.FileNameWithoutExtension, Is.EqualTo("AssemblyInfo"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeClass.cs");
            Assert.That(this.sut.FileNameWithoutExtension, Is.EqualTo("SomeClass"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeXML.xml");
            Assert.That(this.sut.FileNameWithoutExtension, Is.EqualTo("SomeXML"));
        }

        [Test]
        public void ShouldSetShortname()
        {
            this.sut = new MockCodeFile(@"c:\swsws\sws\AssemblyInfo.vb");
            Assert.That(this.sut.ShortName, Is.EqualTo("AssemblyInfo.vb"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeClass.cs");
            Assert.That(this.sut.ShortName, Is.EqualTo("SomeClass.cs"));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeXML.xml");
            Assert.That(this.sut.ShortName, Is.EqualTo("SomeXML.xml"));
        }
        
        [Test]
        public void IfVborCSharpFileShouldGetText()
        {
            var expected = "Public class Paul { public void jjj() {  } }";

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeClass.cs", expected);
            Assert.That(this.sut.Text, Is.EqualTo(expected));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeClass.vb", expected);
            Assert.That(this.sut.Text, Is.EqualTo(expected));

            this.sut = new MockCodeFile(@"c:\swsws\sws\SomeXMl.xml", expected);
            Assert.That(this.sut.Text, Is.EqualTo(string.Empty));
        }
    }
}

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