Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

Dynamic Unit Testing

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
1 Sep 2011CPOL3 min read 28.8K   173   16  
Dynamic Unit Testing
using System.Reflection;
using NUnit.Framework;

namespace Business.Test
{
    [TestFixture]
    public class RangeFixture
    {
        private readonly dynamic _range;

        public RangeFixture()
        {
            _range = new DynamicObjectBase(typeof(Range<int>));
        }


        [Test,
        Description("Tests two ranges that don't overlap.")]
        public void OverlapTest([Random(0, 1000, 5)] int firstStart,
                                [Random(1001, 2000, 5)] int firstEnd,
                                [Random(2001, 5000, 5)] int secondStart,
                                [Random(5001, 10000, 5)] int secondEnd)
        {
            var methodInfo = typeof(Range<int>).GetMethod("Overlaps", BindingFlags.NonPublic | BindingFlags.Static);
            var result = methodInfo.Invoke(null, new []
                                        {
                                            new Range<int>(firstStart, firstEnd),
                                            new Range<int>(secondStart, secondEnd)
                                        });
            Assert.False((bool)result);
        }


        [Test,
        Description("Tests two ranges that don't overlap.")]
        public void DynamicOverlapTest([Random(0, 1000, 5)] int firstStart,
                                [Random(1001, 2000, 5)] int firstEnd,
                                [Random(2001, 5000, 5)] int secondStart,
                                [Random(5001, 10000, 5)] int secondEnd)
        {
            Assert.False(_range.Overlaps(new Range<int>(firstStart, firstEnd),
                                         new Range<int>(secondStart, secondEnd)));
        }

    }
}

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)
Italy Italy
My name is Idalgo Cantelli. I'm a software developer skilled in .Net technologies. I work with .Net since February, 2002. I also have a strong experience as a technical trainer, having taught in more than thirty classroom courses. I'm MCTS and MCPD-EAD on .Net 2.0, planning an upgrade to 3.5.

Comments and Discussions