Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Lib/test/support/_hypothesis_stubs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
"assume",
"reject",
"register_random",
"stateful",
"strategies",
"HealthCheck",
"settings",
"Verbosity",
]

from . import strategies
from . import stateful, strategies


def given(*_args, **_kwargs):
Expand Down
97 changes: 97 additions & 0 deletions Lib/test/support/_hypothesis_stubs/stateful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Stubs for the subset of ``hypothesis.stateful`` used by the test suite.

The stateful API has no equivalent of ``@example``, so without Hypothesis
there is nothing to replay: the ``TestCase`` generated for a state machine
is skipped, and ``run_state_machine_as_test`` raises ``unittest.SkipTest``.
The decorators and helpers are accepted so that state machine classes can
be defined at import time.
"""
import unittest

from ._helpers import stub_factory
from .strategies import StubStrategy

__all__ = [
"Bundle",
"MultipleResults",
"RuleBasedStateMachine",
"consumes",
"initialize",
"invariant",
"multiple",
"precondition",
"rule",
"run_state_machine_as_test",
]

_SKIP_REASON = "Hypothesis required for stateful test"

Bundle = stub_factory(StubStrategy, "hypothesis.stateful.Bundle")


def consumes(bundle):
return bundle


class MultipleResults:
def __init__(self, *values):
self.values = values


def multiple(*args):
return MultipleResults(*args)


def _identity_operator(f):
return f


def _identity_decorator(*_args, **_kwargs):
return _identity_operator


rule = _identity_decorator
initialize = _identity_decorator
invariant = _identity_decorator
precondition = _identity_decorator


class _TestCaseProperty:
"""Build (and cache) a skipped ``unittest.TestCase`` per state machine."""

def __init__(self):
self._cases = {}

def __get__(self, obj, owner):
if owner not in self._cases:

class StateMachineTestCase(unittest.TestCase):
settings = None

def runTest(self):
raise unittest.SkipTest(_SKIP_REASON)

StateMachineTestCase.__name__ = f"{owner.__name__}.TestCase"
StateMachineTestCase.__qualname__ = f"{owner.__qualname__}.TestCase"
self._cases[owner] = StateMachineTestCase
return self._cases[owner]


class RuleBasedStateMachine:
TestCase = _TestCaseProperty()

def bundle(self, name):
return Bundle(name)

def check_invariants(self, settings, output, runtimes):
pass

def setup_state(self):
pass

def teardown(self):
pass


def run_state_machine_as_test(state_machine_factory, *, settings=None):
raise unittest.SkipTest(_SKIP_REASON)
3 changes: 3 additions & 0 deletions Lib/test/support/_hypothesis_stubs/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __or__(self, other):
"frozensets",
"functions",
"integers",
"ip_addresses",
"iterables",
"just",
"lists",
Expand All @@ -62,6 +63,8 @@ def __or__(self, other):
"slices",
"timedeltas",
"times",
"timezone_keys",
"timezones",
"text",
"tuples",
"uuids",
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/support/hypothesis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
except ImportError:
from . import _hypothesis_stubs as hypothesis
else:
# Real Hypothesis does not import this submodule on its own; do it here
# so that ``hypothesis.stateful`` is available in both modes.
import hypothesis.stateful

# Regrtest changes to use a tempdir as the working directory, so we have
# to tell Hypothesis to use the original in order to persist the database.
from test.support import has_socket_support
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``hypothesis.stateful`` and the ``timezones()`` and ``ip_addresses()``
strategies to the Hypothesis stubs used when Hypothesis is not installed.
Loading