Reputation: 5946
I got an application with two types of users. Say, we got user
This is an example for a test:
[TestFixture]
public class TestCalls
{
private static RestApiClient client;
[SetUp]
public void Init()
{
client = new RestApiClient("http://localhost:1234/");
SetToken("A", "1234");
}
[Test]
public async Task ExampleTest()
{
// a test methods
var value = await client.ExecuteRequestAsync(...);
Assert.That(value, Is.Not.Null.And.Not.Empty)
// more assertions
}
}
SetToken
simply sets the authentication-token on my RestApiClient
-insance.
The problem is that user A gets other values than user B (same type of course, different values, but another database)
I could solve it with using TestCaseAttribute
but I want to have the SetToken
in SetUpAttribute
-method Init()
[Test]
[TestCase("A")]
[TestCase("B")]
public async Task ExampleTest(string user)
{
SetToken(user, "1234"); // of course setting right password
// a test methods
var value = await client.ExecuteRequestAsync(...);
Assert.That(value, Is.Not.Null.And.Not.Empty)
// more assertions
}
Is there any possibility to have s.th like configurations for NUnit? So I could run everything twice (for both users)? Or what could I do to test both users? (Copy-pasting all tests is not a solution)
Upvotes: 1
Views: 1224
Reputation: 5946
Found the solution:
We can add multiple TestFixture
-attributes and give them values.
We need to define a constructor for the test-class with the same amount of arguments.
Then in the constructor we assign those values to fields (here I'm using private readonly fields)
And then we can use them in the SetUp
.
NUnit automatically creates now test-cases for both users.
my Test-class looks like this now:
[TestFixture("A", "1234")]
[TestFixture("B", "ABCD")]
public class TestCalls
{
private static RestApiClient client;
private readonly string username;
private readonly string password;
public TestCalls(string username, string password)
{
this.username = username;
this.password = password;
}
[SetUp]
public void Init()
{
client = new RestApiClient("http://localhost:1234/");
SetToken(this.username, this.password);
}
[Test]
public async Task ExampleTest()
{
// a test methods
var value = await client.ExecuteRequestAsync(...);
Assert.That(value, Is.Not.Null.And.Not.Empty)
// more assertions
}
}
Upvotes: 2