Friday, April 7, 2017

XCUI: Create your first XCUI test

Last time, we have discussed What is XCUI. This time, let's see how can we create your first XCUI test in Xcode.

The Test Navigator

Xcode has a test navigator at the Navigator Bar.  This navigator shows the hierarchy of the test bundle, classes and methods. One projects may have multiple test bundles, each test bundle may have multiple classes, and each class may have multiple test methods. You can easily create, manage and run the tests under this navigator.
The snapshot above is a sample navigator with three test bundles. The bundle "Sanity" have one class named "Sanity", and under this class there are four test methods. Each test method is matching to a test case. So there are four test cases in this bundle. The test case "testCreateDossierFromDataset" is disabled as it is in gray. Click on the test bundle "Sanity", it will be active with an arrow shows up at the right. Clicking this arrow will trigger the running of all four tests.

Create Your First Test


Step 1: Create the test bundle 
To create your first test, you need to create the test bundle first. At the test navigator, click the add button (+) to create a new UI Test Target.
Choose the OS X UI Testing Bundle, and click 'Next'.

Create the Product Name, choose the language, edit the Project and the Target to be tested. Click 'Finish'.
The new test bundle will appear directly under the navigator. And a default class with the same name as the product name will be created together. This class will automatically contain one test method.


Step 2: Modify the test method
In the class, there are three methods will be created automatically: setUp(), tearDown(), testExcample(). The setUp method is called before each test method, it's the best place to set any configurations or read in any environment parameters for the test. The tearDown method is called after each test method, it's the best place to do the clear-up work. Each method starting with "test" will be considered as a test method and will be able to run.

Here in the class at the setUp method, there are two lines generated automatically. The first line sets the 'continueAfterFailure' to false, this means the test will not stop at the failures. The second line launches the application.

In the method testExample, we add in two lines as below. The first line we assign the application to a variable. The second line we assert the existence of the application.


Step 3: run the test cases 
Run the test case by click the left side arrow.
 After succeed, you will see the test method is flagged as green.

The Report Navigator for Test Details

At the Report Navigator, you can see the details of the test status step by step. The Report Navigator is also located on the navigator bar.  Click the test under the scheme, you will be able to see all the test status on the right panel. Each action is recorded with a timestamp, and at certain actions, there is a shortcut automatically generated.  Click the icon similar to an eye to see the shortcuts.

And the tap "Coverage" shows the code coverage of this test on the target project. The tab "Logs" shows the location where the logs generated for this test.

This chapter shows how to create a test case in XCUI and how to read the test details. Next chapter, I will go deep into the classes of Xcode UI framework.




No comments :

Post a Comment