5
votes

I'm using Visual Studio 2017 to develop Xamarin Android application. I want to add simple unit tests of logic only. For that purpose, I tried adding "Unit Test App (Android)" or "Class Library (Android)" projects with unit tests and none of them work.

Unit Test App (Android)

I add new project to my solution of type "Unit Test App (Android)". Generated project contains TestsSample class with sample tests, but I have no idea how to launch them. I have ReSharper installed, but when right clicking on the project/class I don't have option to run the tests. When I go to "Test" -> "Run" -> "All tests", the solution is built, but nothing more happens.

Class Library (Android) with nUnit

The other trial was to add new project of type "Class Library (Android)" to the solution. After, I installed nUnit nuget package (install-package nunit which installed package 'nunit.3.6.1' with respect to project 'MoneyBack.Tests', targeting 'MonoAndroid,Version=v6.0'), installation was successful.

I added the following class:

using NUnit.Framework;

namespace MoneyBack.Tests
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void SampleTest()
        {
            Assert.IsTrue(true);
        }
    }
}

Now when right-clicking on the tests project I see ReSharper's option "Run Unit Tests", so I clicked it and got the following exception: TargetInvocationException

Am I doing something wrong ? Or it's the issue with VS2017 ? I don't know if I should install VS2015 back or there is some solution. Thanks for your help!

3
A Unit Test App (Android) project needs to be run on a device/emulator as it requires the Xamarin.Android framework to run. Also NUnit 2.6.x is required. - SushiHangover
Please copy and paste the exception here rather than posting a screen shot. - Code-Apprentice
I can't copy-paste this exception, it's a window in which there is no TextBox... @SushiHangover even though it requires emulator, it should launch it, not throw exception, right ? BTW, I only want to test my platform-independent logic, do I always need to run tests on the device ? Even if yes, I cannot do it.. - Dawid Sibiński
@DawidSibinski Platform-independent logic tests should go in standard/PCL NUnit/XUnit/... test projects. As far as your Unit Test App (Android) project, set an Android device/emulator target and build/run the application (Resharper does not know anything about Xamarin-specfic device testing). - SushiHangover
@SushiHangover ah, you see, I added "Unit Test Project (.NET Framework)" and it works! Thanks! That's what I was looking for. However, I still cannot launch those tests on the device... - Dawid Sibiński

3 Answers

16
votes

There are three basic levels of testing:

The classic unit testing of pure .Net/Mono code via xUnit|NUnit

  • Nothing new here, this is the same testing that .Net programmers have been doing all long and has nothing to do with the Xamarin platform frameworks

Note: These tests are totally independent of Xamarin.Android|iOS|Mac

On platform testing (including platform features)

Note: There are multiple on device testing wrappers for NUnit, XUnit, etc... Xamarin includes a NUnitLite version that runs on Android and iOS and that provide a device specific UI to run those tests. Xamarin has templates that create a Unit Test App project for Android or iOS.

Note: These tests can include platform dependent features (Networking, Bluetooth, GPS, SMS, etc... but no GUI related tests) and can also reference Nunit [Test]s written in PCL-based assemblies or platform-specific libraries.

UI Testing

A Casabash/Appium/... driven tests of the UI elements in your application and their reaction to input (touch) events.

  • Test Cloud/Mobile Center and/or other local, public or private mobile test clouds

  • Xamarin Test Cloud

1
votes

Test are run from the device itself not directly from the code as we are used to. Do this: 1. set you android project as VS startup project 2. Select the harware or simulator that you want to execute the unit test project 3. Run it 4. The unit test project will be deployed and executed in the device. 5. Tap on the line with the project name. The available tests will appear. Tap each one for execution and debug ! :)

0
votes
  1. You need to install nUnit 2.6.X for using xamarin.UItest.
  2. nUnit 3 is not supported.