Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Assert test with different cases on one method and all of them pass when comparing with different response cases?

is it possible to test 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 am getting response from API with deserialized json to my Error object I assume that Response Body Message and HttpStatusCode is correct after different VALIDATIONS from API and it will be correct in from my assert.

Method where I am deserializing from Json to 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 test 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);
        }

Idea is to test all cases in one method and pass them successfully, but in this case it will fail on second Assert, because after validation from API response body message have this string: "Date parameter cannot be empty string" and first Assert will be passed, but it will fail on second Assert, because responseBody.Message from api is : "Date parameter cannot be empty string". Same would be with othe TestCases such as null or "String" and etc, because from TestCase(null) , it will fail on first assert because after validation from api response body comes with different string responseBody.Message = "Date parameter cannot be empty null" . How can I manage these Asserts nicely without creating separate test methods for each TestCase ?

like image 508
BinaryTie Avatar asked Oct 29 '22 13:10

BinaryTie


1 Answers

You can pass two parameters for test. One is date and the second can be expectedError. This way your TestCases would look like:

[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 just a single Assert:

Assert.AreEqual(expectedError, responseBody.Message);
like image 144
Dovydas Šopa Avatar answered Nov 02 '22 09:11

Dovydas Šopa