Boriska64 Boriska64 - 1 year ago 111
C# Question

Pass a List<SomeObject> paramter to NUnit Test

I am working on a NUnit test that needs to be able to accept a List as a parameter, and I am wondering how can I pass that list as a parameter?

I can pass in each user individually to the unit test like so:

[Test, TestCaseSource("GetUsers")]
public void MyExample_Test(User user)
{
Asset.Pass();
}

private static List<User>GetUsers()
{
List<User>users=new List<User>();
users.Add(new User());
users.Add(new User());
users.Add(new User());
users.Add(new User());

return users;
}


But I need to pass in
List<User>
as WHOLE.

Answer Source

The purpose of TestCaseSource is to return a collection where each object in the collection creates a new Test from your test method. You want to pass in a collection, so you need to wrap that collection in a collection. Talk about recursive :)

You want something like,

[Test, TestCaseSource("GetUserLists")]        
public void MyExample_Test(IList<User> users)
{   
    Asset.Pass();
}

private static IEnumerbale<List<User>> GetUserLists()
{
    IList<User> users = new List<User>();
    users.Add(new User());
    users.Add(new User());
    users.Add(new User());
    users.Add(new User());

    return new [] { users };
}

Normally, you will want to add multiple collections to the collection you are returning otherwise you don't need TestCaseSource, you would just construct your collection in your test method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download