Xcode validation, performance and UI tests
In this section we are going to study the capabilities of Xcode to generate tests that allow us to detect errors and evaluate performance. These Tests can be performed at the code level related to the data or processes and at the code level related to the graphical interface.
We will focus on related Xcode native testing mechanisms with the class XCTestCase. Xcode also allows you to add other frameworks for testing like the popular one GHUnit; However, the native methods that we will explain In this section they are much more integrated with the development environment.
- 4.1 Preparing the project for testing
- 4.2 Unit Test: Validation tests
- 4.3 Unit Test: Performance tests
- 4.4 UI Test: Interface Testing
4.1 Preparing the project for testing
When we create the project we select the options Includes Unit TestandIncludes UI Test.

Includes Unit Test: It will allow us to carry out testing code related to data and processes.
Includes UI Test: It will allow us to perform tests related to the interface.
In the Project Navigator the folders will be generated <project_name>Tests and <project_name>UITest. In this case, what is the name of the project? TestSample if generate the folders TestSampleTests and TestSampleUITest.

Now we can activate the test browser, the rhombus with a dash in medium, and we will see the two test modules (targets) initially created.

Each test module is made up of a set of classes derived from XCTestCase and each class a set of methods. We can execute a single method, all the methods of a class or all the methods of all classes of a module or target. The button play located to the right of the test navigator indicates the tests we want to perform.
4.2 Unit Test: Validation tests
Within the class that Xcode has created we automatically fill in the method testExamplewho has also created us. We can create any other method with the name we want. Sometimes when we add new testing methods it is necessary to do a build to appear in the test browser.
Before executing any test method of this class, execute the method setup. This method initializes the member variables that we may need in any test method the class. In our example we define the propertiesapp, appDelegate and viewController and in the method setup we load them.
Swift:
We can now implement our first testing method, testExample. What we are going to test is the method ParseNumber of our ViewController. This method receives a NSString and returns true if represents a decimal number. To test it, we pass it a string that represents a number and should return true. If everything is correct the method XCTAssertTrue will be executed correctly and will update all flags within Xcode accordingly.
Swift:
When executing the method by pressing play we see how some green indicators next to the method, both in the code and in the test browser.

Next we can modify our method ParseNumber so that it always returns false and we will see how our test method reacts.
Swift:
We execute again by clicking on the green indicator next to the method in the test browser. We will see that when you move the mouse over it, it appears the play button.
In this case, a red error indicator appears on both the code as in the test browser.

The error with the chain that we have configured in XCTAssertTrue.

There are a variety of assertions of type XCTAssertTrue for carry out all the checks that we consider necessary. you can find in the following link:
4.3 Unit Test: Performance tests
The objective of these tests is to determine the average execution time of a certain piece of code. The code we want to study we place inside measureBlock. In this case the call to Parse of the object viewController.
Swift:
The method Parse of viewController is a simulation method that simply pauses for two seconds.
Swift:
In addition to the green indicators in the output window we get the next message:
Test Case '-[TestSampleTests testPerformanceExample]' measured [Time, seconds] average: 2.003, relative standard deviation: 0.094%, values: [2.000132, 2.000050, 2.004982, 2.004994, 2.001563, 2.003315, 2.004982, 2.002713, 2.005006, 2.002375],
performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10,000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
It tells us that the average (average) of execution time of our method is 2003. Perform 10 runs and tells us the time of each execution: values: [2.000132, 2.000050, 2.004982, 2.004994, 2.001563, 2.003315, 2.004982, 2.002713, 2.005006, 2.002375]
It also calculates various statistical parameters such as type deviation, relative standard deviation: 0.094%
We can see how the measurements made by the test correspond to the two seconds of pause that we have indicated in the method call sleepForTimeInterval
Performance tests do not count the time spent stopping in breakpoints (breakpoint). It is necessary disable breakpoints before running tests performance.
4.4 UI Test: Interface Testing
The objective of these tests is to evaluate the behavior of the application directly from the interface elements that the user will use. If you can consider a complete simulation of the user interaction.
Interface tests are organized in the same way as tests previous: modules (targets), classes and methods.
The generation of a test method has two phases:
- Recording of user interaction.
- Add thevalidationwhat we want to do about the result.
1. Recording of the interaction
We click on a method in the test browser and place the cursor within it. We see that a red button appears at the bottom circular (REC). This button will launch the application recording the user interactions.

In our example we place ourselves inside the method testExample which is empty.
}
While we are carrying out actions on the interface, the code for our test method. The recording consists of the generation of code inside the method where we had placed the cursor.
In this case:
- - We have clicked on a text field on the interface.
- - We have written “000”.
- - We have clicked on the “HasNumber0” button
The method looks like this:
Swift:
2. We add validation
At the end of the code that has been generated automatically we check that the text that appears in the text field after pressing the button HasNumber0 is “YES”, since the code associated with that button checks that the previously entered text is 0.
And the method code would remain in the controller:
Next we try the method by pressing play on the right side and generates the simulation. When it ends, the result is indicated on our test view with the corresponding green indicator.
