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:
- 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 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!
[…] Creating Cake script for building and deploying Xamarin app: Part 1 running unit tests […]
[…] Creating Cake script for building and deploying Xamarin app: Part 1 running unit tests […]