Property-based testing is a form of testing that has been around for quite a while. LayoutTest is a form of property-based testing. In this video, Kyle mentions other testing frameworks that implement property testing, such as SwiftCheck. Property-based testing is popular in functional programming, but that doesn't mean you have to know functional programming to take advantage of it. Plus, learn how LayoutTest differs from the standard idea of property-based testing.
- [Narrator] LayoutTest borrows concepts from something called property-based testing which is popular in functional languages like Haskell or Scala. In this video we will learn a little bit about what property-based testing is and how it is used. We will also look at how LayoutTest differs from standard property-based testing. The original implementation in Haskell was called QuickCheck. There's also an implementation in Scala called ScalaTest. The framework generates random values and method calls using generators. You then define invariants or properties that must hold true.
After all the random calls are made with random data, you test these invariants to make sure they hold true. When a failure occurs, the framework will tell you what path led to this failure so that you can reproduce it and debug. You can go a little more in depth with this and write code to narrow down test data and calls to make it easier to debug exactly what's going on. You can make constraints for the random input as well to make it so that certain issues that you know you do not need to test for don't get input.
And you can also constrain inputs based on already selected random inputs. So if one property has a certain value that is dependent on another property, you can also represent that here. Some of the benefits are you would spend less time thinking of test cases and rather how to define your test data. You also get more coverage because of the random input and you discover edge cases that are very difficult to find by traditional means. Also, every time you run the test you're running with different random data so you end up with better test coverage the more times you run it.
For LayoutTest, instead of random input, it uses generators with defined data that we went over in the last video. These generators are defined for you and you can create custom sets of data. Stability is also prioritized instead of randomness. There are only so many things that can change a layout of a view in most cases. So instead of having completely random data, LayoutTest goes with defining a set of data that covers many edge cases. There are also no method calls in LayoutTest because the view is just laid out.
Like property-based testing, LayoutTest tests invariants with many sets of data that are generated. Here you will see there is a property-based testing library for Swift and one for Objective C. There's also a short talk on how to use the SwiftCheck library if you'd like to learn more. Property-based testing is an interesting concept that you may find useful for your unit test. It's definitely worth exploring.
- 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