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.

 

 

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been 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, Microsoft MVP and Telerik MVP.
This entry was posted in Essentials, Xamarin. Bookmark the permalink.

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

  1. Sanket says:

    Nice Article, I have followed same to implement TDD in my Xamarin Forms project.
    Now I also want to test unit test case coverage for the same. Does Opencover works here? Or any other suitable tool?
    I tried open cover but some facing some issues. I have created separate PCL project for unit tests referring to project PCL, which later executes in Android and iOS test project.
    Please suggest.

  2. Sherita says:

    But,fortunately many grants for firsttime home buyersare available to help pay for a new home.

    Dan Havey, you have a great website, its called and why dont
    you talk real quick about that. Each house loan program will have its own individual set of rules.
    Sometimes dirty talking over a text message is way easier than doing it live
    in person. They have expert eyes to sneak around and
    noses to sniff throughout neighborhoods with homes for sale that
    are just about to be posted in the market.

    Assistance may be available through your local Community Planning & Development
    office or nonprofit organizations.

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

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

Leave a Reply

Your email address will not be published.