xUnit.net 2.0 supports both Portable Class Library (PCL) and platform specific projects for iOS and Android.
Unit tests for Xamarin have two main components, which may reside in the same assembly. This post will show you how to get started.
- Xamarin Studio 5.0
- Visual Studio 2013 Update 2 with the latest Xamarin for Visual Studio
Xamarin support consists of two logical components, which may be in the same assembly or may be split:
1. Assemblies containing tests. This may be either a Xamarin project or a PCL. This assembly contains your test classes.
2. App for running tests on a device or simulator. This bootstraps the tests. It's important that you set any permissions, such as internet access, geo locations, notifications, etc, if your app requires it.
For simplicity here, we'll put both pieces in the same project, but you can also put your tests in a separate library and reference it in your runner app. We'll use Android as an example, but it's exactly the same for iOS.
- Create a new blank Android project:
Add the NuGet references for
xunit.runner.xamarin. Make sure the Prerelease option is enabled as xUnit 2.0 is still in beta.
After installing the package, you'll see a file MainActivity.cs.txt (on Android or AppDelegate.cs.txt for iOS). Copy/paste the contents of that file into your real MainActivity.cs file. Congratulations, your runner app is now ready, you just need to add some tests!
Create a new test class and start creating your tests. For more info on xUnit, check out the Getting Started page.
Current status on the Runners
The runners today are functional, but they're not pretty. The iOS and Android runners do share quite a bit of code, but the UI is duplicated as it was created before Xamarin.Forms was announced. There's a ton of room for improvement and I welcome any help in the form of a Pull Request. The code is all on GitHub here.