Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Unit Testing with Android Unit Test Project in Xamarin.Forms

Hi all,

I would like to add unit tests for my business logic.

I created an Android Unit Test Project:


Then, I add the Android Project reference for test my OperationsService


But, I can't create an instance of this service


Someone know why can't I do references between android projects?

I have no problems for Shared or iOS projects. Even I could do reference to shared project in my Android Unit Test Project.

Any idea?

Thanks, Daniel


  • JesseJohnstonJesseJohnston USMember, University

    I am seeing the same issue. I have a iOS, Android, and Shared PCL project. When I create a "iOS Unit Test Project" in Xamarin Studio I can successfully reference classes in my iOS and Shared projects. When I create an "Android Unit Tests Project" in Xamarin studio, I can only reference classes in my Shared project by not my Android project.

    I have added both the Android and Shared projects as references in the Android Unit Test project. But when I build, I get an error saying that I am missing an assembly reference to the Android application project.

    It seems like there might be a dependency issue. When I build the iOS Unit Test project, it first builds the Shared project, then it builds the iOS Application project, and then it builds the iOS Unit Test project. When I build the Android Unit Test project, it first builds the Shared project and then it attempts to build the Android Unit Test project, but it does not build the Android application project.

    The build output looks something like this -

    Building: App_Shared (Release) Build started 1/8/2015 11:57:06 AM. ....... Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.0357120 Building: App_DroidTest (Release) ....... TestsSample.cs(4,7): error CS0234: The type or namespace name "Droid" does not exist in the namespace "App". Are you missing an assembly reference?

    Is anyone else seeing this issue? Is there some way to force the Android Unit Test project to first build the Android Application project?

  • DogChalmersDogChalmers USMember

    Hi Jonathan, I just wanted to clarify. Are you suggesting a solution like this:

    MyProj (Solution)

    • MyProj (PCL)
    • MyProj.Android (default Xamarin project)
    • MyProj.Android.Shared (Android Class Library)
    • MyProj.Android.Test (Android Unit Test App)
    • MyProj.iOS (default Xamarin project)

    Where MyProj.Android contains almost nothing and MyProj.Android.Shared contains everything (Activities/Bundles et all) so that they can be referenced in the Android unit tests?

  • DogChalmersDogChalmers USMember

    Hi @JonathanPryor are you able to clarify? We are new to Xamarin Forms and after two weeks we are still struggling to implement any kind of functioning unit test.

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Are you suggesting a solution like this:

    That's a solution, yes. A variation on that is to use a Shared Project between your MyProj.Android and MyProj.Android.Test projects.

  • XamarinersXamariners SGBeta ✭✭


    A little late on the thread, but I just put out a nuget package 'SpecFlow.Xamarin.Forms' that facilitates unit testing in Xamarin.forms in the console runner (ie for visual studio, team city, etc). It makes it easy to test your ViewModels / Commands/ Internal Navigation.

    In order to keep the test as close to the truth as possible, Xamarin.Forms.Init() is still called by the testing framework, so until we can flag that on/off in the Xamarin.Forms framework, you'll have to catch it.

    You can find the step by step tutorial here.

Sign In or Register to comment.