Last week we decided to start with test-first development using XUnit. To
get underway, let’s create a new Xamarin.Forms application called BlogProjectTracker.
Once the projects are created we’ll return to the solution and add a new .NET project of type Portable Library which we’ll call BlogProjectTracker.Tests.
Finally, we’ll add an empty iOS project which will be used as the test runner, and which we’ll call BlogProjectTracker.Tests.iOS. We can then do the same for Android calling the new project BlogProjectTracker.Tests.Droid
Next we add xUnit to our PCL. Make sure you have the version by James Newkirk and Brad Wilson.
In the Tests.iOS and Tests.Droid projects, add that same xUnit NuGet package,but also add the xUnit runner: Xunit.net for Devices.
Remember to add a reference from the test to your project. Notice that in the iOS project, you now have a file AppDelegate.cs.txt. Copy and paste its contents over the contents of AppDelegate.cs.
We’ll want to create the following folders in the test PCL:
- Converters
- Mocks
- Model
- View
- ViewModel
Tests
In the Model folder, we’ll add our first class of tests which we’ll call ProjectTests. You can take out the constructor, and add our first test (notice the attribute):
[Fact]
public void OneEqualsOne(){
int expected = 1;
int actual = 1;
Assert.Equal(expected, actual);
}
Before running this first test, return to the BlogProjectTracker.iOS AppDelegate.cs file and change this line:
AddTestAssembly(Assembly.GetExecutingAssembly());
to this line:
AddTestAssembly(typeof(ProjectTests).Assembly);
Set the Test.iOS project as the startup and click run. The iOS simulator should come up, and your test should be there. Click Run Everything and you should see that your test is a success,
Next week we begin test-first development of our project.
2 Responses to 52 Weeks of Xamarin: Week 6 – Starting xUnit Testing