Unit Testing with Android Unit Test Project in Xamarin.Forms

nasdannasdan Daniel SanchezESMember

Hi all,

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

I created an Android Unit Test Project:

image

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

image

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

image

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

Posts

  • JesseJohnstonJesseJohnston Jesse Johnston 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 Dog Chalmers 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 Dog Chalmers 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 Jonathan Pryor 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 ben ishiyama-levy SGBeta ✭✭

    Hi,

    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.