### Get Example Value from Strategy Source: https://hypothesis.readthedocs.io/en/latest/tutorial/introduction.html Demonstrates how to use the .example() method on a strategy to get a sample value. This method is intended for interactive use only. ```python >>> st.lists(st.integers() | st.floats(allow_nan=False)).example() [-5.969063e-08, 15283673678, 18717, -inf] ``` -------------------------------- ### Example of Phase Configuration Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/_settings.html Demonstrates how to configure Hypothesis to run only explicit examples from the @example decorator. ```python settings(phases=[Phase.explicit, Phase.shrink]) ``` -------------------------------- ### Get Example Value from Strategy Source: https://hypothesis.readthedocs.io/en/latest/_sources/tutorial/introduction.rst.txt Demonstrates the use of the `.example()` method on a Hypothesis strategy to retrieve a sample value. This method is intended for interactive use (REPL) and not for use within tests. ```pycon >>> st.lists(st.integers() | st.floats(allow_nan=False)).example() [-5.969063e-08, 15283673678, 18717, -inf] ``` -------------------------------- ### Fetch examples from Redis Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Retrieve all examples associated with a given key from the Redis database. This is useful for replaying or analyzing previously stored examples. ```python examples = list(database.fetch(b"my_test_key")) ``` -------------------------------- ### Save an example to Redis Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Use the save method to add an example to the database. The key identifies the test case, and the value is the example data. ```python database.save(b"my_test_key", b"example_data_1") ``` -------------------------------- ### settings.max_examples Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/_settings.html Property to get the maximum number of satisfying examples Hypothesis will consider before stopping the search for counter-examples. ```APIDOC ## max_examples (property) ### Description Gets the maximum number of satisfying examples Hypothesis will consider before stopping. ### Note Hypothesis might call the test function fewer times if a bug is found early. ``` -------------------------------- ### Fetch Examples from Database Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Fetches all stored example values for a given key. Returns an empty iterator if the key or its directory does not exist. ```python def fetch(self, key: bytes) -> Iterable[bytes]: kp = self._key_path(key) if not kp.is_dir(): return try: for path in os.listdir(kp): try: yield (kp / path).read_bytes() except OSError: pass except OSError: # pragma: no cover # the `kp` directory might have been deleted in the meantime pass ``` -------------------------------- ### Move an example between keys in Redis Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Transfer an example from a source key to a destination key. If the source and destination keys are the same, it effectively re-saves the example. ```python database.move(b"source_key", b"destination_key", b"example_to_move") ``` -------------------------------- ### Directory-Based Example Database Initialization Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Initializes a DirectoryBasedExampleDatabase with a given path. This class stores example data in a file system structure. ```python class DirectoryBasedExampleDatabase(ExampleDatabase): """ An example database that stores examples in a directory structure. Each test corresponds to a directory, and each example to a file within that directory. While the contents are fairly opaque, a |DirectoryBasedExampleDatabase| can be shared by checking the directory into version control, for example with the following ``.gitignore``:: # Ignore files cached by Hypothesis... .hypothesis/* # except for the examples directory !.hypothesis/examples/ Note however that this only makes sense if you also pin to an exact version of Hypothesis, and we would usually recommend implementing a shared database with a network datastore - see |ExampleDatabase|, and the |MultiplexedDatabase| helper. """ # we keep a database entry of the full values of all the database keys. # currently only used for inverse mapping of hash -> key in change listening. _metakeys_name: ClassVar[bytes] = b".hypothesis-keys" _metakeys_hash: ClassVar[str] = _hash(_metakeys_name) def __init__(self, path: StrPathT) -> None: super().__init__() self.path = Path(path) self.keypaths: dict[bytes, Path] = {} self._observer: BaseObserver | None = None self._ensure_directory_exists_called = False ``` -------------------------------- ### Fetch Examples from InMemoryDatabase Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Retrieves all examples associated with a given key from the in-memory database. Returns an empty iterable if the key is not found. ```python def fetch(self, key: bytes) -> Iterable[bytes]: yield from self.data.get(key, ()) ``` -------------------------------- ### Using @example for Specific Inputs Source: https://hypothesis.readthedocs.io/en/latest/_sources/tutorial/replaying-failures.rst.txt Use the `@example` decorator to explicitly provide inputs that Hypothesis will always run. These explicit examples run before randomly generated ones and do not undergo shrinking. ```python # two mersenne primes @example(2**17 - 1) @example(2**19 - 1) @given(st.integers()) def test_integers(n): pass test_integers() ``` ```python @example(2**17 - 1) @given(st.integers()) def test_something_with_integers(n): assert n < 100 ``` -------------------------------- ### Install Hypothesis Source: https://hypothesis.readthedocs.io/en/latest/_sources/quickstart.rst.txt Use pip to install the Hypothesis library. ```shell pip install hypothesis ``` -------------------------------- ### find Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html Returns the minimal example from a given strategy that matches a predicate function. It searches for an example that satisfies the condition and raises an exception if no such example is found within the configured limits. ```APIDOC ## find ### Description Returns the minimal example from the given strategy ``specifier`` that matches the predicate function ``condition``. ### Parameters - **specifier** (SearchStrategy[Ex]) - The strategy to draw examples from. - **condition** (Callable[[Any], bool]) - The predicate function to match. - **settings** (Settings | None) - Optional settings for the search. Defaults to a settings object with `max_examples=2000`. - **random** (Random | None) - Optional random number generator for seeding. - **database_key** (bytes | None) - Optional key for database caching. ### Raises - **InvalidArgument**: If `specifier` is not a `SearchStrategy`. - **NoSuchExample**: If no example matching the condition is found. ``` -------------------------------- ### Find Minimal Example with Condition Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html Finds the minimal example from a given strategy that satisfies a specific condition. It uses Hypothesis's internal testing mechanism and can optionally use a database key for caching. Raises NoSuchExample if no example is found. ```python def find( specifier: SearchStrategy[Ex], condition: Callable[[Any], bool], *, settings: Settings | None = None, random: Random | None = None, database_key: bytes | None = None, ) -> Ex: """Returns the minimal example from the given strategy ``specifier`` that matches the predicate function ``condition``.""" if settings is None: settings = Settings(max_examples=2000) settings = Settings( settings, suppress_health_check=list(HealthCheck), report_multiple_bugs=False ) if database_key is None and settings.database is not None: # Note: The database key is not guaranteed to be unique. If not, replaying # of database examples may fail to reproduce due to being replayed on the # wrong condition. database_key = function_digest(condition) if not isinstance(specifier, SearchStrategy): raise InvalidArgument( f"Expected SearchStrategy but got {specifier!r} of " f"type {type(specifier).__name__}" ) specifier.validate() last: list[Ex] = [] @settings @given(specifier) def test(v): if condition(v): last[:] = [v] raise Found if random is not None: test = seed(random.getrandbits(64))(test) test._hypothesis_internal_database_key = database_key # type: ignore try: test() except Found: return last[0] raise NoSuchExample(get_pretty_function_description(condition)) ``` -------------------------------- ### Save Example to InMemoryDatabase Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Saves a new example (value) for a given key to the in-memory database. If the value is new for the key, it broadcasts a 'save' event. ```python def save(self, key: bytes, value: bytes) -> None: value = bytes(value) values = self.data.setdefault(key, set()) changed = value not in values values.add(value) if changed: self._broadcast_change(("save", (key, value))) ``` -------------------------------- ### Setup State for Stateful Testing Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/stateful.html Class method to retrieve or initialize the state setup for a given class. It scans for rule, initializer, and invariant markers. ```python @classmethod def setup_state(cls): try: return cls._setup_state_per_class[cls] except KeyError: pass rules: list[Rule] = [] initializers: list[Rule] = [] invariants: list[Invariant] = [] for _name, f in inspect.getmembers(cls): rule = getattr(f, RULE_MARKER, None) initializer = getattr(f, INITIALIZE_RULE_MARKER, None) invariant = getattr(f, INVARIANT_MARKER, None) if rule is not None: rules.append(rule) if initializer is not None: initializers.append(initializer) if invariant is not None: invariants.append(invariant) if ( getattr(f, PRECONDITIONS_MARKER, None) is not None and rule is None and invariant is None ): raise InvalidDefinition( f"{_rule_qualname(f)} has been decorated with @precondition, " "but not @rule (or @invariant), which is not allowed. A " "precondition must be combined with a rule or an invariant, " "since it has no effect alone." ) state = _SetupState( rules=rules, initializers=initializers, invariants=invariants ) cls._setup_state_per_class[cls] = state return state ``` -------------------------------- ### DatabaseComparison State Example Source: https://hypothesis.readthedocs.io/en/latest/stateful.html This example demonstrates how a DatabaseComparison state can be used to reproduce assertion errors by showing the sequence of operations that led to the failure. ```python state = DatabaseComparison() var1 = state.add_key(k=b'') var2 = state.add_value(v=var1) state.save(k=var1, v=var2) state.delete(k=var1, v=var2) state.values_agree(k=var1) state.teardown() ``` -------------------------------- ### Executor Setup and Teardown Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html Illustrates a pattern for setting up a token before executing a function and tearing it down afterwards, ensuring resource cleanup. ```python token = setup() return function(data) finally: teardown(token) return execute return default_executor ``` -------------------------------- ### Fuzzing with re.compile and Hypothesis Source: https://hypothesis.readthedocs.io/en/latest/reference/integrations.html Use `st.nothing()` as a strategy for patterns when fuzzing `re.compile` if no specific pattern is required. This example demonstrates a basic fuzz test setup. ```python from hypothesis import given, strategies as st, reject import re @given(pattern=st.nothing(), flags=st.just(0)) def test_fuzz_compile(pattern, flags): try: re.compile(pattern=pattern, flags=flags) except re.error: reject() ``` -------------------------------- ### Unsatisfiable Source: https://hypothesis.readthedocs.io/en/latest/reference/api.html Indicates that Hypothesis ran out of time or examples before finding enough that satisfy the assumptions. This can be due to a function being too slow, excessive use of `assume()`, or an overly restrictive starting point. ```python class hypothesis.errors.Unsatisfiable: ``` -------------------------------- ### DataStrategy Invalid Operations Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/strategies/_internal/core.html This method raises an `InvalidArgument` error for operations like `map`, `filter`, `flatmap`, or `example` on a `DataStrategy`, guiding users towards `@composite` for such use cases. ```python def __not_a_first_class_strategy(self, name: str) -> NoReturn: raise InvalidArgument( f"Cannot call {name} on a DataStrategy. You should probably " "be using @composite for whatever it is you're trying to do." ) ``` -------------------------------- ### RedisExampleDatabase.fetch Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Fetches all examples associated with a given key from the Redis database. ```APIDOC ## fetch(key: bytes) -> Iterable[bytes] ### Description Retrieves all example values stored under the specified key from the Redis datastore. It ensures the key's expiration is reset upon fetching. ### Parameters - **key** (bytes) - Required - The key under which the examples are stored. ``` -------------------------------- ### Targeted Property-Based Testing Example Source: https://hypothesis.readthedocs.io/en/latest/_sources/reference/api.rst.txt Demonstrates how to use `hypothesis.target` to guide input generation towards values that are more likely to falsify a property. This can improve test efficiency by focusing on problematic areas of the input space. ```python from hypothesis import given, strategies as st, target @given(st.floats(0, 1e100), st.floats(0, 1e100), st.floats(0, 1e100)) def test_associativity_with_target(a, b, c): ab_c = (a + b) + c a_bc = a + (b + c) difference = abs(ab_c - a_bc) target(difference) # Without this, the test almost always passes assert difference < 2.0 ``` -------------------------------- ### target(observation: int | float, *, label: str = "") -> int | float Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/control.html Provides feedback to Hypothesis to guide its search for inputs that cause errors. Observations must always be finite. Hypothesis tries to maximize the observed value over several examples. The optional 'label' argument can distinguish between and separately optimize distinct observations. ```APIDOC ## target(observation: int | float, *, label: str = "") -> int | float ### Description Calling this function with an ``int`` or ``float`` observation gives it feedback with which to guide our search for inputs that will cause an error, in addition to all the usual heuristics. Observations must always be finite. Hypothesis will try to maximize the observed value over several examples; almost any metric will work so long as it makes sense to increase it. The optional ``label`` argument can be used to distinguish between and therefore separately optimise distinct observations, such as the mean and standard deviation of a dataset. It is an error to call ``target()`` with any label more than once per test case. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **observation** (int | float) - Required - The observed value to guide the search. - **label** (str) - Optional - A label to distinguish between distinct observations. ### Request Example ```python from hypothesis import target # Example: Minimize error by maximizing -abs(error) error = calculate_error() target(-abs(error)) # Example with a label to optimize mean and std dev separately mean_runtime = get_mean_runtime() target(mean_runtime, label="mean_runtime") std_dev_runtime = get_std_dev_runtime() target(std_dev_runtime, label="std_dev_runtime") ``` ### Response #### Success Response (200) - **observation** (int | float) - The observation value that was passed in. #### Response Example ```json { "example": 123.45 } ``` ``` -------------------------------- ### Initialize RedisExampleDatabase Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Instantiate RedisExampleDatabase with a Redis client and optional configuration for expiration and key prefix. Ensure the Redis client is properly configured before use. ```python from redis import Redis from hypothesis.database import RedisExampleDatabase redis_client = Redis() database = RedisExampleDatabase(redis_client) ``` -------------------------------- ### Configure Hypothesis Entry Point in pyproject.toml Source: https://hypothesis.readthedocs.io/en/latest/_sources/extensions.rst.txt Declare the Hypothesis setup hook as an entry point in your pyproject.toml file. This allows Hypothesis to automatically discover and run your setup function upon import. ```toml [project.entry-points.hypothesis] _ = "mymodule.a_submodule" ``` ```toml [project.entry-points.hypothesis] _ = "mymodule:_hypothesis_setup_hook" ``` -------------------------------- ### Improvement to @example() and assume() interaction Source: https://hypothesis.readthedocs.io/en/latest/changelog.html This example demonstrates an improvement in how `assume()` interacts with the `@example()` decorator, preventing `UnsatisfiedAssumption` errors. Use this when `assume()` conditions might conflict with explicitly provided examples. ```python from hypothesis import given, example, assume import pytest @given(value=floats(0, 1)) @example(value=0.56789) # used to make the test fail! @pytest.mark.parametrize("threshold", [0.5, 1]) def test_foo(threshold, value): assume(value < threshold) ... ``` -------------------------------- ### Executing Explicit Examples in Hypothesis Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html This section of code executes explicit examples defined using the @example decorator. It collects any errors that occur during the execution of these examples and applies simplification if verbosity is low. ```python # There was no @reproduce_failure, so start by running any explicit # examples from @example decorators. if errors := list( execute_explicit_examples( state, wrapped_test, arguments, kwargs, original_sig ) ): # If we're not going to report multiple bugs, we would have # stopped running explicit examples at the first failure. assert len(errors) == 1 or state.settings.report_multiple_bugs # If an explicit example raised a 'skip' exception, ensure it's never # wrapped up in an exception group. Because we break out of the loop # immediately on finding a skip, if present it's always the last error. if isinstance(errors[-1].exception, skip_exceptions_to_reraise()): # Covered by `test_issue_3453_regression`, just in a subprocess. del errors[:-1] # pragma: no cover if state.settings.verbosity < Verbosity.verbose: # keep only one error per interesting origin, unless # verbosity is high errors = _simplify_explicit_errors(errors) ``` -------------------------------- ### Example of Hypothesis Phase Explanation Source: https://hypothesis.readthedocs.io/en/latest/changelog.html This example shows how Hypothesis might comment on a failing test case, indicating which parts of the example could be varied without changing the outcome. This helps in understanding minimal failing examples. ```python test_x_divided_by_y( x=0, # or any other generated value y=0, ) ``` -------------------------------- ### example.via() Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html Attach a machine-readable label noting the origin of this example. This method is optional and does not change runtime behavior, but supports self-documenting behavior and tooling. ```APIDOC ## example.via() ### Description Attach a machine-readable label noting what the origin of this example was. `example.via()` is completely optional and does not change runtime behavior. `example.via()` is intended to support self-documenting behavior, as well as tooling which might add (or remove) `@example` decorators automatically. ### Method ```python def via(self, whence: str, /) -> "example": ``` ### Parameters #### Path Parameters - **whence** (str) - Description: The machine-readable label to attach. ``` -------------------------------- ### Provider Conformance Test Setup Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/internal/conjecture/provider_conformance.html This internal setup logic configures the test environment for provider conformance. It handles different provider lifetimes and initializes the provider and its context manager. ```python class CopiesRealizationProvider(HypothesisProvider): avoid_realization = Provider.avoid_realization with with_register_backend("copies_realization", CopiesRealizationProvider): @Settings( settings, suppress_health_check=[HealthCheck.too_slow], backend="copies_realization", ) class ProviderConformanceTest(RuleBasedStateMachine): def __init__(self): super().__init__() @initialize(random=st.randoms()) def setup(self, random): if Provider.lifetime == "test_case": data = ConjectureData(random=random, provider=Provider) self.provider = data.provider else: self.provider = Provider(None) self.context_manager = self.provider.per_test_case_context_manager() self.context_manager.__enter__() self.frozen = False ``` -------------------------------- ### Registering CI and Development Profiles Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Demonstrates how to register CI and development profiles using Hypothesis settings, combining local and shared databases. ```python settings.register_profile("ci", database=shared) settings.register_profile( "dev", database=MultiplexedDatabase(local, ReadOnlyDatabase(shared)) ) settings.load_profile("ci" if os.environ.get("CI") else "dev") ``` -------------------------------- ### Redis Example Database Configuration Source: https://hypothesis.readthedocs.io/en/latest/reference/api.html Configuration for RedisExampleDatabase, storing Hypothesis examples as sets in Redis. It includes parameters for expiration, key prefix, and a listener channel for changes. ```python RedisExampleDatabase( _redis_ , _*_ , _expire_after =datetime.timedelta(days=8)_, _key_prefix =b'hypothesis-example:'_, _listener_channel ='hypothesis-changes'_, ) ``` -------------------------------- ### Example usage of @example with assume Source: https://hypothesis.readthedocs.io/en/latest/_sources/changelog.rst.txt Demonstrates how @example interacts with assume, showing a test that previously failed but now passes due to improved handling of assumptions. ```python @given(value=floats(0, 1)) @example(value=0.56789) # used to make the test fail! @pytest.mark.parametrize("threshold", [0.5, 1]) def test_foo(threshold, value): assume(value < threshold) ... ``` -------------------------------- ### Verify Settings Initialization Arguments Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/_settings.html Asserts that the keyword-only arguments to settings.__init__ match the defined settings. This check helps maintain consistency when adding or removing settings. ```python assert set(all_settings) == { p.name for p in inspect.signature(settings.__init__).parameters.values() if p.kind == inspect.Parameter.KEYWORD_ONLY } ``` -------------------------------- ### Execute Explicit Examples with Validation Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html Iterates through explicit examples provided via @example(), validating them against the arguments expected by @given() and executing them if the 'explicit' phase is enabled. ```python assert isinstance(state, StateForActualGivenExecution) posargs = [ p.name for p in original_sig.parameters.values() if p.kind is p.POSITIONAL_OR_KEYWORD ] for example in reversed(getattr(wrapped_test, "hypothesis_explicit_examples", ())): assert isinstance(example, Example) # All of this validation is to check that @example() got "the same" arguments # as @given, i.e. corresponding to the same parameters, even though they might # be any mixture of positional and keyword arguments. if example.args: assert not example.kwargs if any( p.kind is p.POSITIONAL_ONLY for p in original_sig.parameters.values() ): raise InvalidArgument( "Cannot pass positional arguments to @example() when decorating " "a test function which has positional-only parameters." ) if len(example.args) > len(posargs): raise InvalidArgument( "example has too many arguments for test. Expected at most " f"{len(posargs)} but got {len(example.args)}" ) example_kwargs = dict( zip(posargs[-len(example.args) :], example.args, strict=True) ) else: example_kwargs = dict(example.kwargs) given_kws = ", ".join( repr(k) for k in sorted(wrapped_test.hypothesis._given_kwargs) ) example_kws = ", ".join(repr(k) for k in sorted(example_kwargs)) if given_kws != example_kws: raise InvalidArgument( f"Inconsistent args: @given() got strategies for {given_kws}, " f"but @example() got arguments for {example_kws}" ) from None # This is certainly true because the example_kwargs exactly match the params # reserved by @given(), which are then remove from the function signature. assert set(example_kwargs).isdisjoint(kwargs) example_kwargs.update(kwargs) if Phase.explicit not in state.settings.phases: continue with local_settings(state.settings): fragments_reported = [] empty_data = ConjectureData.for_choices([]) try: execute_example = partial( state.execute_once, empty_data, is_final=True, print_example=True, example_kwargs=example_kwargs, ) with with_reporter(fragments_reported.append): if example.raises is None: execute_example() else: # @example(...).xfail(...) bits = ", ".join(nicerepr(x) for x in arguments) + ", ".join( f"{k}={nicerepr(v)}" for k, v in example_kwargs.items() ) try: execute_example() except failure_exceptions_to_catch() as err: if not isinstance(err, example.raises): raise # Save a string form of this example; we'll warn if it's # ever generated by the strategy (which can't be xfailed) state.xfail_example_reprs.add( repr_call(state.test, arguments, example_kwargs) ) except example.raises as err: # We'd usually check this as early as possible, but it's # possible for failure_exceptions_to_catch() to grow when ``` -------------------------------- ### Use Hypothesis Backend in Settings Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/internal/conjecture/providers.html Shows how to specify the 'hypothesis' backend in test settings. This is typically the default, so explicit setting is often unnecessary but demonstrates the configuration option. ```python from hypothesis import given, settings, strategies as st @given(st.integers()) @settings(backend="hypothesis") def f(n): pass ``` -------------------------------- ### Change Number of Examples with max_examples Source: https://hypothesis.readthedocs.io/en/latest/_sources/tutorial/settings.rst.txt Adjust the number of examples Hypothesis generates for a test using the `max_examples` setting. The default is 100. This example sets it to 5. ```python from hypothesis import given, settings, strategies as st @given(st.integers()) @settings(max_examples=5) def test(n): print("prints five times") ``` -------------------------------- ### Configure CI/Dev Profiles with Databases Source: https://hypothesis.readthedocs.io/en/latest/reference/api.html Registering profiles for CI and development environments using different database configurations. This example shows how to combine local and shared databases for development. ```python local = DirectoryBasedExampleDatabase(".hypothesis/examples") shared = ReadOnlyDatabase(GitHubArtifactDatabase("user", "repo")) settings.register_profile("ci", database=local) settings.register_profile("dev", database=MultiplexedDatabase(local, shared)) ``` -------------------------------- ### Initialize InMemoryExampleDatabase Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html Initializes an in-memory dictionary-based example database. This is useful for testing or short-lived sessions but does not persist data between runs. ```python def __init__(self) -> None: super().__init__() self.data: dict[bytes, set[bytes]] = {} ``` -------------------------------- ### Define a Rule-Based State Machine Source: https://hypothesis.readthedocs.io/en/latest/_sources/stateful.rst.txt This example demonstrates the basic structure of a state machine for comparing a database implementation with an in-memory model. It sets up temporary directories, initializes the database and model, and defines bundles for keys and values. ```python import shutil import tempfile from collections import defaultdict import hypothesis.strategies as st from hypothesis.database import DirectoryBasedExampleDatabase from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule class DatabaseComparison(RuleBasedStateMachine): def __init__(self): super().__init__() self.tempd = tempfile.mkdtemp() self.database = DirectoryBasedExampleDatabase(self.tempd) self.model = defaultdict(set) keys = Bundle("keys") values = Bundle("values") @rule(target=keys, k=st.binary()) def add_key(self, k): return k @rule(target=values, v=st.binary()) def add_value(self, v): return v @rule(k=keys, v=values) def save(self, k, v): self.model[k].add(v) ``` -------------------------------- ### InMemoryExampleDatabase Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/database.html A concrete implementation of `ExampleDatabase` that stores examples in an in-memory dictionary. It is not persistent between runs. ```APIDOC ## Class InMemoryExampleDatabase ### Description A non-persistent example database implemented using an in-memory dictionary. Useful for testing or multiple calls within a single session, but not for general use due to lack of persistence. ### Methods #### `__init__() -> None` Initializes the in-memory database with an empty dictionary for data storage. #### `fetch(key: bytes) -> Iterable[bytes]` Retrieves all values associated with the given key from the in-memory dictionary. Returns an empty iterable if the key is not found. #### `save(key: bytes, value: bytes) -> None` Saves a value for the given key in the in-memory dictionary. If the value is new for this key, it broadcasts a 'save' change event. #### `delete(key: bytes, value: bytes) -> None` Deletes a specific value associated with the given key from the in-memory dictionary. If the value was present and is removed, it broadcasts a 'delete' change event. ``` -------------------------------- ### Support for Expected-Failing Examples with @example().xfail() Source: https://hypothesis.readthedocs.io/en/latest/changelog.html Introduces the ability to mark examples as expected to fail, similar to pytest's xfail functionality. This can be used with optional `condition`, `reason`, and `raises` arguments. ```python @example(...).xfail() ``` ```python @example(...).xfail(condition="...", reason="...", raises=...) ``` ```python @example(...).via(...).xfail(...) ``` ```python @example(...).xfail(...).via(...) ``` -------------------------------- ### Use Positional and Keyword Arguments with @example Source: https://hypothesis.readthedocs.io/en/latest/reference/api.html Arguments to `@example` behave like arguments to `@given`, accepting either positional or keyword arguments. Unlike `@given` strategies, `@example` takes concrete values. ```python from hypothesis import given, example from hypothesis import strategies as st @example(1, 2) @example(x=1, y=2) @given(st.integers(), st.integers()) def test(x, y): pass ``` -------------------------------- ### Define Rules and Bundles for State Machines Source: https://hypothesis.readthedocs.io/en/latest/_sources/stateful.rst.txt Create a state machine with bundles for folders and files, and define rules for creating them. Initializes are used to populate bundles. ```python import hypothesis.strategies as st from hypothesis.stateful import Bundle, RuleBasedStateMachine, initialize, rule name_strategy = st.text(min_size=1).filter(lambda x: "/" not in x) class NumberModifier(RuleBasedStateMachine): folders = Bundle("folders") files = Bundle("files") @initialize(target=folders) def init_folders(self): return "/" @rule(target=folders, parent=folders, name=name_strategy) def create_folder(self, parent, name): return f"{parent}/{name}" @rule(target=files, parent=folders, name=name_strategy) def create_file(self, parent, name): return f"{parent}/{name}" ``` -------------------------------- ### Using @example to run specific inputs Source: https://hypothesis.readthedocs.io/en/latest/tutorial/replaying-failures.html Use the `@example` decorator to ensure Hypothesis always runs specific, explicit inputs for a test, in addition to randomly generated ones. These explicit examples do not shrink. ```python # two mersenne primes @example(2**17 - 1) @example(2**19 - 1) @given(st.integers()) def test_integers(n): pass test_integers() ``` ```python @example(2**17 - 1) @given(st.integers()) def test_something_with_integers(n): assert n < 100 ``` -------------------------------- ### Change Number of Examples with max_examples Source: https://hypothesis.readthedocs.io/en/latest/tutorial/settings.html Adjust the number of examples (inputs) Hypothesis generates for a test using the `max_examples` setting. This is useful for tests that are particularly expensive or cheap to run. The default is 100 examples. ```python from hypothesis import given, settings, strategies as st @given(st.integers()) @settings(max_examples=5) def test(n): print("prints five times") ``` -------------------------------- ### Redis Pub/Sub Listener Setup Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/extra/redis.html Sets up a Redis pub/sub listener for a specific channel and defines a handler for incoming messages. Ensure the Redis client is properly initialized before use. ```python self._pubsub.subscribe(**{self.listener_channel: self._handle_message}) ``` -------------------------------- ### Finalize and Observe @example Execution Source: https://hypothesis.readthedocs.io/en/latest/_modules/hypothesis/core.html This section finalizes the test case after @example() execution, freezing the data and delivering observability data if enabled. It also modifies the report text to indicate an 'explicit example'. ```python finally: if fragments_reported: assert fragments_reported[0].startswith("Falsifying example") fragments_reported[0] = fragments_reported[0].replace( "Falsifying example", "Falsifying explicit example", 1 ) empty_data.freeze() if observability_enabled(): tc = make_testcase( run_start=state._start_timestamp, property=state.test_identifier, data=empty_data, how_generated="explicit example", representation=state._string_repr, timing=state._timing_features, ) deliver_observation(tc) ``` -------------------------------- ### Failing Example Replay Source: https://hypothesis.readthedocs.io/en/latest/explanation/example-count.html Shows how Hypothesis replays a failing example an additional time after the initial failure to check for flakiness. This occurs even when only the generation phase is enabled and the minimal failing example is re-executed. ```python from hypothesis import Phase, given, settings, strategies as st @given(st.integers()) @settings(phases=[Phase.generate]) def test_function(n): print(f"called with {n}") assert n != 0 test_function() ``` -------------------------------- ### Example Hypothesis Failure Output Source: https://hypothesis.readthedocs.io/en/latest/_sources/tutorial/adding-notes.rst.txt This is an example of the default output Hypothesis provides when a test fails. ```text Falsifying example: test_a_thing(x=1, y="foo") ```