Yar
Yar

Reputation: 7476

Initializing the argument in NUnit 'TestCase' attribute

I haven't seen this on their Documentations, but I am looking to do a test like this:

[TestFixture]
public class SampleModel
{
    // stuff

    [TestCase(new DBContext(), 502)]
    public IQueryable<SomeModel> GetPrograms(DBContext dbContext, int? programId)
    {
        // Assert stuff
    }
}

Is it possible to initialize an argument in TestCase attribute?

Upvotes: 0

Views: 300

Answers (1)

Alexander Stepaniuk
Alexander Stepaniuk

Reputation: 6438

No, this is not possible for TestCase attribute. C# attributes accept constant only parameters.

But this is possible with TestCaseSource attribute:

[TestFixture]
public class SampleModel
{
    // stuff

    static object [] GetProgramsCases {
        new object[] { new DBContext(), 502 }
    };


    [TestCaseSource("GetProgramsCases")]
    public IQueryable<SomeModel> GetPrograms(DBContext dbContext, int? programId)
    {
        // Assert stuff
    }
}

Upvotes: 1

Related Questions