BrandGhost

Master xUnit Like A Pro in Under 10 Minutes!

In this video, we'll be getting started with xUnit. xUnit is a testing tool that helps you write better tests and make your code testable. We'll be using xUnit in conjunction with C# to create simple tests. If you're new to xUnit or are looking to improve your testing skills, this video is for you! By the end of this video, you'll be able to get started with xUnit and create some simple tests using C#. For more videos on programming with detailed examples, check this out: https://www.youtube.c...
View Transcript
in this video we're going to look at getting X units set up to run our unit tests in visual studio so we're going to be starting with a new project looking at the things that you need to add and then walking through a couple of scenarios so that you can understand how to get unit tests set up to run rate in visual studio so with that said let's waste no time and jump right over to visual studio alright so I'm here in visual studio and all that I've done is created a brand new class Library project and just to prove that to you if I go jump over here and we go look at the actual project file we can see what it looks like here nothing fancy I haven't gotten added anything yet this is truly just an empty class project it even has this class 1 file that it dropped in for us automatically from the template so we are starting right from scratch just to explain what we're going to be trying to accomplish if you look on the right side of my window here I do have this text Explorer panel and we want our tests to show up here so so that we can just go ahead and press the Run button and have them all run of course at this point in time we have absolutely no tests so let's figure out how we can get X units set up and then from there I'll label a couple of tests for us to go run and we'll see them show up in this test Explorer so the first thing we'll need to do is actually go to the nuget package manager so I'm just going to go ahead and right click on the project go to manage nuget packages and it will bring up this other window for us to go look at where we can install packages I'm going to go over to the browse Tab and this is where we're going to be able to look for the X unit package that we want to work with so we're going to search for X unit right in here pick the one right at the top that currently has 340 million downloads for me it's going to be version 2.5.0 and we'll go press install you may have a prompt that comes up that asks you to preview the changes and just press ok to that and the dialogues that follow and from there we have X unit installed so the cool thing now is if we go back to our class if this is where we're going to be writing our tests we can actually go label a test and have it show up in our runner in X unit we use the word fact to label a test and if I add the using statement we'll have it come up here you can use private you can use public I generally stick with private and then you would name your tests whatever you'd like the naming convention that I usually use and you can kind of do whatever you would like is that I like to have the method that's being tested something about the scenario that follows in the next part after the underscore and then the last part is that something about the expectation that I have at the end I'm just leaving it with x's for now but we can come back to this later now that we have a test labeled with a fact we should be able to go to our test Explorer and see it show up in the list and behold we have it in this nice layout here where it's organized by our name in space then the class that we're in and then our test name so let's go ahead and see what happens when we try to run this if I go ahead and just ask to run all of the tests it actually says that it's completed but if you look closely it says that there were no tests actually run so zero passed zero failed and zero were skipped although it did say that it finished so what's happening here if we go look at our output panel and then switch it from build over to test this does give us a little bit of a hint so it is saying that it looks like there's a bit of an issue with how we're trying to run our tests so it says that this project does not reference any nuget adapter test Discovery or execution might not work for this project now it's a little bit misleading because if we were just looking at our test Explorer it might not be obvious but there is an error that shows up so if you are paying close attention you would have seen this but I think it's easy for a lot of people to miss so let's go add what's missing we actually need to have an X unit Runner installed as well so let's go get that nuget package if I go back over to here and I say x unit Runner search for that we have it right at the top there's X unit Runner Visual Studio we can go ahead and install this now too awesome okay so let's go try again to run this now that we have this nuget package for the X unit Runner again I will just go run all of these and the same thing happens it says we have zero tests that ran so no pass fail or skipped and again we have an error this time it says that it still can't find the test host but it does have this other uh warning that's gone away right so we don't have this anymore it just says that it couldn't find the test host so there's one more thing that we actually have to go install and this is going to be a total of three things but the last thing that's missing here is that we need the Microsoft test SDK so if I go ahead and install this nuget package as well now that I have all three of these installed if I go back to the test Explorer go run all of my tests you'll see that it's taking a little bit longer it's discovered a test and it's actually said that it's finished running a test if we check our output panel looks like there's no issues here now that's the build tab but if we go to test and we look to the bottom there's actually no issues at all so this did in fact go run successfully but it did require three packages so let's go have one more quick look at what our project file looks like so we can have the list of all the packages needed if you're following Along on your side and you're trying to get X units set up keep in mind that these are the packages that I had to install just to be able to right click and have tests run inside of visual studio so that is we need X unit installed and we will need an X unit runner for visual studio as well so these are two different X unit nuget packages and then finally this is the piece that most people Miss I myself missed this all of the time when I'm setting up new test projects and in fact even before getting ready for this video to go record it I couldn't remember what the name of this package was so I had to go recall it and these are all three things that you will need to install to have your X unit tests run I just wanted to make a quick note about the different types of tests that we can have here and I'm not going to go into detail but you can have a fact to label a test and the other thing that you can have to label a test is called a theory a theory is going to allow you to pass parameters into your test and if I say Boolean here and then you can actually label with attributes where you want that data to come from again I won't go into a ton of detail here I just want to illustrate quickly that we can go have true and false passed in and just to show you what this will look like if I go to the test Explorer now and I expand this you'll see that we now have two tests and it actually shows you printed out the parameters that are passed in there are more complicated things that you can do aside from inline data because that only takes constant values but you can actually go generate more data to pass into your theory another quick note is that I did label this as a private void and I have Theory here I had fact before but your test class must be public so if I go ahead and make this internal because I don't want other assemblies to be able to see this you'll see that it is flagged already and does give us a warning if we check it out and it does tell us it needs to be public so I can leave this as public we are able to seal these if we'd like we can still discover them right in our window so that's just a super quick video to show you how you can get set up with X unit inside of visual studio and a quick recap is that we need three nougat packages one is X unit one is the X unit runner for visual studio and the other one is the Microsoft testing SDK so if you forget what they are you can go back in this video press pause when it's all shown on screen and just make sure that you look at those nuget packages installed we only touched on a couple of the different variations that we can use with X unit so the fact and the theory attributes and then I quickly showed inline data but there's so much more we can do to generate test data and we can look at that in follow-up videos so thanks for watching I hope you found this helpful it's just a quick tip to get you started with X unit so thanks and we'll see you later

Frequently Asked Questions

What are the three NuGet packages I need to install to use xUnit in Visual Studio?

You need to install three NuGet packages: xUnit, xUnit Runner for Visual Studio, and the Microsoft Test SDK.

Why did my tests not run after I labeled them with the 'Fact' attribute?

If your tests didn't run, it's likely because you didn't have the necessary NuGet packages installed, specifically the xUnit Runner and the Microsoft Test SDK. Make sure all three packages are installed.

Can I use the 'Theory' attribute in addition to the 'Fact' attribute for my tests?

Yes, you can use the 'Theory' attribute to pass parameters into your tests, which allows for more dynamic testing scenarios.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload