Using a test data factory to create data
The factory pattern is a common OO pattern from the famous Gang of Four (GoF) Design Patterns book. A test data factory is a tweaked implementation of this pattern that enables the creation of data for use in testing, in a simple and repeatable manner.
Our test data factory will make it easier for us to create test data in a repeatable way by abstracting away the logic to create record instances into a single location: the test data factory. This will make it much easier for us to update the code in the future should an update be required—for example, a new required field is added to the object.
There are a number of test data factories available from other developers within the Salesforce ecosystem that provide a series of ways of creating test data. Some utilize dynamic describe information on the object to retrieve a list of required fields and pre-populate the field with data, while others use default record templates. Some...