What's the preferred way to unit test Xamarin.Forms?

ReedReed Member ✭✭
edited April 8 in Xamarin.Forms

I see two possible ways to do it:
1. One of the way is to abstract Xamarin.Forms classes, like displayed in this blog https://intellitect.com/unit-testing-xamarin-forms
2. Other way is to use https://github.com/jonathanpeppers/Xamarin.Forms.Mocks NuGet package

In my case, I need to mock Device.BeginInvokeOnMainThread and Device.Idiom, so maybe introducing the whole NuGet package is not worth it?

I would like to hear what way do you prefer.


  • JohnHardmanJohnHardman GBUniversity mod

    @Reed said:
    I would like to hear what way do you prefer.

    Xamarin.Forms APIs were clearly not designed with automated testing in mind. A lack of interfaces, use of static methods, UI objects without AutomationId properties etc., all make test automation harder work than it should be.

    In my own code, I have wrapped most (all?) Xamarin.Forms classes that I use, as per (1) above. As well as improving testability, this also gives me a central point to add workarounds for Xamarin.Forms bugs as/when they appear. I have similar wrappers around third-party plugins APIs as well.

Sign In or Register to comment.