Search⌘ K
AI Features

Running a Property

Understand how to integrate and execute a basic property test in an Elixir project using PropEr. Explore writing properties instead of traditional unit tests, running multiple test cases, and interpreting the bundled results for property-based testing.

We'll cover the following...

The properties we’ve seen so far left a lot to the imagination. Now that we have a stand-in directory structure similar to what we’d use in a real project, we can tie everything together. We’ll add a property to the project and execute it. The property will be basic and test nothing of significance, but it’ll give us brief idea of what things look like and how they should all fit together.

Property practice

We’ll replace all the code in the test/pbt_test.exs file with the code shown in the code widget below.

The use instructions in lines 2 and 3 of the code loads the required macro definitions, and instead of the test "description" do ... end format required by ExUnit, we’ll use the property "description" do ... end to write properties.

We can run the tests in the code below.

Note: To execute the code after changing it, please click “Run” which will save our code, and then run mix test in the terminal.

defmodule Pbt do
  @moduledoc """
  Documentation for Pbt.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Pbt.hello
      :world

  """
  def hello do
    :world
  end
end
Running a property in Elixir

A hundred executions have all been bundled together. The mix function will group all of them under a single one if they pass.

Let’s focus on the property for now. We’ll get to the rest of the code in the next chapter.

Now we have a test —many tests, in fact. Each period is a specific test sample, and there are a hundred of them.