Click here to Skip to main content
15,894,907 members
Articles / Programming Languages / C#

Pragmatic Unit Tests using "yield return" for providing test cases

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
5 Feb 2013CPOL3 min read 38.7K   120   14  
How to gain better control over the testing process reducing the amount of test methods in a unit test.
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            foreach (TestCase testCase in getTestCases())
            {
                // Create the tested class
                var c = new Calculator();
                try
                {
                    // invoke the tested method
                    int result = c.Divide(testCase.Dividend, testCase.Divisor);

                    // check the result
                    Assert.AreEqual(testCase.ExpectedResult, result);
                    Assert.IsNull(testCase.ExpectedExceptionType);
                }
                catch (Exception ex)
                {
                    // an error has occured
                    Assert.IsNotNull(testCase.ExpectedExceptionType);
                    Assert.AreEqual(testCase.ExpectedExceptionType, ex.GetType());
                }
            }
        }

        private IEnumerable<TestCase> getTestCases()
        {
            // both Dividend and diviser are 0
            var tc = new TestCase(0, 0, 0, typeof (DivideByZeroException));
            tc.Description = "both Dividend and diviser are 0";
            yield return tc;

            // Dividend is 0, Diviser is > 0
            tc = new TestCase(0, 1, 0, null);
            tc.Description = "Dividend is 0, Diviser is > 0";
            yield return tc;

            // Dividend is > 0, Diviser is 0
            tc = new TestCase(1, 0, 0, typeof (DivideByZeroException));
            tc.Description = "Dividend is > 0, Diviser is 0";
            yield return tc;
        }

        #region Nested type: TestCase

        private class TestCase
        {
            public TestCase(int dividend, int divisor,
                            int expectedResult, Type expectedExceptionType)
            {
                Dividend = dividend;
                Divisor = divisor;
                ExpectedResult = expectedResult;
                ExpectedExceptionType = expectedExceptionType;
            }

            public int Dividend { get; set; }
            public int Divisor { get; set; }
            public int ExpectedResult { get; set; }
            public Type ExpectedExceptionType { get; set; }
            public string Description { get; set; }
        }

        #endregion
    }
}

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) Polenter - Software Solutions
Germany Germany
I'm C# developer from Cologne, Germany. Here I owe a small software company. My hobby is general optimization - natural talent of all lazy people Wink | ;-)

Comments and Discussions