Partager via


Poor mans bootstrapping unit testing

I wrote in a pervious post about my inability to use NUnit, and my want to avoid my own unit testing engine.  I felt caught between a rock and a hard place.  I want to test, but i don't have to the testing tools available.  I mentioned that i would need some sort of bootstrapping system and I think I've got it.  Here's the current ICollectionTests class:

namespace Testing.Collections.Tests

{

    [TestFixture]

    public abstract class ICollectionTests

    {

        protected ICollectionTests() {

        }

        protected abstract ICollection<A> GetNewCollection<A>();

        [Test]

        public void InitiallyEmpty() {

            Assert.IsTrue(false);

            ICollection<int> collection = GetNewCollection<int>();

            Assert.IsTrue(collection.Empty);

        }

        [Test]

        public void EmptyAfterAdd() {

            Assert.IsTrue(false);

            ICollection<int> collection = GetNewCollection<int>();

            bool modified = collection.Add(0);

            if (modified) {

                Assert.IsFalse(collection.Empty);

            } else {

                Assert.IsTrue(collection.Empty);

            }

        }

        public void RunAllTests() {

            InitiallyEmpty();

            EmptyAfterAdd();

        }

    }

}

As you can see i run all the tests by running (surprise surprise) RunAllTests.  Note that initially all test are designed to fail.  This is so i can be sure that the tests are actually running.  I know provide a concrete implementation for my ArrayCollection class:

namespace Testing.Collections.Tests

{

    public class ArrayCollectionTests : ICollectionTests

    {

        public ArrayCollectionTests() {

        }

        protected override ICollection<A> GetNewCollection<A>() {

            return new ArrayCollection<A>();

        }

    }

}

To finish it all up, i create an executable out of my test assembly.  I.e:

namespace Testing.Collections.Tests

{

    class Program

    {

        static void Main(string[] args) {

            ArrayCollectionTests arrayCollectionTests = new ArrayCollectionTests();

            arrayCollectionTests.RunAllTests();

        }

    }

}

 

I then try to run the exe and happily find that i get an AssertionException thrown.  I then go back and remove the Assert.IsTrue(false); one by one and at the end i have an executable which runs without a glitch.