Skip to content

Hypothesis property-based testing demo #2732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ venv
downloaded_files/
pymodbus.log
*.lock
.hypothesis/
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ development = [
"build>=1.2.2",
"codespell>=2.3.0",
"coverage>=7.6.1",
"hypothesis>=6.137.1",
"mypy>=1.11.2",
"pylint>=3.3.0",
"pytest>=8.3.3",
Expand Down Expand Up @@ -313,3 +314,4 @@ quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

38 changes: 38 additions & 0 deletions test/hypothesis_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Property-based tests for Modbus payload round-trip encoding/decoding with randomized word order."""
import pytest
from hypothesis import given, settings
from hypothesis import strategies as st

from pymodbus.client.mixin import ModbusClientMixin as mixin


word_order_strategy = st.one_of(st.just("big"), st.just("little"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to @pytest.mark.parameterize but it will mix in the big/little with testing a range of values.


@given(value=st.integers(min_value=-(2**15), max_value=2**15 - 1), word_order=word_order_strategy)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will perform ~100 tests with strategically chosen numbers in the given range to stress-test the function. (like fuzzing)

def test_round_trip_int16(value, word_order):
"""Test round-trip int16 encoding/decoding with randomized Modbus word order."""
regs = mixin.convert_to_registers(value, word_order=word_order, data_type=mixin.DATATYPE.INT16)
result = mixin.convert_from_registers(regs, word_order=word_order, data_type=mixin.DATATYPE.INT16)
assert result == value

@given(value=st.integers(min_value=0, max_value=2**16 - 1), word_order=word_order_strategy)
def test_round_trip_uint16(value, word_order):
"""Test round-trip uint16 encoding/decoding with randomized Modbus word order."""
regs = mixin.convert_to_registers(value, word_order=word_order, data_type=mixin.DATATYPE.UINT16)
result = mixin.convert_from_registers(regs, word_order=word_order, data_type=mixin.DATATYPE.UINT16)
assert result == value

@given(value=st.integers(min_value=-(2**31), max_value=2**31 - 1), word_order=word_order_strategy)
def test_round_trip_int32(value, word_order):
"""Test round-trip int32 encoding/decoding with randomized Modbus word order."""
regs = mixin.convert_to_registers(value, word_order=word_order, data_type=mixin.DATATYPE.INT32)
result = mixin.convert_from_registers(regs, word_order=word_order, data_type=mixin.DATATYPE.INT32)
assert result == value

@given(value=st.floats(allow_nan=False, allow_infinity=False, width=64), word_order=word_order_strategy)
@settings(deadline=None)
def test_round_trip_float64(value, word_order):
"""Test round-trip float64 encoding/decoding with randomized Modbus word order."""
regs = mixin.convert_to_registers(value, word_order=word_order, data_type=mixin.DATATYPE.FLOAT64)
result = mixin.convert_from_registers(regs, word_order=word_order, data_type=mixin.DATATYPE.FLOAT64)
assert result == pytest.approx(value, rel=1e-12)