Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin (Android) Unit Tests in Visual Studio 2017

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!

like image 288
Dawid Sibiński Avatar asked Mar 21 '17 21:03

Dawid Sibiński


2 Answers

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.

  • Xamarin.iOS Unit Testing
  • Xamarin.Mac Unit Testing via GUIUnit
  • Xamarin.Android Setup and Automating
Alternatives to NUnitLite:
  • xUnit.net Runners for Devices
  • NUnit test runners for Xamarin and mobile devices

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

like image 138
SushiHangover Avatar answered Sep 30 '22 04:09

SushiHangover


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 ! :)

like image 38
paketman Avatar answered Sep 30 '22 06:09

paketman