Abstract unit tests

0

I have a problem when using abstract classes to keep my code common to my tests, initialization, shutdown etc ... I use a concrete class just to initialize the variables. The inherited code of the abstract class does not run when running the tests, I believe some configuration is missing, an example code follows:

        namespace meusTestes
        {
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using NUnit.Framework;

            public abstract class AbTest
            {
                public abstract string getString();

                [Test]
                public void SharedTest()
                {
                    Assert.NotNull(getString()); //Don't execute
                }
            }

            [TestFixture]
            public class RealTest : AbTest
            {
                public override string getString()
                {
                    return " "; //Don't execute
                }

                [Test]
                public void InternalTest()
                {
                    Assert.IsTrue(true); // Execute
                }
            }
        }

The InternalTest test runs correctly, but the inherited (SharedTest) class of the abstract class does not, my doubt is why the inherited does not execute dodo as the documentation provides for inheritance testing.

    
asked by anonymous 30.09.2014 / 15:36

2 answers

1

Following the suggestion of Cigano follows the code:

namespace meusTestes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NUnit.Framework;

    public abstract class AbTest
    {
        public abstract string getString();

        public virtual void SharedTest()
        {
            Assert.NotNull(getString()); 
        }
    }

    [TestFixture]
    public class RealTest : AbTest
    {
        public override string getString()
        {
            return " "; 
        }

        [Test]
        public void InternalTest()
        {
            Assert.IsTrue(true); 
        }

        [Test]
        public override void SharedTest() { base.SharedTest(); }
    }
}
    
30.09.2014 / 21:07
0

The class AbTest does not needs to be abstract, as you do not have to worry about instantiating these classes, since NUnit will do this for you.

I always use "base" test classes to encapsulate implementations that will be reused in daughters and work quietly for calls of base.Metodo() and properties protected .

Solution

Remove the abstract .

    
01.10.2014 / 21:14