Search code examples
c#nunit

C# NUnit No suitable constructor found even when using TestFixture


I'm using NUnit in C# to do some unit tests. I have this class inheritance structure:

[TestFixture(null)]
public abstract class BaseTests
{
    protected BaseTests(StatusesEnum? status)
    {
    }

    [Test]
    public abstract void TestMethod_1();
}



[TestFixture(null)]
public class SalesTests : BaseTests
{
    protected SalesTests(StatusesEnum? status) : base(status)
    {
    }

    //When I run this test from this class it throws the "No suitable constructor was found"
    [Test]
    public override void TestMethod_1()
    {
    }
}



//When I run the test from this class it works perfectly since it passes value to the constructor
public class CustomerTests : SalesTests
{
    public CustomerTests() : base (StatusesEnum.New) { }
}

When I'm running the CustomerTests they run perfectly and they call the TestMEthos_1 from the SalesTests as expected.

But when I'm running only the SalesTests class I keep getting the exception of No suitable constructor found. The expected result should be that the status parameter will be null and the tests should pass since I'm checking that parameter in the tests method.

I found many answers saying to simply add [TestFixture] attribute but that didn't help as well. So any idea on how to fix that would be great.


Solution

  • I solved it by simply changing both base classes (BaseTests & SalesTests) to be abstract since abstract class does not run tests on its own, only the inheriting classes run their tests.

    public abstract class BaseTests
    {
        protected BaseTests(StatusesEnum? status)
        {
        }
    
        [Test]
        public abstract void TestMethod_1();
    }
    
    public abstract class SalesTests : BaseTests
    {
        protected SalesTests(StatusesEnum? status) : base(status)
        {
        }
    
        [Test]
        public override void TestMethod_1()
        {
        }
    }
    
    public class CustomerTests : SalesTests
    {
        public CustomerTests() : base (StatusesEnum.New) { }
    }