Abstract unit tests

Asked

Viewed 273 times

0

I am having a problem using abstract classes to keep the common code of my tests, shutdown boot etc... I use a concrete class only to initialize the variables. The code inherited from the abstract class does not run when running the tests, I believe it is some configuration missing, follow an example 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();

                [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 runs correctly, but the inherited test (Sharedtest) of the abstract class does not, my doubt is why the inherited test does not run.

  • "O código da classe abstrata não é executado (...)" From what you posted, she doesn’t even have code that can be executed.

  • In the Nunit documentation it is possible to inherit the tests, it follows: May not be Abstract - Although the attribute may be Applied to an Abstract class intended to serve as a base class for test fixtures. http://www.nunit.org/index.php?p=testFixture&r=2.5.9

  • I’m not sure what it might be, but have you tried writing an empty override? Something like: public override void SharedTest() : base() { }

  • Well around, I’ll post the answer, thanks Gypsy

2 answers

1


Following the suggestion of the Gypsy 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(); }
    }
}

0

The class AbTest nay need to be abstract, because you don’t have to worry about instantiating those classes, since Nunit will do it for you.

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

Solution

Remove the abstract.

  • It is still a solution, but in the question I do not include, I want to force the child classes to implement the getString() method. Something very similar to Template method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.