Search code examples
xamarin.formsmoqxamarin.uitest

Xamarin.Forms UI Test Environment


I am trying to set up my Xamarin.Forms application to use UI Tests. Currently the tests are working fine, but I would like to be able to mock or handle the API calls that the application calls, rather than the actual API calls being executed in the tests.

There appears to be a way that UITest can detect if it is running in Test Cloud, but I can't seem to find a way for the application to know if it is running tests locally. I am using an IoC Container to register the various interfaces that interact with these APIs, and would like the App constructor to be able to detect if it is running a UITest, then register the appropriate 'actual' interface instances or the 'mock' instances. Is there a known way to handle this?


Solution

  • Your issue can be solved in many ways, but this is what I actually do:

    You can create a dedicated compiler configuration:

    enter image description here

    Then, based on the configuration you would manipulate your container boostrap pointing your interfaces to the mock objects.

    Whenever you want to run UI tests you would compile this configuration instead of the release configuration.