How to use hypothesis - 10 common examples

To help you get started, we’ve selected a few hypothesis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github holoviz / spatialpandas / tests / spatialindex / test_rtree.py View on Github external
@given(st_bounds_array())
def test_bounds_array_generation(bounds_array):
    assert bounds_array.shape[1] % 2 == 0
    n = bounds_array.shape[1] // 2
    for d in range(n):
        assert np.all(bounds_array[:, d + n] >= bounds_array[:, d])
github HypothesisWorks / hypothesis / tests / cover / test_health_checks.py View on Github external
    @given(st.lists(st.binary(min_size=1024, max_size=1024), average_size=100))
    @settings(database=None, buffer_size=1000)
    def test(x):
        pass
github olipratt / swagger-conformance / tests / test_custom_types.py View on Github external
        @hypothesis.settings(
            max_examples=50,
            suppress_health_check=[hypothesis.HealthCheck.too_slow])
        @hypothesis.given(put_strategy)
        def single_operation_test(client, put_operation, get_operation,
                                  put_params):
            """PUT an colour in hex, then GET it again as an int."""
            put_params['int_id'] = 1
            result = client.request(put_operation, put_params)
            assert result.status in put_operation.response_codes, \
                "{} not in {}".format(result.status,
                                      put_operation.response_codes)

            result = client.request(get_operation, {"int_id": 1})
            assert result.status in get_operation.response_codes, \
                "{} not in {}".format(result.status,
                                      get_operation.response_codes)
github biocore-ntnu / pyranges / tests / hypothesis_helper.py View on Github external
else:
    max_examples = 1000
    slow_max_examples = 100
    deadline = None

lengths = st.integers(min_value=1, max_value=int(1e7))
small_lengths = st.integers(min_value=1, max_value=int(1e4))

strands = st.sampled_from("+ -".split())
single_strand = st.sampled_from(["+"])
names = st.text("abcdefghijklmnopqrstuvxyz", min_size=1)
scores = st.integers(min_value=0, max_value=256)

datatype = st.sampled_from([pd.Series, np.array, list])

feature_data = st.sampled_from(["ensembl_gtf", "gencode_gtf", "ucsc_bed"])

chromosomes = st.sampled_from(
    ["chr{}".format(str(e)) for e in list(range(1, 23)) + "X Y M".split()])
chromosomes_small = st.sampled_from(["chr1"])
cs = st.one_of(chromosomes, chromosomes_small)

runlengths = data_frames(
    index=indexes(dtype=np.int64, min_size=1, unique=True),
    columns=[
        column("Runs", st.integers(min_value=1, max_value=int(1e7))),
        # must have a min/max on floats because R S4vectors translates too big ones into inf.
        # which is unequal to eg -1.79769e+308 so the tests fail
        column("Values", st.integers(min_value=-int(1e7), max_value=int(1e7)))
    ])

better_dfs_no_min = data_frames(
github HypothesisWorks / hypothesis / tests / quality / test_discovery_ability.py View on Github external
# This strategy tests interactions with `filter()`.  It generates the even
# integers {0, 2, 4, 6} in equal measures.
one_of_nested_strategy_with_filter = one_of(
    just(0),
    just(1),
    one_of(
        just(2),
        just(3),
        one_of(
            just(4),
            just(5),
            one_of(
                just(6),
                just(7),
            )
        )
    )
).filter(lambda x: x % 2 == 0)

for i in range(4):
    exec('''test_one_of_flattens_filter_branches_%d = define_test(
        one_of_nested_strategy_with_filter, lambda x: x == 2 * %d
    )''' % (i, i))
github rocky / python-decompile3 / pytest / test_fstring.py View on Github external
def run_test(text):
    hypothesis.assume(len(text))
    hypothesis.assume("f'{" in text)
    expr = text + "\n"
    code = compile(expr, "", "single")
    deparsed = code_deparse(code, sys.stdout, PYTHON_VERSION, compile_mode="single")
    recompiled = compile(deparsed.text, "", "single")
    if recompiled != code:
        print(recompiled)
        print("================")
        print(code)
        print("----------------")
        assert (
            "dis(" + deparsed.text.strip("\n") + ")" == "dis(" + expr.strip("\n") + ")"
        )
github pyta-uoft / pyta / tests / custom_hypothesis_support.py View on Github external
@hs.composite
def ifexp_node(draw, test=const_node(hs.booleans()),
               expr=const_node(), orelse=const_node()):
    # TODO: Add an option for whether expr and orelse strategies produce the same type.
    test = draw(test)
    expr = draw(expr)
    node = astroid.IfExp()
    node.postinit(test, expr, expr)
    return node
github PokeAPI / pokebase / tests / test_module_interface.py View on Github external
    @given(endpoint=text())
    @patch('pokebase.interface.get_data')
    def testArg_endpoint_Text(self, mock_get_data, endpoint):

        mock_get_data.side_effect = ValueError()

        with self.assertRaises(ValueError):
            interface.APIResourceList(endpoint)
github HypothesisWorks / hypothesis / tests / cover / test_testdecorators.py View on Github external
@given(builds(Litter, integers(), integers()))
def test_named_tuples_are_of_right_type(litter):
    assert isinstance(litter, Litter)
github python-attrs / attrs / tests / test_dunders.py View on Github external
    @given(booleans())
    def test_hash_mirrors_eq(self, eq):
        """
        If `hash` is None, the hash generation mirrors `eq`.
        """
        C = make_class("C", {"a": attr.ib()}, eq=eq, frozen=True)

        i = C(1)

        assert i == i
        assert hash(i) == hash(i)

        if eq:
            assert C(1) == C(1)
            assert hash(C(1)) == hash(C(1))
        else:
            assert C(1) != C(1)