xUnit v3 is awesome in that it takes advantage of Microsoft Testing Platform. But you're stuck on v2 and need to migrate?
NO SWEAT! It couldn't be easier... AND you can even drop some extra nuget packages when you do it.
View Transcript
All right, you've been using XUnit V2 for the last 20 years or so, and now it's time to move on to XUnit V3. So, what the heck is that? How do we get going? And what does this have to do with Microsoft testing platform? Hi, my name is Nick Coantino and I'm a principal software engineering manager at Microsoft. In a previous video, which I'll link right up here if you haven't seen it already, I walked through some of the new changes in XUnit V3 because I wasn't even aware that there was a V3 until I went looking a little bit harder. So, in this video, I want to talk about how V3 looks with Microsoft testing platform and specifically some of the differences that we see going from V2 to V3. I have plenty of other videos on how you can use XUnit in terms
of writing facts and theories and unit tests for your code. But in this video, we're just going to go through some of the differences in behavior at a high level. So, if that sounds interesting, just a reminder, subscribe to the channel and check out that pin comment for my courses on Dome Train. Let's jump over to Visual Studio and look at XUnit v2 to start with. On my screen right now, I have the system under test. So, you can see in my solution over here, I just have the system under test project. I have a couple of things defined here. These don't really matter. I just want to show you where they're coming from. But what I'm going to be doing in this solution is over time building out things. So, I have a previous video where we did this with MS test and I'm
going to be using what I'm calling legacy XUnit. So, I have the test rate here that I'll open up. And I'm just going to show you in the project file. You can see that I'm referencing XUnit version 2.9.3. One important thing to note is, and I'm going to bring this up later, is that you'll see that I'm including XUnit. This is the name of the package. When we go to look at V3, it's not the same package. And I think for me, this is probably why I missed out on this. And I feel like I'm late to the game, even though it's still pretty earlyish. So, I'm going to go back over to the tests. This is going to be what we have for our tests. And when I pop open the test explorer on the right side of my screen here, you can see
that the test explorer does have them all listed out, right? Nothing too crazy or fancy going on. If you're not totally familiar with some of the XUnit syntax, just a quick note, a fact is a single test. So, this is one test method. And then a theory allows you to parameterize the tests. There's different ways that you can have data for your theories, but this is called inline data where we can just put them right above as individual attributes and that way they get plugged into these parameters. If we think about that, that's three tests right here and a fourth test above. If I go to the test explorer, we have the one and then the one, two, three tests here that are parameterized, right? So, four in total, right? This is the three and we can expand it. What we should be able to
do in theory, no pun intended, is that I can press play on these and they should all go run. But they don't, right? So something's up. What's going on here? We can see them in the test explorer. If I go open up this output and I go to test, I have a bunch of issues going on here, right? It says test discovery or execution might not work for this project. It's recommended to uh reference Nougat test adapters in each test project. Oh, okay. And it can't find the test host. Okay, no problem. So, we're missing a couple things. Let's go back into here. So, I need to make sure that I have a test runner. So, let me go ahead and do that. Go to the Nougat packages here. By the way, this is all planned. I didn't forget this. I just wanted to walk
you through this. So, when we look at XUnit v3, you can see the difference. Let me go get the XUnit runner for Visual Studio installed. That's going to be the test adapter that we have. Now we should in theory I would have expected that we needed that installed to even see the tests up here. But let's go run these now. Cross our fingers that we got everything and still not working. I actually have a video on YouTube for this. It's one of my earliest YouTube videos where I was getting more consistent. But if we check, we still have a problem. If I go to the test output, right, we still cannot find the test host. We need one more thing included. Fortunately, it's right on my screen right here. So to get XUnit v2 running in Visual Studio, we need three Nougat packages. Okay, so
now when I go do this, right, that's a it's a bit of a stretch, but let's go press play. Now they all work. If I check the output, we don't have anything crazy going on. We can see that it was able to discover them and then it was able to run them. So that means just to kind of show you in the project, right? We have three references just to get XUnit to run inside here. Now, one more thing that I want to call out before we switch over to V3 is that when we look up at the top, this is actually just going to be a class library. And if you watch the previous video on MS test, which if you haven't seen already, I will link that up here as well. that one you can see that we can make the test project
actually a console instead. So we have a console application instead of a class library. But traditionally in XUnit v2 it had to be a class library. Now what I want to do is show you in XUnit v3 just how simple and straightforward it is. Okay, I'm just going to go unload this project. Let me save it. I will unload. So now there's no tests. So nothing's discoverable. And that actually took a little while to do. Let me go load up this one. And spoiler alert, don't look at that. Not supposed to see it. But what we can see is I have an executable output type. Okay. So, we have an executable output type. I have one package reference. I'm going to go open up the program.cs. Okay. Um, this technically could have just been renamed to this. Um, anyway, I just copied and pasted the
tests over from the legacy one, right? If this looks very familiar, it's because it's the exact same code. I just dropped it over here. What I'm able to do is go to the test explorer. You can see, okay, just like we saw the first time, it discovered them, but will it run? And yes, they run with a single package reference. Now, this might seem trivial, right? It's it's like, okay, that's probably what I expected, but this didn't work before in V2 of XUnit. If you're working in sort of the workplace and your company has not migrated over to using the central package management, that would mean that across your different projects, you would have all of these package references. You might have different versions of like, you know, xunit for the v2 one. You'd have the runner, you'd have the testing SDK. These would be
polluted everywhere. And even if you do have central package management, you need three references. Now, it's only the single one. Just a reminder, I said I was going to call it out again, but see how this says include xunit.v3. It used to look like that for v2. So, it is a different package altogether, but it is just one. Okay. The really cool part is that when you go to upgrade, just to explain it briefly in case it's not super obvious, right? Let me pull this one back up again. This is what it used to look like. If you wanted to upgrade, this is truly how simple it is. You would get rid of that. You'd get rid of that. You'd add av3 and then you just have to make sure that you got the the version, but your tests would work the exact same way
and they would be discoverable in the testr runner and everything the exact same way. So that's pretty awesome in terms of a migration strategy is that you can literally change your xunit package and then remove two other nougat packages. Pretty cool. As software developers, we love removing code. It's a real good feeling. So, let me go unload that again. I wanted to talk about how we can go run this in a different way. So, most of us, I think if you're developing in Visual Studio, I don't use VS Code that often, so I'm not sure what they have for test runners. I think most people if you're using VS Code, you probably just open up the terminal. But, let me go ahead and go to the developer PowerShell here. Let's get that cleared up. What we should be able to do, don't look at the
TUN one. That's a spoiler for another video. I'm going to change directories into this one right here. So now I'm in here. What I should be able to do because this is a console project is dot net run. Okay, we weren't able to do this historically with xUnit v2 because they were class libraries. But now with v3 and the Microsoft testing platform, this stuff is all baked in, right? So, you have a standalone executable that you can go run your tests. You can see here that it ran for tests. But, uh, I had a little bit of a spoiler that I forgot to delete the comment. And let's go jump back to it because this right here is an attribute that we can use to use the Microsoft testing platform runner instead. And what that means, just to show you, is we see what this
looks like, right? We can see xunit.net v3 in process runner. That's the XUnit v3 runner. But we can switch over if you want to be consistent with Microsoft testing platform. If we do net run now, this output will be consistent with other Microsoft testing platform output. So let's go run this. And we can see that we get this other runner instead, right? Microsoft testing platform runner. We have color coding like it's split out differently. If you are building other things with Microsoft testing platform and you want to be more consistent with that, then you can use this simple attribute to be able to turn that on. Overall, that's mostly all that I wanted to cover in this video. But the highlevel things that I wanted to draw attention to are that it's fewer Nougat packages. It is a different Nougat package, not just a new
version of the old one. The migration strategy is awesome because it's for me at least in terms of the features that I use across the board. I had I don't know thousands of tests in brand ghost which is something else I'm building and I was able to essentially just take out the v2 reference replace it with v3 and then remove the testr runner and SDK projects. Like I removed those nougat packages and swapped it for v3 and everything just worked. Super cool, super easy to migrate, and that means that I can start taking advantage of more of the Microsoft testing platform features. That's all for this one. If you thought that was helpful, let me know. If there's more features that you want to see in XUnit V3, leave a comment below. I'm happy to try answering them, putting videos together on them, because there's
new features in V3 if you watch the previous video that I will be exploring for myself. Now, with that said, the follow-up video to this, I had a spoiler in this video. It will be TUnit. I will be looking at TUnit next in terms of how that works and Microsoft testing platform. So, thanks so much for watching and I'll see you in the next one.