Paul Sjoerdsma
Paul Sjoerdsma

Reputation: 85

Xunit Prevent base class to execute tests

I have recently started using XUnit, we have several methods that we need to test that all use the same test methods and same test data. Obviously creating a base class that contains all the test methods and call a virtual method that can be overriden was my first approach. However when I execute the test runner, the base class is also executed and since the virtual method doesn't contain any code, most of these tests fail.

So how should I structure this code so that the methods in BaseEmployeeTest are not called?

public class BaseEmployeeTest : IEmployeeTest
{


    public virtual void CallTestMethod(string userSessionId, long employeeId)
    {
        // no-op 
    }

    [Theory]
    [MemberData(nameof(TestData.Emp), MemberType = typeof(Data))]
    public void Pu_CanSee_Own_Customer_Employees(long employeeId)
    {
        var ex = Record.Exception(() => CallTestMethod(_fixture.PuUserSessionId, employeeId));


        Assert.Null(ex);
    }
}

public class Test : BaseEmployeeTest
{
    public override void CallTestMethod(string userSessionId, long employeeId)
    {
        CallTestDataMethod("x", "y"
    }
}

Upvotes: 5

Views: 1209

Answers (1)

Ruben Bartelink
Ruben Bartelink

Reputation: 61885

If you make it abstract, that should cover your need, i.e. replace

public class BaseEmployeeTest

with

public abstract class BaseEmployeeTest

Upvotes: 8

Related Questions