we're going to look at xunit testing in C we create an xunit test project write some unit tests and show you how to use fact and Theory the assert method is an important part of unit testing and we'll go through some of the different assert methods the first step is to create an xunit test project and we'll do that in Visual Studio then Visual Studio 2022 we'll go to create a new project we'll do a search for xunit select xunit test project with c sh we're going to name the project name round the code. xunit give the solution name the same going to select n 8 long-term support and that's created the project for us now that we've created a test project we need something to test to do that we're going to create a class library and write some methods to create the class Library we're going to right click on the solution go to add a new project going to do a search for class Library select the appropriate template I'm going to call the project name round the code xunit do methods select framework. net8 longterm support we're going to rename this class number Helper and we're going to write some methods in it we change the class name we're going to mark it as static and the first method that we're going to write is to check whether a number is an odd number so as part of the parameter we're going to pass in the number and then we'll return the number mod 2 equal 1 that will indicate that it's an odd number I'm going to do the same for an even number now we going to rename the method as is even number we're going to do number B 2 equals z the other method we're going to do is we're going to do it based on a rating score so we're going to call the method rting score pass it in the number again and we're going to do a switch statement so if we say it's under free we're going to return a result of bad if it's equal to three and less than seven we're going to say that it's okay if it's bigger or equal to seven and less than 10 we're going to say great and as a default we're going to return unknown now that we've created some methods we can test each one in our test project we first need to add a dependency to our test project to the class Library so we'll add that then we're going to add a new class and this class is going to be called number helper fact tests this is where we're going to write our tests the first test that we're going to add we're going to call the is odd number method we're going to pass in a value of three as the parameter we expect that to return true so we call assert do true and we import the number helper method from the round the code. xunit do methods call the is number method and pass in the value of three I'm going to do a similar one now so is odd number value is six we expect that to return false so we change the assert method to assert do false and we pass in the parameter as six so that's the method check in for the is odd number we're going to do it for the is even number as well so we're going to rename it is even number Val of three and that should return false we expect the assert to return false so when we pass in number helper do is even number of three that will return false I'm going to do the same for the value of six but this time we expect that to return true because six is an even number we change the assert to True number helper is even number CHS the value to six now in order to test it in Visual Studio we can go to test and test Explorer notice on the right hand side though that we've got no tests in there and the reason being is that we need to apply the fact attribute to each one of [Music] them as we add a fact to each of them we can see on the right hand side that it's updating the test Explorer we now have four tests in there that's run them and see if they all pass they are all now passing we've added our test with one number but what if we wanted to add multiple numbers to the same test to show that we're going to create a new class and we're going to call it number help per Theory tests we're going to copy and paste the codes from the fact tests into the theory tests and I'm going to make some changes so we're going to change the fact attribute to Theory and what we can do with this is that we can pass in a parameter so we're going to pass in the number as the parameter and then up here we can pass in some data for that parameter so we're going to do a check for free so this free will be passed in as a parameter for one of the tests and we can also add multiple tests by adding multiple inline data attributes so we're going to do the test for five I'm going to do the same for seven as well and I'm going to update our tests for each one so for this we're going to pass in two four and six pass in the number parameter and we need to change the number to the parameter that we're passing in and we'll do the same for the two at the bottom as well we're going to pass in 3 five and seven pass in the number as the parameter and then pass it into the method and we'll do the same for the final one we're going to pass pass in 2 4 and six we expect all of them to be an even number that's now created some additional tests and notice for each SE method it contains a separate test let's run them and see if they're going to pass they're all passing for us we checked whether a value is true or false but what about other assert functions let's take a look we're going to do string comparisons on the method that we wrote for rating score so if we go into number helper fact test first test we're going to do is we're going to check a rating score of seven so rating score underscore value of seven we expect that to to return great so we can call assert do equal we expect it to equal great and we pass it number hel per rating score as the actual result need to mark it as fact otherwise it won't appear as a test and we can do the same value of seven but we don't expect it to equal bad so it does not equal bad we can then change the assert from equal to not equal pass in the parameter as bad and we can keep the number helper the same our tests are now passing for that we can also do that with the theory tests so we're going to write a test we're going to call it rating score underscore values we're going to check the correct rating for each one so for this test we're going to pass in two parameters we're going to pass in a number and we're also going to pass in the rating and we can call assert equal we're going to expect the rating so that's going to be the rating description and we'll call the number helper rating score method passing in the number parameter we need to mark it as Theory and we can pass in some data so we're going to do a couple of tests so two we expect that to be bad for a rating score of five we expect that to be okay and for a rating score of nine we expect that to be great they're all pass in for us there are a number of other assert methods that you can use in your unit test just call the assert method and have a look through the different methods that you can use for each test if a test isn't working as it should be how do we debug let's find out we're going to debug this test in the number helper Theory test we're going to place a break point and we're going to do a search for that test so we can see the number Theory test is OD number what we can do is we can right click and go to debug so when we debug it's going to stop at any break point we can see what parameter is being passed in we can see the parameter's been passed in of seven there another one of five and another another one of three so that's how you debug a test so that's a tutorial on how to use xunit watch this video next to learn more about xunit it's important to know much about xunit and test your code thoroughly to stop bugs creeping up inside your project