This video shows how LayoutTest allows you to specify different sets of data for your tests to make sure your layout works under a variety of circumstances. Especially with user-generated content, you may often see unexpected sets of data shown in your iOS app. LayoutTest helps you test all of the edge cases of these data sets very easily.
- [Instructor] In order to fully test if a view's layout is correct you must make sure that it functions properly with all different types of data that may be used. In this video, we will see how to specify the data that will be used to test your view's layout. Layout test defines a base class called LYTDataValues in Objective-C or just DataValues in Swift. This class helps you to define all of your test data for each property in the view you will be testing. The DataValues object is one object that has multiple values to test and it allows LayoutTest to run through all of these values for a single property.
For instance, if we were testing a UILabel, we would need to have a set of strings to test. In this case, we see DataValues is instantiated with the values parameter and it has an empty string, a one word string, and a multi-word string. Basically, you want to think of edge cases. You specify all combinations of data for each property and LayoutTest will loop through all of these values to test all different combinations. Let's take an example where we have two labels and we have three values to test for each label.
In this case, we have three times three different combinations, or three squared which is nine different combinations of test data because we allow duplicate values to be in the two labels. This means that the time complexity for running our test is exponential, you multiply the number of values for each property together. As you may be thinking, this may become an issue later on if you have too many properties, we will explore this later in the What Common Errors Occur video. When you run a test, it goes through all of these different combinations.
As you see this is a table of the different values it goes through in our example. We have two labels, and we have the three different types of values. It would go through all of these different combinations. As you can tell, you could expand this table to more properties and different types of values. You will see this type of diagram later on in the course as well. LayoutTest will automatically run through these combinations for you. It would be annoying to have to create a new DataValues object every time you wanted to test a property that, for instance needs a string.
Likely the same edge cases exist for any property that takes a string or different data types. Because of this, LayoutTest provides a few concrete subclasses of DataValues that gives you different data values to test common edge cases. For instance, the BoolValues object has a true and false that it would loop through. The StringValues object would have empty, nil, a long string, a normal length string, and a string with special characters. Feel free to look at the implementation of the Objective-C classes associated with these in the LayoutTest source code on the GitHub project to see what specific cases will be tested.
For your use, you can probably just use these objects as they are to get great test coverage. When you use these subclasses, the most common way to use them is with the initializer with no arguments. However, there are also two different initializers that are good to know about. There's the initializer where you can specify the required parameter, which if set to true, it will not return a nil value. So, it will automatically filter that out. Then there's the more generic initializer that takes a filter closure where you can filter out any different values from this object.
This is not very common but it does exist. You can also create your own subclass of data values. You want to do this when you have a certain set of values that are used numerous times in your tests. It's helpful if you have a custom enum set of values, or you want to restrict the integers or strings that can be assigned to a certain property. All you have to do to create your custom subclass of data values is set the values property to your static list of values. In a later video, you will learn how to use these objects to provide data to the test.
- Installing the library
- Specifying test data
- Reviewing property-based testing
- Using test data and writing the LayoutTest test
- Testing views at different sizes
- Debugging with snapshots
- Dealing with common errors
- Advanced debugging tips
- Exploring catalog view