Editing
Property-based Testing with PropEr
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Property-based Testing with PropEr == [[File:PropEr_logo.png|right|100px|alt=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)|Elixir]] programming language is '''PropEr'''. === Introduction to PropEr === [[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 === 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 === 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 === 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 === 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 === 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.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information