Unit Testing .NET MAUI Apps with xUnit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
You are testing your apps, right? Because testing is important. So in this video, we're going to learn how to use xUnit together with your .NET MAUI application. I have seen a couple of people who have been wondering like, how do I test my, well, unit test application? So they added this xUnit test project to their solution for .NET MAUI. But then they run into trouble with all kinds of weird error messages that things cannot be run or the test doesn't run or it doesn't even build at all. So, to show you how to actually do this, I created this little video to show you how to add a xUnit project to your .NET MAUI solution so that you can test code that is actually in your .NET MAUI application. So let's just switch over to Visual Studio and show you how it's done. Here we are in Visual Studio 2022. I've created a file new .NET MAUI application. You can see it here running on Windows. But in this case, the application itself is not really that important because I'm not going to show you how to actually do the tests, implement all your business logic and actually test that. I'm just going to show you how to add the unit test project in here and then it's up to you to write the actual tests. So let's close down this and here in our solution, we just have this single project, which is our .NET MAUI application. And then people would start adding their unit test project and actually start testing code in their .NET MAUI application. That seems like a good approach, right? So let's do that. Let's go to our solution. Click Add, New Project and here we can search for xUnit. So there is different unit test frameworks out there. xUnit is fairly popular right now and you can run this on Windows, Linux and macOS. So that seems like a logical choice if you want to have this cross platform application, right? So let's do that. Let's click next, I'm going to name this UnitTestProject. Doesn't really matter what the name is, it probably makes more sense for you if you call it something different. The framework that should probably match whatever is in your .NET MAUI project. For now the only option is .NET 6, but in the future it will be .NET 7, 6, et cetera, et cetera. So choose the right one. So we're going to create that. It will think a little bit and it will create our project with a little scaffolding code here. Unit test one, fact test one, et cetera, et cetera. Now, this unit test project should build. It should just run. In fact, this test should succeed because it doesn't do anything. So whenever I right click here and I do run tests, it will bring up the Test Explorer. I think it's going to build these little application because this is just how unit testing works. It's a DLL and it gets picked up by the framework with these attributes and it will start running through all these tests. You can already see the little thing here. Test has passed. Show Test Explorer. Here we are. And this test has passed. So that's great. But what we didn't do is add a dependency, add a reference to our .NET MAUI application. So go here to dependencies in your unit test project. We're going to right click add project reference. And here we have our MauixUnitTestSample, which is the name of our .NET MAUI application. So let's check that box. And now it made a reference to the .NET MAUI application. And now we can actually reference code from our .NET MAUI application because there's probably services in there or your business logic and whatnot that you actually want to test from there, right? So now you can create all references from all the objects in that project and you can actually start testing. But whenever you want to do the same thing now and you say, hey, here, I want to run all the tests, then it's going to build again. And you can immediately see like, hey, whoa, this doesn't work, it's not compatible. This is not even going to build. So how to fix this? Well, what's causing this actually is because .NET MAUI, well, we don't even need to right click. We just click once on the csproj here. Or you can right click and do Edit... I think you have to unload it. Oh no, here it is. Edit Project File. You will get this view and you can see these target frameworks, which is Android iOS Mac Catalyst, maybe Windows if you're on here. But if we'll do the same thing in our unit test project here, you can see that the target framework is net6.0 so the targets don't align. And that's something that needs to be fixed. So let's also add a net6.0 target to our .NET MAUI application. We can do that very easily by just adding net-6.0 here and a little semicolon. And now we've added the net6 target. But the problem is that we also need to output this as a DLL, as a library. But here the output type says exe, right? That is needed for all the other targets in here, Android, iOS, Mac Catalyst. To be very honest, I don't really know why that is. Probably for historical reasons, but it needs to be there. And we can kind of easily fix this as well. If you know a little bit about the build engine, I think we can scroll down here and we can actually take some inspiration from down here a little bit up. We have this condition right here. And now what we want to do is only do this output type to an EXE whenever the condition is not net-6.0 because the default value for this is Library and otherwise we're going to do this in Android. Actually, let me just copy this from here. I have this prepared. So for this OutputType, on every node you can do this condition. And whenever the TargetFramework, that's a variable is not net-6.0, so it's not this one, then we're going to do this EXE. So for all the target frameworks in here, as long as it's not that one, it's going to be EXE. And the default value of output type is Library. So whenever we have this net-6.0, this is not going to be included. It's going to fall back to the default value of Library and we're good to go. Actually, I think this is all we need to do to actually make this build again. I think this might still cause an error because you have to kind of like unload and reload your projects. At least that's what I ran into earlier. It still didn't really build until, I don't know, fiddled a little bit. So maybe to be very sure, just close your Visual Studio after you do this, open it up again and then everything will reload properly. Although this seems to go quite smoothly, so it seems to maybe be good. But this should allow you to automatically start building your unit tests actually, write your unit test. You can write them now and it should build and actually complete. But what you also might want to do at some point is actually access .NET MAUI APIs from your unit test. So maybe you want to access, I don't know, some APIs, some controls that are in .NET MAUI that need to be unit tested. I don't know, you don't want to unit test our stuff, we already do that. But there might be some API that you need to check things against. So if that's the case, go back to your xUnit test project and what you can then do is basically say, <UseMaui> and set this to true and now it will pull in the .NET MAUI library and it will allow you to actually also access all the .NET MAUI APIs. You can use those in your unit test project as well, if that's what you need. Like I said, this doesn't really build. I don't know what I did last time to actually make it work, but you probably want to unload and do all the things. Let me try to run this thing here again while I talk you through the rest. But if you unload and reload or maybe restart your visual studio, then it really works. Pinky promise. See, here we are. I think the build actually succeeded right now. So it's actually running the test and the test succeeds. See? So now after a little bit of fiddling here, it actually works. And this is how you can add a unit test project to your .NET MAUI application. Now this allows you to add C unit testing project to your net Maui solution. Another way around if you actually want to use those cross platform APIs or you actually want to run it on a device, because in some scenarios there is definitely a difference in behavior, whenever you run it on an actual device, you also have device runners. And to run that on Android and iOS and run your unit tests actually on the device, that is not what this does. This is purely for basically your .NET only code to allow you to unit test that right from your .NET MAUI application. So I hope this was meaningful to you. The source code attached to this, just so you know how to actually repeat this, it's in the GitHub repository that is down in the video description below. I've added little comments that are marked with xUnit inside of the csproj. So you can easily know what you actually have to do. If you like this video, please click the like button, which will spread to more people. And all those people can actually start testing because testing is important people. And of course if you want to know more about .NET MAUI, be sure to check out this playlist right here. There is a recommended video right here. Please click this button so you're subscribed to my channel and I'll be seeing you for the next one.
Info
Channel: Gerald Versluis
Views: 10,009
Rating: undefined out of 5
Keywords: unit testing, .net unit testing, c# unit testing, unit test, dotnet maui, unit testing c#, .net maui, dotnet maui tutorial, unit testing tutorial .net maui, xunit, .net maui xunit, dotnet maui xunit, dotnet maui unit testing, .net maui unit testing, Unit Testing .NET MAUI App with xUnit, .net 6, .net 6 maui, xunit maui
Id: C9vIDLQwc7M
Channel Id: undefined
Length: 9min 7sec (547 seconds)
Published: Mon Jul 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.