Search code examples
c#nunit

How can I use string interpolation in a parameterized test?


I am doing some API testing in C# with NUnit and RestSharp. I would like to parameterize my GET tests so I am not doing a lot of repeated tests. The problem I am running into is that I have an assignmentGuid which goes into the request. Currently, I am doing the request like so with string interpolation. LufasApiService.SendGetRequestWithResponse($"v2/audit?assignmentGuid={_assignmentGuid}&unitNumber=450&auditPeriodId=1500");

Here is what I have for the parameterized test:

    [Test]
    [TestCaseSource("GetAPIRequests")]
    public void TestGetAPIs(string url, string responseContent) 
    {
        var response = LufasApiService.SendGetRequestWithResponse(url);
        Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        Assert.That(response.ContentType, Is.EqualTo("application/json"));
        Assert.That(response.Content.Contains(responseContent));
    }

    private static IEnumerable<string[]> GetAPIRequests()
    {
        yield return new string[] { $"v2/audit?assignmentGuid={_assignmentGuid}&unitNumber=450&auditPeriodId=1500", "auditInstanceGuid" };
    }

The message I am getting is An object reference is required for the non-static field, method, or property 'APITests._assignmentGuid'

I am not sure what is going on with this and would appreciate any help. Thank you.


Solution

  • Thanks to @Lennart and @AztecCodes for the suggestions. I made the _assignmentGuid static and the test worked splendidly.