Data Generation
Explore how to use StreamData in Elixir to generate random data streams for property-based testing. Learn to create and compose generators for integers, lists, and complex structures such as random email addresses. Understand how binding generators enables building flexible data generation tailored for testing scenarios.
One of the design goals of stream_data is to provide a set of tools for data generation that could also work outside of property-based testing. For example, generating random data can be useful when seeding databases with fake data.
What are data generators?
At the core of data generation are generators. A generator is a data structure that contains logic that stream_data uses to generate data. Essentially, a generator is like a function that we can call to generate random terms. Let’s start with a simple instance of a generator, StreamData.integer/0. This generator produces random integers. We can use all the stream_data generators as Elixir streams since they implement the Enumerable protocol.
Executable
Output
iex> StreamData.integer() |> Enum.take(5)
[0, 0, -3, 0, -1]
stream_data generators are infinite streams of random data, so we only had to take a few items out of the stream using Enum.take/2 in this example. If we had called Enum.to_list/1 passing the generator as the argument, we would’ve waited forever.
More on stream_data generators
stream_data comes equipped with a few generators for simple data types, like the ones for integers or booleans, plus a bunch of ways to combine ...