To run NUnit tests on a target device, we use an
Android.App.Instrumentation subclass, which can be created and executed by using the
adb shell am instrument command.
First, we create the
TestInstrumentation.cs file, which creates a subclass of
Xamarin.Android.NUnitLite.TestSuiteInstrumentation (declared in
TestInstrumentation(IntPtr, JniHandleOwnership) constructor must be provided, and the virtual
AddTests() method must be overridden.
AddTests() controls which tests are actually executed. This file is largely boilerplate.
.csproj must be modified to add
.csproj may be modified to add the
RunTests MSBuild target, which would allow invoking the unit tests as:
msbuild /t:RunTests Project.csproj
Using a new target is not required; the corresponding
adb command may be used instead:
adb shell am instrument -w @PACKAGE_NAME@/app.tests.TestInstrumentation
@PACKAGE_NAME@ as appropriate; it is the value present in
Important Note: With the Xamarin.Android 5.0 release, the default package names for Android Callable Wrappers will be based on the MD5SUM of the assembly-qualified name of the type being exported. This allows the same fully-qualified name to be provided from two different assemblies and not get a packaging error. So make sure that you use the `Name` property on the `Instrumentation` attibute to generate a readable ACW/class name.
The ACW name must be used in the
adb command. Renaming/refactoring the C# class will thus require modifying the
RunTests command to use the correct ACW name.