DEV Community

sreejithvijayan
sreejithvijayan

Posted on

Unit Testing Xamarin Forms apps in Appcenter

We had been using Microsoft Appcenter extensively as part of our mobile app development using Xamarin forms as it is the best bet for cloud services like build, distribution, testing for mobile apps. Also it provides diagnostic services like Crash report, analytics out of the box which helps in effective troubleshooting.

The one thing which was missing in Appcenter, or we thought it was until we found out was the inclusion of unit test cases in the build pipeline. We know that it's very much possible in Azure devops CI/CD pipeline by simply adding some code to your YAML file but since Appcenter uses more of a pre-configured pipeline with no or very less work to do for the developer, it was difficult to figure out. This search ended with the build scripts which can be configured as part of Appcenter builds.

Let's have a look at what build scripts are and how to configure the same in your Appcenter builds.

Build Scripts

Appcenter, by default supports 3 type of build scripts which runs at different stages of the pipeline. - post-clone, pre-build, post-build.

post-clone : runs immediately after the repo is cloned. Ideal place for
installing any extra nuget packages required.

pre-build : runs before the actual build starts. Ideal place for running unit
test cases or updating any app-specific constants like API url etc.

post-build : runs after the build has finished and copied all the necessary
artifacts to the output directory. The post-build script will run
even if the build fails. Ideal place for publishing the output apk
or ipa file to any cloud server or any other repo other than
Appcenter.

Adding these scripts in your solution is simple and is explained in the below link -

https://docs.microsoft.com/en-us/appcenter/build/custom/scripts/

How we did it??

Coming back to our scenario, We have xunit test cases written as part of our project and we wanted to ensure that all the test cases are passing before generating build artifacts, so the ideal place to run the test cases would be in the pre-build script.( Infact its a debatable topic, whether to run test cases in post-build vs pre-build. In post-build script, it didn't make any sense to me as why do you need an artifact to be built and copied to output directory and then coming to know that your unit tests are failing. Anyways, its your choice on where to put it.)

Below are the steps for configuring unit tests as part of appcenter pre-build.

  • In your Xamarin Forms solution, inside your Droid or iOS project, add a New File named "appcenter-pre-build.sh" at project root level.

  • Replace the contents of the file with below text

#!/usr/bin/env bash

echo "Running a search for XUnit test projects:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.UnitTests..csproj' -exec echo {} \;
echo

echo "Running XUnit tests:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.UnitTests..csproj' | xargs
dotnet test --logger "trx;LogFileName=unitestresults.trx";
echo

echo "Reading XUnit tests result"
testResultPath=$(find $APPCENTER_SOURCE_DIRECTORY -name 'unitestresults.trx')
echo

grep ' [FAIL]' $testResultPath
failures=$(grep -o ' [FAIL]' $testResultPath | wc -l)

if [[ $failures -eq 0 ]]
then
echo "Unit Tests passed"
else
echo "Unit Tests failed"
exit 1
fi 
  • Push this new file to your branch.

  • Go to Appcenter portal, navigate to -> Build-> Branches->
    go to your branch where the .sh file is pushed and go to build settings. Make sure your pre build script is reflecting in the build scripts section as shown below.

Alt Text

  • Click on "Save and Build" for the changes to take effect.

  • Now check the build logs to ensure that your test cases are running as part of pre build.

We are currently exploring on how to capture code coverage also as part of the build pipeline. Will definitely be posting it here once we find a solution for this as well.

On the whole, the capability to add pretty much anything as part of build scripts, Microsoft Appcenter would be the ideal and easiest service for configuring devops lifecycle for a mobile application.

If you have any questions or feedback - feel free to reach out at @sreejithvijayan - love to hear from you!

Top comments (0)