52 Weeks of Xamarin: Week 6 – Starting xUnit Testing

Last week we decided to start with test-first development using XUnit.  To
get underway, let’s create a eggwithlight 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.xUnit

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,

TestRunner

 

Next week we begin test-first development of our project.

 

 

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials, Xamarin. Bookmark the permalink.

4 Responses to 52 Weeks of Xamarin: Week 6 – Starting xUnit Testing

  1. Pingback: My readings in 2015 week 40 | My path to become awesome dev

  2. Pingback: Jesse Liberty: 52 Weeks of Xamarin: Week 7 – First Unit Tests | XamGeek.com

Comments are closed.