nunittest-fixture

NUnit: How to start a test only with one userRole, while there are 3 different userRoles defined in TestFixture


I have a "Regression Test" Class in which i defined 3 different user Roles in the TestFixture enter image description here

In the regression test i'm testing other web app functionalities beside of the main feature functionalities. Currently im having there 5 different tests as seen below in the screenshot. enter image description here

What i want now is, that the first 4 Test Methods will be started ONLY with the admin user Role, and the last Test Method called UserRights should be tested only with hsuINT and hsuIntRequest

The problem im having right now is, that all the 5 Test Methods will be started with all userRoles defined at the class in TestFixture

enter image description here

Is there a way, how to tell the TestMethod with which UserRole it should start the test?


Solution

  • In NUnit, the primary purpose of a TestFixture is to group together all the tests that need a common setup. So if the first four tests require the user role admin, and the last one requires running under two different roles, they don't belong in the same TestFixture.

    You can restructure your tests into two fixtures, similar to this:

    [TestFixture("adminInt")]
    public class FirstFixture
    {
        // Your first four tests here
    }
    
    [TestFixture("hsuINT")]
    [TestFixture("hsuIntRequest")]
    public class SecondFixture
    {
        // Your last test here
    }
    

    Of course, the first fixture doesn't really need a parameter... you can hard code the role if you prefer.

    If splitting the fixture requires duplicating a lot of setup code (which you don't show in the question) you can derive each class from a common base, which has the setup. In that case, all three classes should have the parameter but only the two derived classes should have the TestFixture attribute.