Title: Parameterize another check with TestCaseData
Time: 15-30 minutes
Purpose: To practice using TestCaseData, and explore other ideas for ways to parameterize our checks
Introduction: At the end of Lesson 9 I challenged you to parameterize another check that youâve already written.
Activity:
Choose a check youâve already written to parameterize
Determine what scenarios to check (use our automation plan or come up with your own)
Some ideas:
Modify the GET a single TodoItem check to check for a non-existent item also (with a NotFound response), and/or send a non-numeric character instead of the ID in the URL
Modify the POST check to check response codes if you send a TodoItem without a Name value (like we did with the PUT)
Some hints:
All of your test data can live in the same class - you can add another IEnumerable of test data to the same TestDataClass
First all , thanks for providing this wonderful course.
I have found an issue, while implementing the delete method.
It returns MethodNotAllowed, instead of NoContent. Not sure what might cause this? could you please explain it? Thanks
Even a test as simple as get by id can be parameterized:
public static IEnumerable GetByIdTestData
{
get
{
yield return new TestCaseData(1).Returns("OK").SetName("valid id");
yield return new TestCaseData(2333).Returns("NotFound").SetName("nonexistent id");
// this one should be tested differently (if we want it at all)
yield return new TestCaseData("cda6932c-540e-401f-853c-da3f2feb6ea2").Returns("BadRequest").SetName("id not an integer");
}
}
Lessons learned:
parameterized tests are rigid (see above). Only parameters of the same data type.
donât call all happy path tests âhappy pathâ, rather have something more descriptive: âvalid idâ.
public static IEnumerable GetByIdTestData
{
get
{
yield return new TestCaseData(1).Returns(HttpStatusCode.OK).SetName("valid id");
yield return new TestCaseData(2333).Returns(HttpStatusCode.NotFound).SetName("nonexistent id");
}
}
Hello, in the Lesson 9 parameterised check you changed the //Assert part from an Assert.AreEqual style-check which takes in expected and actual response codes âŠ
⊠to a âreturnâ of only the response status code, and the PutTestData is returning the expected value per scenario - here Iâm struggling to see which part of the code is now doing the actual check between response vs expected ?
Also, in the parameterised check is it easy to check more than one thing in the same Test? e.g. checking the properties / values coming back and the status code in a single tets - so if any one of those fails, the single Test fails. (Maybe I can have a go at this when I better understand the construction of the test )
Great question @simienrigler!
The part of the code thatâs doing the actual check is within the TestCaseData - hereâs some information from NUnit on that TestCaseSource | NUnit Docs.
I havenât tried checking multiple items, but that link should be able to help you along if youâd like to try it out!