How to write an Assert test with different cases using the same method, and all of them pass when comparing with different cases of the answer?

You can check many cases, for example:

[TestCase(""), TestCase(null), TestCase("String"), TestCase("2010-07-14T00:00:00.000Z"), TestCase("201-07-14T00:00:00.000Z")] 

And when I get a response from the API with deserialized json to my Error object, I assume that the Response Body Message and HttpStatusCode are correct after the different VIDIDATIONS from the API, and that will be correct from my statement.

Method where I deserialize from Json to a generic type:

 public TType GetResponseJsonAsObject<TType>(HttpResponseMessage content) { var contentResult = content.Content.ReadAsStringAsync().Result; var specialCase = JsonConvert.DeserializeObject<TType>(contentResult); return specialCase; } 

Here is my testing method:

 [TestCase(""), TestCase(null), TestCase("String"), TestCase("2010-07-14T00:00:00.000Z"), TestCase("201-07-14T00:00:00.000Z")] public void PostAPI_SpecialCaseRequestDate_WithError_ProceedsSuccessfully(string date) { // arrange SpecialCaseRequest data = new SpecialCaseRequest() { Name = "Test", ExpirationDateTime = date, }; // act + assert string json = JsonConvert.SerializeObject(data, Formatting.Indented); PostMethods sendJsonDemo = new PostMethods(); var response = sendJsonDemo.SendJsonDemo(json); var responseBody = sendJsonDemo.GetResponseJsonAsObject<Error>(response); Assert.IsNotNull(responseBody); Assert.AreEqual("Date parameter cannot be empty string", responseBody.Message); Assert.AreEqual("Date parameter cannot be empty null", responseBody.Message); } 

The idea is to test all cases in one method and pass them successfully, but in this case it will fail in the second Assert, because after the validation from the message of the API response body contains this line: "Date parameter cannot be an empty string "and Assert will be passed first, but it will fail in the second Assert, because responseBody.Message from api:" Date parameter cannot be an empty string ". The same would be with the same TestCases as null or "String", etc., because from TestCase(null) it will not first state, because after checking from the response body api another line appears responseBody.Message = "Date parameter cannot be empty null." How can I manage these Asserts beautifully without creating separate test methods for each TestCase?

+6
source share
2 answers

You can pass two parameters for the test. One of them is date , and the second can be expectedError . So your TestCase will look like this:

 [TestCase("", "Error1"), TestCase(null, "Error2"), TestCase("String", "Error3")] [TestCase("2010-07-14T00:00:00.000Z", "Error4")] [TestCase("201-07-14T00:00:00.000Z", "Error5")] 

And you will have only one Assert :

 Assert.AreEqual(expectedError, responseBody.Message); 
+2
source

After clarifying that the OP uses NUnit 3.5.0, I think you should take a look at the TestCaseData attribute: https://github.com/nunit/docs/wiki/TestCaseData

It seems to provide an even better way to organize code than the solution offered by @DovydasSopa, although one statement remains unchanged.

0
source

Source: https://habr.com/ru/post/1014489/


All Articles