Chapter 9. Unit Testing Your Xamarin.Forms Apps Using the NUnit and UITest Frameworks

In our previous chapter, we updated our TrackMyWalks application to allow us to use Facebook to sign into our app. You learned how you can use both the Xamarin.Auth and Facebook SDK to authenticate if the user is a valid Facebook user. Next, you learned how to create a custom FacebookApiUser model and FacebookCredentials class that will be used to store the user's credentials, so that these can be used throughout our app to obtain information about the user, as well as post information to their Facebook wall.

During the development of our TrackMyWalks app, we have designed and implemented various design patterns and best practices, with the intention of making it easier to maintain and test our app by separating the user interface and business logic.

In this chapter, you'll learn how to create and run unit tests using the NUnit and UITest testing frameworks right within the Xamarin Studio IDE. You'll learn how to write unit tests for our ViewModels that will essentially test the business logic to validate that everything is working correctly, before moving on to testing the user interfaces portion using automated UI testing.

This chapter will cover the following topics:

  • Creating a unit testing solution using the popular NUnit testing framework
  • Adding the Moq NuGet package to the unit testing solution
  • Adding the Xamarin Test Cloud Agent NuGet package to the UITest solution
  • Successfully learning how to test your ViewModels
  • Running unit tests and UITests using the Xamarin Studio IDE
  • Understanding the common types of UITest testing methods
  • Creating a unit testing solution using the UITest framework
  • Successfully learning how to test your ContentPages (Views)

Creating a unit test solution folder using Xamarin Studio

During the development of our TrackMyWalks application, we have designed the user interfaces, ViewModels, and ContentPages. As developers, there may be times when we would like to obtain feedback to let us know when our application logic is working as expected. We can use the NUnit testing framework to provide us with that confirmation.

In this section, you'll begin by adding a new solution folder to our existing TrackMyWalks Portable Class Library solution. This new solution folder will be used to separate each of our NUnit and UITests from our main solution.

The good news is that Xamarin Studio has built in support for the NUnit framework that we can run our unit tests from, and then have our results displayed, right within the Xamarin Studio IDE.

Let's look at how to add a new Solution folder to our TrackMyWalks Portable Class Library, by performing the following steps:

  1. Right-click on the TrackMyWalks solution project and choose the Add | Add Solution Folder menu option, as shown in the following screenshot:

    Creating a unit test solution folder using Xamarin Studio

  2. Next, enter in TrackMyWalks.Tests for the name of the solution folder.

Now that you have created the TrackMyWalks.Tests solution within the main TrackMyWalks solution project, our next step is to create a new unit test project solution, that will be responsible for testing the business logic within our TrackMyWalksViewModels.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.39.60