Add UnitTest to your Xamarin app

Hello,

Is there any guide on how to run Unit Tests in a Xamarin App along with the build process? Specifically, looking for some content here: http://devcenter.bitrise.io/xamarin/add-unit-test-to-your-xamarin-app/

PS: Cross posting from Github issues: (https://github.com/bitrise-io/devcenter/issues/98#issuecomment-270224304)

Thanks

1 Like

Thanks for asking the question here, and welcome to discuss.bitrise.io! :wave: :slight_smile:

I’ll ping the guys who are more experienced with Xamarin, to join us here.

@godreikrisztian can you please have a look?

Hi @venkatacarvana,
thanks for the question!

Currently we do not have step to run xamarin unit tests.

We started to work on unit testing with Touch.Unit framework. The related functionality was developed in xamarin-builder (our DEPRECATED shared xamarin builder tool), but we did not build a step to use it. See the related pull request.

The problem was, that we would need to use an external project to build a Touch.Server.exe, which we can use to run the test automated.

Which testing framwork are you using for unit testing locally?

Hi @godreikrisztian,

I am currently using xUnit to unit test locally

Venkata

@venkatacarvana thanks for the info!

Based the xUnit documentation, it seems you can run the tests with the xunit.console.exe:

xunit.console.exe YOUR_TEST.dll

Before the test you need to restore your nuget packages and build your solution.

To run the test add a Script step with following content input value to your workflow:

#!/bin/bash
set -e 

mono="/Library/Frameworks/Mono.framework/Versions/Current/Commands/mono"
xunit_runner="YOUR_SOLUTION_DIR/packages/xunit.runner.console.YOUR_XUNIT_VERSION//tools/xunit.console.exe"
test_dll="YOUR_XUNIT_PROJECT_DIR/bin/Debug/YOURTEST.dll"

"$mono" "$xunit_runner" "$test_dll"

For example in case of our sample project:

#!/bin/bash
set -e 

mono="/Library/Frameworks/Mono.framework/Versions/Current/Commands/mono"
xunit_runner="$HOME/git/packages/xunit.runner.console.2.1.0/tools/xunit.console.exe"
test_dll="$HOME/git/Multiplatform.xUnit/bin/Debug/Multiplatform.xUnit.dll"

"$mono" "$xunit_runner" "$test_dll"

Notes:

  • $HOME/git is the directory where Git clone step clones your project by default
  • nuget restore can be done by NuGet restore step
  • building your solution can be done by Xamarin archive step or with a Script step with following content:
#!/bin/bash
set -e 

xbuild YOUR_SOLUTION_PATH /target:Build /p:Configuration=Debug

Please let me know how does it work for you!

Hi @godreikrisztian,

We got around to trying this and since all VMs that Bitrise spins up for build are unix based how would we run a console runner that is an exe to run tests? Do correct me if I am wrong and missing something.

Thanks
Venkata

@venkatacarvana the key is this script from @godreikrisztian 's answer:

#!/bin/bash
set -e 

mono="/Library/Frameworks/Mono.framework/Versions/Current/Commands/mono"
xunit_runner="YOUR_SOLUTION_DIR/packages/xunit.runner.console.YOUR_XUNIT_VERSION//tools/xunit.console.exe"
test_dll="YOUR_XUNIT_PROJECT_DIR/bin/Debug/YOURTEST.dll"

"$mono" "$xunit_runner" "$test_dll"

You run the .exe through mono, that’s part of how mono can be used on different platforms (including unix based ones).

The script above basically runs the xunit.console.exe exe through the mono binary - it runs mono and passes the exe as a parameter, similar to how you can run ruby scripts through the ruby runner, e.g. ruby /path/to/my/ruby/script.rb