Does anyone have an example unit test project? #14847
-
There isn't too much info online about how to write unit tests directly for .NET MAUI when using the single-project style. I know that you could try to move most of your code into a .NET Standard project and then test that way. However, is there any easy way to test a .NET MAUI project itself? In order to do so, the test project would need the same target framework as the MAUI project. I was able to add a regular net7.0 target, alongside the native targets, and then test that way. But it had caused a lot of glitches with Visual Studio. Can't even run and debug the app via Visual Studio after doing that. Works that way when running from the CLI, but I like using VS. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 10 replies
-
You can't currently run unit tests against .NET Maui app projects, but you can run unit tests against .NET Maui library projects. It is a flaw in the design and I believe they are looking to correct that in .NET 8. |
Beta Was this translation helpful? Give feedback.
-
Whenever I see people testing their software, I am confused because if you understand 1 + 1 = 2 and you wrote it in code why are you testing whether if it's true, if you are testing you didn't write 1 + 2, that means testing actually is unnecessary as long as you are so good at programming. |
Beta Was this translation helpful? Give feedback.
-
@symbiogenesis - Can you say more about this - about the problems you saw doing this?
This worked for me, in my very simple test case. And it's what's suggested here: https://cedricgabrang.medium.com/adding-xunit-test-to-your-net-maui-project-ee36c00a8542. I'm not suggesting this is the ideal solution - and #3552 in .NET8 will help. But I'd like to understand better what breaks with this today for you in VS. There might a tooling bug we should investigate, for instance. Thx. |
Beta Was this translation helpful? Give feedback.
-
Hi there!! |
Beta Was this translation helpful? Give feedback.
-
Suggested workaround doesn't work, after tweaking a plugin and some platform code, you can't deploy to device or sim to debug although everything compiles ok. ![]() |
Beta Was this translation helpful? Give feedback.
-
There is now: https://learn.microsoft.com/en-us/dotnet/architecture/maui/unit-testing and it seems to allow directly testing .NET MAUI (C#) projects. |
Beta Was this translation helpful? Give feedback.
#3552
It's closed against .NET 8 apparently. Don't think it has been back ported to .NET 7 (you know, the current production release we are all using, thanks MS...)