PropEr

From Elixir Wiki
Jump to navigation Jump to search

PropEr[edit]

File:PropEr logo.png
PropEr logo

PropEr is a property-based testing tool for the Elixir programming language. It allows developers to express and verify properties of their code through automated testing. By generating random input data and testing code against specified conditions, PropEr helps identify bugs, edge cases, and corner scenarios that may not be immediately apparent.

Features[edit]

PropEr offers several powerful features that make it a valuable tool for Elixir developers:

  • **QuickCheck-style Properties**: PropEr enables developers to define properties with assertions about the expected behavior of their code.
  • **Automated Test Generation**: PropEr generates random test data based on the properties defined, providing comprehensive test coverage.
  • **Shrinking Mechanism**: When a property fails, PropEr automatically minimizes the generated counterexample, making it easier to debug and fix the issue.
  • **Integration with Elixir**: PropEr seamlessly integrates with the Elixir ecosystem, leveraging Elixir's robust features and libraries.
  • **Custom Generators**: PropEr allows developers to define custom data generators, enabling them to test code with specific input requirements.
  • **Stateful Testing**: PropEr supports stateful testing, allowing developers to define and test code that maintains internal state.

Getting Started[edit]

To use PropEr in an Elixir project, follow these steps:

1. Install PropEr by adding it as a dependency in your `mix.exs` file:

```elixir defp deps do

 [
   {:proper, "~> x.x.x"}
 ]

end ```

2. Run the `mix deps.get` command to fetch and compile the dependency.

3. Define properties in your test modules using the `@proper` attribute:

```elixir defmodule MyModuleTest do

 use ExUnit.Case
 use PropEr.Property
 # Define properties using the @proper attribute
 @proper my_property do
   forall {x, y} <- proper_gen.int(), do: x + y == y + x
 end
 # Run the properties using proper_check/2
 test "property test" do
   PropEr.check_all()
 end

end ```

4. Run the tests using the `mix test` command, which will execute both regular tests and property-based tests.

Resources[edit]

To further explore PropEr and property-based testing in Elixir, check out the following resources:

  • PropEr Documentation: The official documentation provides comprehensive information on installing, configuring, and using PropEr.
  • PropEr GitHub Repository: The source code repository for PropEr contains the latest code, issues, and contributions.
  • Elixir and Mix Guide: If you are new to Elixir, this guide will help you learn the basics of Elixir and how to use Mix, Elixir's build tool.

See Also[edit]

  • Elixir: An introduction to the Elixir programming language.
  • ExUnit: Elixir's built-in test framework used for testing Elixir code.
  • QuickCheck: An influential property-based testing tool for functional programming languages.