Run NUnit tests in Appcenter?

Viewed 738

I have a Xamarin project that I regularly push to Appcenter, where it's build. That works fine, but now I added NUnit 3 tests to my solution and while they execute fine locally, they seem not to be executed during Appcenter build.

How do I configure my solution so that my test project is executed on Appcenter? Seems like one has to combine it with Xamarin.UITest, but I don't really understand what steps are necessary for that. Note that my NUnit tests are not UI tests, they are normal unit tests.

Update: Quote from https://devblogs.microsoft.com/appcenter/faster-android-tests-and-nunit-3/

Now you can update your NUnit package along with UITest to the latest versions and run tests both locally and in App Center,

So I would expect NUnit tests to be run during App Center build without an additional script. Ca anyone shed some light on this? Adding a bounty now.

2 Answers

The easiest way I found to have AppCenter run a project's NUnit tests was by adding a post-build script for each app and install NunitXml.TestLogger Nuget package into your NUnit project, which will output an Xml file of your test results.

To create a post-build script it needs to be in the root directory of the Android/iOS .csproj and named appcenter-post-build.sh. Then your script should look something like this:

#Android post build script
#Make sure the directly to the NUnit csproj is correct
ProjectPath="$APPCENTER_SOURCE_DIRECTORY\YourProject.NUnit\YourProject.NUnit.csproj"
echo "$ProjectPath"
#To generate the xml file it requires nuget NunitXml.TestLogger
dotnet test "$APPCENTER_SOURCE_DIRECTORY" --logger:"nunit;LogFilePath=TestResults.xml"
echo "NUnit tests result:"
pathOfTestResults=$(find $APPCENTER_SOURCE_DIRECTORY -name 'TestResults.xml')
cat $pathOfTestResults
echo

#Looks for a failing test and causes the build to fail if found
grep -q 'result="Failed"' $pathOfTestResults

if [[ $? -eq 0 ]]
then 
echo "A test Failed" 
exit 1
else 
echo "All tests passed" 
fi

The last part should cause your AppCenter build to fail if a test does. Also, you may need to try building it twice in AppCenter before it picks up that a post build script has been added to your repo.

Related