Testing a Xamarin Block from the Command Line

I come from a native iOS / Android development background and I am trying to understand the tool around Xamarin Unit Testing using the command line.

From my point of view, there are two types of code that you want to use Unit Test:

  • Plain old C # code - without any dependencies on any iOS / Android framework - so no emulator required to run on iOS / Android
  • Code that depends on the iOS / Android frameworks to run on the device / emulator

The official Xamarin documentation mentions NUnitLite / Touch.Unit but does not mention support around the command line. However, I found, but it is not clear to me if this is a tool officially supported by Xamarin. It also seems that you can only run tests on an emulator / device using this tool.

Another example I found relates to xUnit.net - it seems that you can also run tests without an emulator / device, and that you can also run them on an emulator / device - however, in this particular blog, it hasn't documented how you do this do.

So my question is, how do I approach Xamarin Unit Testing and what tools do you recommend using so I can have command line support in my CI.

thank

+3


source to share


2 answers


The most popular unit testing modules used with Xamarin are NUnit and XUnit. They are both similar to JUnit.

  • Typically a Xamarin cross-platform application uses a Portable Class Library (PCL) project that contains platform agnostic (generic) code: business logic, model, view models, service, etc. This code is tested in a separate pcl or .net45 that links to the original project and nunit / xunit.

To run nunit / xunit , you need to run the appropriate test runner and point it to the test build. Both nunit and xundit use console runners that can be parameterized on their command line (see links).

Feel free to choose nunit or xunit. I like them.

  1. You can also have platform unit tests (which depend on the android / ios / uwp sdks) and need to run on the device. These tests can also be built with nunit or xunit and run with nunit device runner or xunit device runner . Basically what's going to be here is you add an android / ios app project to test nunit / junit references, contains your tests and references to your common tests, and can run them on the device.

  2. There is also a coded UI test layer where NUnit, Xamarin UITest and Specflow can be used. I assume this part is outside the scope of your question.

But again, you are coming from Android and used with gradle. Well, Xamarin and .net don't have gradle, but they have Cake . I use it to automate all my projects / tests / ci / deployments etc.



Cake (C # Make) is a cross platform build automation system with C # DSL for performing tasks such as compiling code, copying files / folders, running unit tests, compressing files, and building NuGet packages.

Your cake script might look something like this:

Task("Run-Unit-Tests")
 .IsDependentOn("Build")
 .Does(() =>
 {
     NUnit("./src/**/bin/" + configuration + "/*.Tests.dll");
 });

 Task("Build")
  .Does(() =>
    {
       DotNetBuild("YourAndroid.csproj");
       DotNetBuild("YourCoreTests.csproj");
         ...
    }
  );

      

Cake comes with a bootstrapper file (ps1 - powershell for windows or sh for mac) that loads all the tools needed to run the script (cake itself, nuget, nunit / xunit, etc.).

Your command line / CI can run it like this:

./build.sh -Target Run-Unit-Tests

      

+3


source


Group testing is painful on Xamarin.

You don't have a lot of experience with a mobile Unit Testing project, but if you want to test your app, I would recommend this integration approach:

1) For any calculation type function (you call it "Plain Old C #") use NUnit (mono supported). However, I cannot think of an example of such a code, since large calculations have to be done on the server side. And there you do separate unit tests



2) UITests (NUnit again) can be done to prove that the application is working and interacting with the UI requires behavior. Xamarin also provides TestCloud, where the app can be tested on many devices, but it is paid for by the service. Alternatively, you can set up a build server like Jenkins to do the job for you.

Anyway - this is my take on how this can be done, and I hope it answers your question a little.

+1


source







All Articles