Creating Cake script for building and deploying Xamarin app: Part 1 running unit tests

Cake allows to write C# scripts for compiling code, running unit tests, copying files/folders, etc. There is built-in support for many tools and many more via addins. In these post series I will show how to create Cake script that can: run unit tests, build Xamarin app, deploy app to App Center, deploy app to Google Play Internal, deploy app to Test Flight, run UI tests. The script can be used in every Xamarin app. I will also show how to create Azure Pipeline that use Cake script.

Introduction

Cake allows you to write C# scripts for compiling code, running unit tests, copying files/folders, etc. There is a built-in support for many tools and many more via addins.

In these post series, I will show you how to create Cake script which can:

  1. Run unit tests
  2. Build Xamarin app
  3. Deploy app to App Center
  4. Deploy app to Google Play Internal
  5. Deploy app to Test Flight
  6. Run UI tests

The script can be used in every Xamarin app. I will also show you how to create Azure Pipeline that uses Cake script.

Before we get into details, you need to have a basic knowledge of Cake: https://cakebuild.net/docs

Why use Cake?

At first, we should consider why are we using scripts at all:

  • time saving 
    • if you are often doing the same task in the same way you should consider automation. Such tasks that are done in the same way are best candidates for automation
  • less errors
    • doing tasks manually can lead to errors. It’s easy to make a mistake that way – we are only humans after all. Script will always run in the same way
  • repeatable
    • by doing tasks manually, we can omit some important steps in the process. Script will always run every needed step
  • versioned
    • scripts can be versioned in source control system, so there is a full history for reference

Now let’s consider why to use Cake:

  • cross-platform
    • Cake scripts can run on Windows, macOS and Linux. Scripts will behave in the same way on every OS and CI system
    • developers across team can work on different OS
  • CI pipeline in script
    • Cake script can be run in the same way on different CI systems like Azure Pipelines, GitHub Actions, TeamCity, Jenkins
    • if CI system stops working, Cake script can be executed on a local machine in the same way. Every developer in the team can run Cake script on his machine
    • script can be first written on local machine and later used on CI. Using script on CI is just a matter of executing it

Running unit tests

First thing we should clean obj and bin directories before running unit tests and building Xamarin app:

Task("Clean")
  .Does(() =>
{
  CleanDirectories("**/bin");
  CleanDirectories("**/obj");
});

Next, we need to restore packages for solution:

Task("Restore")
  .Does(() =>
{
  NuGetRestore(PATH_TO_SOLUTION);
});

After that, we can run unit tests:

Task("RunUnitTests")
  .IsDependentOn("Clean")
  .IsDependentOn("Restore")
  .Does(() =>
  {
     var settings = new DotNetCoreTestSettings
     {
         Configuration = "Release",
         ArgumentCustomization = args => args.Append("--logger trx")
     };

      DotNetCoreTest(PATH_TO_UNIT_TESTS_PROJECT, settings);
  });

By default dotnet test won’t publish test results. We need to pass argument --logger with value trx, so test results will be saved to *.trx file (Visual Studio Test Result).

Full script

var target = Argument("target", (string)null);

// General
const string PATH_TO_SOLUTION = "TastyFormsApp.sln";
const string PATH_TO_UNIT_TESTS_PROJECT = "TastyFormsApp.Tests/TastyFormsApp.Tests.csproj";

//====================================================================
// Cleans all bin and obj folders.

Task("Clean")
  .Does(() =>
{
  CleanDirectories("**/bin");
  CleanDirectories("**/obj");
});

//====================================================================
// Restores NuGet packages for solution.

Task("Restore")
  .Does(() =>
{
  NuGetRestore(PATH_TO_SOLUTION);
});

//====================================================================
// Run unit tests

Task("RunUnitTests")
  .IsDependentOn("Clean")
  .IsDependentOn("Restore")
  .Does(() =>
  {
     var settings = new DotNetCoreTestSettings
     {
         Configuration = "Release",
         ArgumentCustomization = args=>args.Append("--logger trx")
     };

      DotNetCoreTest(PATH_TO_UNIT_TESTS_PROJECT, settings);
  });

//====================================================================

RunTarget(target);

Running the script

To run the script, navigate to project directory and execute following command in console:
dotnet cake --target=RunUnitTests

If you need more detailed output, you can pass argument --verbosity with value diagnostic:

dotnet cake --target=RunUnitTests --verbosity=diagnostic

Summary

The script with sample Xamarin.Forms app project is already available on my GitHub. In the next post I will show you how to build Xamarin app. Stay tuned!

  1. […] Creating Cake script for building and deploying Xamarin app: Part 1 running unit tests […]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *