Why you should use MbUnit

Saying that you should use MbUnit because it allows you to keep track of what tests pass and fail in daily build reports misses the biggest reason to use it.

I never could understand the point of the *Unit unit test framework but now I do. It’s really quick and easy to use. You create a library and adds some public classes and label them as test fixtures. Include the project you want to test and code up some tests. Then run the class library via the console or the gui application.

using System.Collections.Generic;
using MbUnit.Framework;
using MyLib;

namespace UnitTests
{
    [TestFixture]
    public class CommandLineParserTests
    {
        [Test]
        public void BoundCheck()
        {
            CommandLineParser parser = new CommandLineParser();
            parser.parseCommandLine("");
            Assert.AreEqual(parser.GetRest().Count, 0);
            Assert.AreEqual(parser.GetProgram(), "");
        }

        [RowTest]
        [Row("test.exe", "test.exe", new string[] {}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("..\\bleh test", "..\\bleh", new string[] {"test"}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("\"C:\\Program Files\\UltraEdit\\uedit32.exe\" file.txt", "C:\\Program Files\\UltraEdit\\uedit32.exe", new string[] { "file.txt"}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("test.txt gooner.txt ", "test.txt", new string[] {"gooner.txt"}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("test.txt  gooner.txt ", "test.txt", new string[] {"gooner.txt"}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("test.txt   gooner.txt  test", "test.txt", new string[] {"gooner.txt", "test"}, new string[] {}, new string[] {}, new string[] {} )]
        [Row("test.txt gooner.txt - ", "test.txt", new string[] {"gooner.txt", "-"}, new string[] {}, new string[] {}, new string[] {} )]
        public void NothingExpected(string commandLine, string program, string []rest, string []flags, string []param, string []values)
        {
            CommandLineParser parser = new CommandLineParser();
            parser.parseCommandLine(commandLine);

            List<string> lRest = new List<string>(rest);
            List<string> lFlags = new List<string>(flags);
            List<string> lParams = new List<string>(param);
            List<string> lValues = new List<string>(values);
            Assert.AreEqual(program, parser.GetProgram());
            for (int i = 0; i < parser.GetRest().Count; i++)
            {
                Assert.AreEqual(parser.GetRest()[i], lRest[i]);
            }
            foreach(string flag in lFlags)
            {
                Assert.IsTrue(parser.IsFlagSet(flag));
            }
            for (int i = 0 ; i < lParams.Count; i++ )
            {
                string flag = lParams[i];
                string value = lValues[i];
                Assert.IsTrue(parser.IsFlagSet(flag));
                Assert.AreEqual(value, parser.GetValue(flag));
            }
        }
	}
}


You might want to look at TestDriven.net too. It’s useful for running the tests through the debugger or just in the IDE.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 64 other followers