Property-based Testing with PropEr

From Elixir Wiki
Jump to navigation Jump to search

Property-based Testing with PropEr[edit]

PropEr logo

Property-based Testing is a software testing technique where the behavior of a program is verified against a set of properties defined by the tester. It provides a way to exhaustively test the program by generating a large number of test cases automatically. One popular tool for property-based testing in the Elixir programming language is PropEr.

Introduction to PropEr[edit]

PropEr is a property-based testing framework built specifically for Elixir. It allows developers to define properties as high-level specifications, which are then used to automatically generate test cases that validate the behavior of the code under various scenarios. With PropEr, you can effectively test your code against a wide range of inputs and edge cases, ensuring its correctness.

Getting Started with PropEr[edit]

To use PropEr in your Elixir project, you need to add it as a dependency in your `mix.exs` file.

```elixir defp deps do

 [
   {:proper, "~> 1.1", only: :test}
 ]

end ```

After adding the dependency, run `mix deps.get` to fetch the necessary libraries. Now, you can start writing property-based tests using PropEr.

Writing Property-Based Tests[edit]

Property-based tests are defined using PropEr's `property/1` macro. The macro takes a property specification as an argument. A property specification is a function that defines the desired behavior of the code under test. It typically contains one or more testable properties expressed as logical conditions over the inputs and outputs of the function.

Here is an example of a simple property-based test for a function `add/2` which adds two integers:

```elixir property "Addition is commutative" do

 check all a, b do
   assert add(a, b) == add(b, a)
 end

end ```

In this example, the property "Addition is commutative" verifies that the `add/2` function produces the same result regardless of the order of the operands.

Running Property-Based Tests[edit]

To execute the property-based tests, use the `mix test` command. PropEr will automatically generate and run test cases based on the properties defined in the code.

```shell $ mix test ```

PropEr provides detailed feedback about the test execution, including the number of generated tests, the success rate, and any failures encountered. This helps identify and fix issues in the code under test.

Advanced Features[edit]

PropEr offers various advanced features to enhance property-based testing in Elixir:

  • Random Generators: PropEr can generate random input values for testing based on custom data types and constraints.
  • Stateful Testing: PropEr allows stateful testing where the behavior of a stateful system can be tested by generating sequences of operations.
  • Shrinking: When a failing test case is found, PropEr performs a shrinking process to find a minimal failing test case, making it easier to identify the root cause.

Conclusion[edit]

Property-based testing with PropEr is a powerful approach to testing Elixir code. It enables developers to write concise and expressive properties, leading to comprehensive and efficient test suites. By leveraging the automatic test generation capabilities of PropEr, you can significantly increase the confidence in your code's correctness and robustness.

See Also[edit]