简体   繁体   中英

How to run the same Nunit test twice programmatically?

I have using RestSharp to test APIs and I have a delete call that I want to run twice in the same method. The delete call will delete with two different query params. It only can take one query param at a time so I want to call it twice with the two different query params. What is the best optimized solution to this. The example below I am deleting with the user id 1 and I want to also delete with user id 2

       [Test]
        public void DeletedUser()
        {
              response = HttpDelete("url");
              QueryParam.Add("id", 1);
              Assert.statusCode(200, response);
          
      }

I have used Andy solution to use TestCase attribute but I get an syntax error when trying to not hard code the data being used. Error Message: "An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type"

ex..

     public static string data = "1"
 
      [TestCase(data)] //Getting the error here
      [Test]
        public void DeletedUser(string id)
        {
              response = HttpDelete("url");
              QueryParam.Add("id", id);
              Assert.statusCode(200, response);

      }

I need to run the call using two dynamic test data. The data gets generated from a Post call before the Delete call and it gets saved and serialized into a class where I have the data variables.. Here is an example of the class where the test data is stored

public class Data
{
    public class UserData
  {
     public string id1;
     public string id2;

   public UserData()
   { 
       id1 = "";
       id2 = "";
   }

  }
   

} 

This is the Post call and how the data is being saved.

[Test]
public void AddUser()
{
    response = HttpPost("url", modle);
    Data data = new Data()
   data.UserData.id1 = response.content;
}

How can I now use this data.UserData.id1 in my TestCase attribute

You can make use of NUnit's [TestCase] attribute to run a test multiple times with different parameters.

Example

[TestCase(1)]
[TestCase(2)]
public void DeletedUser(int id)
{
    response = HttpDelete("url");
    QueryParam.Add("id", id);
    Assert.statusCode(200, response);

}

You can extend this with as many parameters as needed to complete a test. For example, if you expect different responses for different IDs:

[TestCase(1, 200)]
[TestCase(2, 404)]
public void DeletedUser(int id, int expectedResponseCode)
{
    response = HttpDelete("url");
    QueryParam.Add("id", id);
    Assert.statusCode(expectedResponseCode, response);

}

Full documentation is available here .

Update

In response to your further question about testing with dynamic data, as Charlie said you can only reference literals or literal constants from an attribute so you won't be able to use [TestCase] with dynamic data.

Instead you could use the [TestCaseSource] attribute. Create a static method that retrieves your test data and returns an array, and then quote the name of this method in the attribute.

private static int[] GetIdsToDelete() {
    // UserData userData = ... read in data
    
    return new int[] { 
        userData.id1,
        userData.id2
    }
}

[TestCaseSource(nameof(GetIdsToDelete))]
public void DeletedUser(int id)
{
    response = HttpDelete("url");
    QueryParam.Add("id", id);
    Assert.statusCode(200, response);

}

You can find the full documentation for TestCaseSource here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM