{"id":9781,"date":"2022-12-08T10:25:27","date_gmt":"2022-12-08T10:25:27","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9781"},"modified":"2022-12-10T17:57:05","modified_gmt":"2022-12-10T17:57:05","slug":"unit-testing-your-maui-project","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/unit-testing-your-maui-project\/","title":{"rendered":"Unit testing your MAUI project"},"content":{"rendered":"<p><em>Note: I found this didn&#8217;t work correctly on Visual Studio for Mac, I&#8217;ll update the post further if I do get it working.<\/em><\/p>\n<p><em>This post is pretty much a duplicate of <a href=\"https:\/\/cedricgabrang.medium.com\/adding-xunit-test-to-your-net-maui-project-ee36c00a8542\" rel=\"noopener\" target=\"_blank\">Adding xUnit Test to your .NET MAUI Project<\/a> but just simplified for me to quickly repeat the steps which allow me to write unit tests against my MAUI project code.<\/em><\/p>\n<p>So, you want to unit test some code in your MAUI project. It&#8217;s not quite as simple as just creating a test project then referencing the MAUI project. Here are the steps to create an NUnit test project with .NET 7 (as my MAUI project has been updated to .NET 7).<\/p>\n<ul>\n<li>Add a new NUnit Test Project to the solution via right mouse click on the solution, Add | Project and select NUnit Test Project<\/li>\n<li>Open the MAUI project (csproj) file and prepend the net7.0 to the TargetFrameworks so it looks like this\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;TargetFrameworks&gt;net7.0;net7.0-android;net7.0-ios;net7.0-maccatalyst&lt;\/TargetFrameworks&gt;\r\n<\/pre>\n<\/li>\n<li>Replace <em>&lt;OutputType&gt;Exe&lt;\/OutputType&gt;<\/em> with the following\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;OutputType Condition=&quot;'$(TargetFramework)' != 'net7.0'&quot;&gt;Exe&lt;\/OutputType&gt;\r\n<\/pre>\n<p>Yes, this is <em>TargetFramework<\/em> singular. You may need to reload the project.\n<\/li>\n<li>Now in your unit test project you can reference this MAUI project and write your tests<\/li>\n<\/ul>\n<p>So, why did we carry out these steps?<\/p>\n<p>Our test project was targeting .NET 7.0 but our MAUI project was targeting different platform implementations of the .NET 7 frameworks, i.e those for Android etc. We need the MAUI project to build a .NET 7.0 compatible version hence added the net7.0 to the TargetFrameworks.<\/p>\n<p>The change to add the OutputType ensures that we only build an EXE output for those other frameworks, and therefore for .NET 7.0 we&#8217;ll have a DLL to reference instead in our tests.<\/p>\n<p>Now we can build and run our unit tests.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: I found this didn&#8217;t work correctly on Visual Studio for Mac, I&#8217;ll update the post further if I do get it working. This post is pretty much a duplicate of Adding xUnit Test to your .NET MAUI Project but just simplified for me to quickly repeat the steps which allow me to write unit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[330,30,185],"tags":[],"class_list":["post-9781","post","type-post","status-publish","format-standard","hentry","category-maui","category-nunit","category-unit-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9781"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9781\/revisions"}],"predecessor-version":[{"id":9795,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9781\/revisions\/9795"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}