Python hypothesis.settings() Examples
The following are 30
code examples of hypothesis.settings().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
hypothesis
, or try the search function
.
Example #1
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_nested_point_user_strategy(): limit_dict = { 'p1': { 'x': range(0xe0,0xf0), }, 'p2': pst.bitstructs( Point2D, {'x':range(0,2),'y':range(2,4)} ) } print("") @hypothesis.given( bs = pst.bitstructs(NestedPoint, limit_dict) ) @hypothesis.settings( max_examples=16 ) def actual_test( bs ): assert isinstance( bs, NestedPoint ) assert 0xe0 <= bs.p1.x <= 0xef assert 0 <= bs.p2.x < 2 assert 2 <= bs.p2.y < 4 print( bs ) actual_test()
Example #2
Source File: concat_process_test.py From pypeln with MIT License | 6 votes |
def test_concat_basic(nums: tp.List[int]): nums_py = list(map(lambda x: x + 1, nums)) nums_py1 = list(map(lambda x: x ** 2, nums_py)) nums_py2 = list(map(lambda x: -x, nums_py)) nums_py = nums_py1 + nums_py2 nums_pl = pl.process.map(lambda x: x + 1, nums) nums_pl1 = pl.process.map(lambda x: x ** 2, nums_pl) nums_pl2 = pl.process.map(lambda x: -x, nums_pl) nums_pl = pl.process.concat([nums_pl1, nums_pl2]) assert sorted(nums_pl) == sorted(nums_py) # @hp.given(nums=st.lists(st.integers())) # @hp.settings(max_examples=MAX_EXAMPLES)
Example #3
Source File: concat_task_test.py From pypeln with MIT License | 6 votes |
def test_concat_basic_2(nums: tp.List[int]): nums_py = list(map(lambda x: x + 1, nums)) nums_py1 = list(map(lambda x: x ** 2, nums_py)) nums_py2 = list(map(lambda x: -x, nums_py)) nums_py = nums_py1 + nums_py2 nums_pl = pl.task.map(lambda x: x + 1, nums) nums_pl1 = pl.task.map(lambda x: x ** 2, nums_pl) nums_pl2 = pl.task.map(lambda x: -x, nums_pl) nums_pl = await pl.task.concat([nums_pl1, nums_pl2]) assert sorted(nums_pl) == sorted(nums_py) # @hp.given(nums=st.lists(st.integers())) # @hp.settings(max_examples=MAX_EXAMPLES)
Example #4
Source File: core.py From schemathesis with MIT License | 6 votes |
def _run_tests( # pylint: disable=too-many-arguments self, maker: Callable, template: Callable, settings: hypothesis.settings, seed: Optional[int], recursion_level: int = 0, **kwargs: Any, ) -> Generator[events.ExecutionEvent, None, None]: """Run tests and recursively run additional tests.""" if recursion_level > self.stateful_recursion_limit: return for endpoint, test in maker(template, settings, seed): feedback = Feedback(self.stateful, endpoint) for event in run_test(endpoint, test, feedback=feedback, recursion_level=recursion_level, **kwargs): yield event if isinstance(event, events.Interrupted): return # Additional tests, generated via the `feedback` instance yield from self._run_tests( feedback.get_stateful_tests, template, settings, seed, recursion_level=recursion_level + 1, **kwargs )
Example #5
Source File: _hypothesis.py From schemathesis with MIT License | 6 votes |
def get_single_example(strategy: st.SearchStrategy[Case]) -> Case: @hypothesis.given(strategy) # type: ignore @hypothesis.settings( # type: ignore database=None, max_examples=1, deadline=None, verbosity=hypothesis.Verbosity.quiet, phases=(hypothesis.Phase.generate,), suppress_health_check=hypothesis.HealthCheck.all(), ) def example_generating_inner_function(ex: Case) -> None: examples.append(ex) examples: List[Case] = [] example_generating_inner_function() return examples[0]
Example #6
Source File: concat_thread_test.py From pypeln with MIT License | 6 votes |
def test_concat_basic(nums: tp.List[int]): nums_py = list(map(lambda x: x + 1, nums)) nums_py1 = list(map(lambda x: x ** 2, nums_py)) nums_py2 = list(map(lambda x: -x, nums_py)) nums_py = nums_py1 + nums_py2 nums_pl = pl.thread.map(lambda x: x + 1, nums) nums_pl1 = pl.thread.map(lambda x: x ** 2, nums_pl) nums_pl2 = pl.thread.map(lambda x: -x, nums_pl) nums_pl = pl.thread.concat([nums_pl1, nums_pl2]) assert sorted(nums_pl) == sorted(nums_py) # @hp.given(nums=st.lists(st.integers())) # @hp.settings(max_examples=MAX_EXAMPLES)
Example #7
Source File: threadpool.py From schemathesis with MIT License | 6 votes |
def _get_worker_kwargs(self, tasks_queue: Queue, events_queue: Queue, results: TestResultSet) -> Dict[str, Any]: return { "tasks_queue": tasks_queue, "events_queue": events_queue, "checks": self.checks, "targets": self.targets, "settings": self.hypothesis_settings, "seed": self.seed, "results": results, "stateful": self.stateful, "stateful_recursion_limit": self.stateful_recursion_limit, "kwargs": { "auth": self.auth, "auth_type": self.auth_type, "headers": self.headers, "store_interactions": self.store_interactions, }, }
Example #8
Source File: test_geometry.py From nxviz with MIT License | 6 votes |
def test_circos_radius(): """ Check radius correctness. Uses the other triangle geometry rule to check that the radius is correct. """ n_nodes = 10 node_r = 1 A = 2 * np.pi / n_nodes # noqa circ_r = 2 * node_r / np.sqrt(2 * (1 - np.cos(A))) assert np.allclose(circ_r, circos_radius(n_nodes, node_r)) # @settings(perform_health_check=False)
Example #9
Source File: test_polcart.py From nxviz with MIT License | 6 votes |
def test_convert_xy(x, y): """Test for conversion of cartesian to polar.""" assume(x != 0 and y != 0) assume(abs(x) > 0.01 and abs(y) > 0.01) # Test radians r, theta = to_polar(x, y) x_new, y_new = to_cartesian(r, theta) assert np.allclose(x, x_new) assert np.allclose(y, y_new) # Test degrees r, theta = to_polar(x, y, theta_units="degrees") x_new, y_new = to_cartesian(r, theta, theta_units="degrees") assert np.allclose(x, x_new) assert np.allclose(y, y_new) # @settings(perform_health_check=False)
Example #10
Source File: stateful.py From brownie with MIT License | 6 votes |
def state_machine( rules_object: type, *args: Any, settings: Optional[dict] = None, **kwargs: Any ) -> None: machine = _generate_state_machine(rules_object) if hasattr(rules_object, "__init__"): # __init__ is treated as a class method rules_object.__init__(machine, *args, **kwargs) # type: ignore brownie.rpc.snapshot() try: sf.run_state_machine_as_test(lambda: machine(), settings=hp_settings(**settings or {})) finally: if hasattr(machine, "teardown_final"): # teardown_final is also a class method machine.teardown_final(machine) # type: ignore
Example #11
Source File: test_wsgi.py From schemathesis with MIT License | 6 votes |
def test_app_with_parametrize(testdir): # Regression - missed argument inside "wrapper" in `BaseSchema.parametrize` testdir.makepyfile( """ import schemathesis from test.apps._flask.app import app from hypothesis import settings schema = schemathesis.from_wsgi("/schema.yaml", app) called = False @schema.parametrize() @settings(max_examples=1) def test(case): global called called = True assert case.endpoint.schema.app is app def test_two(): assert called """ ) result = testdir.runpytest() result.assert_outcomes(passed=3)
Example #12
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_nested_point_limited(): limit_dict = { 'p1': { 'x': range(0xe0,0xf0), }, 'p2': { 'y': range(0xf0,0x100), } } print("") @hypothesis.given( bs = pst.bitstructs(NestedPoint, limit_dict) ) @hypothesis.settings( max_examples=16 ) def actual_test( bs ): assert isinstance( bs, NestedPoint ) assert 0xe0 <= bs.p1.x <= 0xef assert 0xf0 <= bs.p2.y <= 0xff print( bs ) actual_test()
Example #13
Source File: test_hooks.py From schemathesis with MIT License | 6 votes |
def test_multiple_hooks_per_spec(schema): @schema.hooks.register("before_generate_query") def first_hook(context, strategy): return strategy.filter(lambda x: x["id"].isdigit()) @schema.hooks.register("before_generate_query") def second_hook(context, strategy): return strategy.filter(lambda x: int(x["id"]) % 2 == 0) assert schema.hooks.get_all_by_name("before_generate_query") == [first_hook, second_hook] strategy = schema.endpoints["/custom_format"]["GET"].as_strategy() @given(case=strategy) @settings(max_examples=3) def test(case): assert case.query["id"].isdigit() assert int(case.query["id"]) % 2 == 0 test()
Example #14
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_bitslist_nested_user_strategy(): type_ = [ [Bits10, Bits11, Bits12], Bits13 ] limit_dict = { 1: pst.bits(13, min_value=0, max_value=10) } print("") @hypothesis.given( blist = pst.bitslists(type_, limit_dict) ) @hypothesis.settings( max_examples=16 ) def actual_test( blist ): assert blist[0][0].nbits == 10 assert blist[0][1].nbits == 11 assert blist[0][2].nbits == 12 assert blist[1].nbits == 13 assert 0 <= blist[1] <= 10 print(blist) actual_test()
Example #15
Source File: test_fixups.py From schemathesis with MIT License | 6 votes |
def test_global_fixup(testdir, fast_api_schema): # When all fixups are enabled globally testdir.makepyfile( """ import schemathesis from hypothesis import settings schemathesis.fixups.install() schema = schemathesis.from_dict({schema}) def teardown_module(module): schemathesis.fixups.uninstall() assert schemathesis.hooks.get_all_by_name("before_load_schema") == [] @schema.parametrize() @settings(max_examples=1) def test(case): assert 0 < case.query["value"] < 10 """.format( schema=fast_api_schema ), ) # Then Fast API schemas that are not compliant should be processed result = testdir.runpytest("-s") result.assert_outcomes(passed=1)
Example #16
Source File: test_hypothesis.py From schemathesis with MIT License | 6 votes |
def test_invalid_body_in_get_disable_validation(simple_schema): schema = schemathesis.from_dict(simple_schema, validate_schema=False) endpoint = Endpoint( path="/foo", method="GET", definition=EndpointDefinition({}, {}, "foo"), schema=schema, body={"required": ["foo"], "type": "object", "properties": {"foo": {"type": "string"}}}, ) strategy = get_case_strategy(endpoint) @given(strategy) @settings(max_examples=1) def test(case): assert case.body is not None test()
Example #17
Source File: test_parameters.py From schemathesis with MIT License | 6 votes |
def test_body(testdir): # When parameter is specified for "body" testdir.make_test( """ @schema.parametrize(method="POST") @settings(max_examples=3, deadline=None) def test_(case): assert_int(case.body) assert_requests_call(case) """, paths={ "/users": { "post": { "parameters": [{"name": "id", "in": "body", "required": True, "schema": {"type": "integer"}}], "responses": {"200": {"description": "OK"}}, } } }, ) # Then the generated test case should contain it in its `body` attribute testdir.run_and_assert(passed=1)
Example #18
Source File: test_parameters.py From schemathesis with MIT License | 6 votes |
def test_path(testdir): # When parameter is specified for "path" testdir.make_test( """ @schema.parametrize(endpoint="/users/{user_id}") @settings(max_examples=3, deadline=None) def test_(case): assert_int(case.path_parameters["user_id"]) assert_requests_call(case) """, paths={ "/users/{user_id}": { "get": { "parameters": [{"name": "user_id", "required": True, "in": "path", "type": "integer"}], "responses": {"200": {"description": "OK"}}, } } }, ) # Then the generated test case should contain it its `path_parameters` attribute testdir.run_and_assert(passed=1)
Example #19
Source File: test_parameters.py From schemathesis with MIT License | 6 votes |
def test_security_definitions_api_key(testdir, schema, location): # When schema contains "apiKeySecurity" security definition # And it is in query or header location = "headers" if location == "header" else location testdir.make_test( f""" @schema.parametrize() @settings(max_examples=1, deadline=None) def test_(case): assert_str(case.{location}["api_key"]) assert_requests_call(case) """, schema=schema, ) # Then the generated test case should contain API key in a proper place testdir.run_and_assert(passed=1)
Example #20
Source File: test_parameters.py From schemathesis with MIT License | 6 votes |
def test_security_definitions_api_key_cookie(testdir, simple_openapi): # When schema contains "apiKeySecurity" security definition # And it is in cookie schema = deepcopy(simple_openapi) components = schema.setdefault("components", {}) components["securitySchemes"] = {"api_key": {"type": "apiKey", "name": "api_key", "in": "cookie"}} schema["security"] = [{"api_key": []}] testdir.make_test( """ @schema.parametrize() @settings(max_examples=1, deadline=None) def test_(case): assert_str(case.cookies["api_key"]) assert_requests_call(case) """, schema=schema, ) # Then the generated test case should contain API key in a proper place testdir.run_and_assert(passed=1)
Example #21
Source File: test_parameters.py From schemathesis with MIT License | 6 votes |
def test_security_definitions_basic_auth(testdir, basic_auth_schema): # When schema is using HTTP Basic Auth testdir.make_test( """ import base64 @schema.parametrize() @settings(max_examples=1, deadline=None) def test_(case): assert "Authorization" in case.headers auth = case.headers["Authorization"] assert auth.startswith("Basic ") assert isinstance(base64.b64decode(auth[6:]), bytes) assert_requests_call(case) """, schema=basic_auth_schema, ) # Then the generated data should contain a valid "Authorization" header testdir.run_and_assert(passed=1)
Example #22
Source File: test_geometry.py From nxviz with MIT License | 5 votes |
def test_get_cartesian(r, theta): """ Test for get_cartesian. Makes sure that `get_cartesian` remains a wrapper around polcart's `to_cartesian`. """ assume(np.isfinite(theta)) assume(np.isfinite(r)) assert get_cartesian(r, theta) == polcart.to_cartesian(r, theta) # @settings(perform_health_check=False)
Example #23
Source File: _testing.py From deal with MIT License | 5 votes |
def get_examples(func: typing.Callable, kwargs: typing.Dict[str, typing.Any], count: int) -> typing.List[ArgsKwargsType]: kwargs = kwargs.copy() for name, value in kwargs.items(): if isinstance(value, hypothesis.strategies.SearchStrategy): continue kwargs[name] = hypothesis.strategies.just(value) def pass_along_variables(*args, **kwargs) -> ArgsKwargsType: return args, kwargs pass_along_variables.__signature__ = signature(func) # type: ignore pass_along_variables.__annotations__ = getattr(func, '__annotations__', {}) strategy = hypothesis.strategies.builds(pass_along_variables, **kwargs) examples = [] @hypothesis.given(strategy) @hypothesis.settings( database=None, max_examples=count, deadline=None, verbosity=hypothesis.Verbosity.quiet, phases=(hypothesis.Phase.generate,), suppress_health_check=hypothesis.HealthCheck.all(), ) def example_generator(ex: ArgsKwargsType) -> None: examples.append(ex) example_generator() # pylint: disable=no-value-for-parameter return examples
Example #24
Source File: ChecksumCL_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def cksum_func( s, words ): return checksum_cl( words ) # ''' TUTORIAL TASK '''''''''''''''''''''''''''''''''''''''''''''''''''' # Use Hypothesis to test Checksum CL # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\/ #; Use Hypothesis to verify that ChecksumCL has the same behavior as #; ChecksumFL. Simply uncomment the following test_hypothesis method #; and rerun pytest. Make sure that you fix the indentation so that #; this new test_hypothesis method is correctly indented with respect #; to the class ChecksumCL_Tests #; #; @hypothesis.settings( deadline=None ) #; @hypothesis.given( #; words=st.lists( pm_st.bits(16), min_size=8, max_size=8 ) #; ) #; def test_hypothesis( s, words ): #; print( [ int(x) for x in words ] ) #; assert s.cksum_func( words ) == checksum( words ) #; #; This new test uses Hypothesis to generate random inputs, then uses #; the checksum_cl to run a little simulation and compares the output to #; the checksum function from ChecksumFL. #; #; To really see Hypothesis in action, go back to ChecksumCL and #; corrupt one word of the input by forcing it to always be zero. For #; example, change the update block in the CL implementation to be #; something like this: #; #; @update #; def up_checksum_cl(): #; if s.pipe.enq.rdy() and s.in_q.deq.rdy(): #; bits = s.in_q.deq() #; words = b128_to_words( bits ) #; words[5] = b16(0) # <--- INJECT A BUG! #; result = checksum( words ) #; s.pipe.enq( result ) !\vspace{0.07in}! #; if s.send.rdy() and s.pipe.deq.rdy(): #; s.send( s.pipe.deq() )
Example #25
Source File: test_swaggerconformance.py From swagger-conformance with MIT License | 5 votes |
def test_get_resp(self): """Test that a GET URL parameter is the same when passed back in the GET response body - i.e. there's no mismatched encode/decode.""" url_base = SCHEMA_URL_BASE + '/example/' def _request_callback(request): value = request.url[len(url_base):] # Special characters will be quoted in the URL - unquote them here. value = urllib.parse.unquote_plus(value) return (200, {}, json.dumps({'in_str': value})) responses.add_callback(responses.GET, re.compile(url_base), callback=_request_callback, content_type=CONTENT_TYPE_JSON) my_val_factory = swaggerconformance.strategies.StrategyFactory() client = swaggerconformance.client.Client(MIRROR_REQS_SCHEMA_PATH) operation = client.api.endpoints["/example/{in_str}"]["get"] strategy = operation.parameters_strategy(my_val_factory) @hypothesis.settings( max_examples=200, suppress_health_check=[hypothesis.HealthCheck.too_slow]) @hypothesis.given(strategy) def _single_operation_test(client, operation, params): result = client.request(operation, params) assert result.status in operation.response_codes, \ "{} not in {}".format(result.status, operation.response_codes) assert result.body.in_str == params["in_str"], \ "{} != {}".format(result.body.in_str, params["in_str"]) _single_operation_test(client, operation) # pylint: disable=I0011,E1120
Example #26
Source File: test_group_method.py From gatorgrouper with GNU General Public License v3.0 | 5 votes |
def test_group_random_extra(): """Testing the random type of grouping with a group of extra people not assigned to their own group""" responses = [ ["Nick", True, False, True, False], ["Marvin", False, False, True, True], ["Evin", True, True, True, False], ["Nikki", True, True, False, False], ["Dan", False, True, False, True], ] num_group = 2 returned_groups1 = group_creation.group_random_num_group(responses, num_group) assert len(returned_groups1) == 2 assert num_group == 2 # Test uses now deleted group_size dependent functions, must be rewritten or deleted # @given(grpsize=integers(min_value=1, max_value=3)) # @settings(verbosity=Verbosity.verbose) # @pytest.mark.hypothesisworks # def test_group_random2(grpsize): # """This hypothesis test will test the group_random_group_size method""" # responses = [ # ["Nick", True, False, True, False], # ["Marvin", False, False, True, True], # ["Evin", True, True, True, False], # ["Nikki", True, True, False, False], # ["Nick", True, False, True, False], # ["Dan", False, True, False, True], # ] # returned_groups = group_creation.group_random_group_size(responses, grpsize) # size_count = grpsize # assert len(returned_groups[0]) == size_count
Example #27
Source File: test_checks.py From schemathesis with MIT License | 5 votes |
def test_response_schema_conformance_references_invalid(complex_schema): schema = schemathesis.from_path(complex_schema) @given(case=schema.endpoints["/teapot"]["POST"].as_strategy()) @settings(max_examples=3) def test(case): response = make_response(json.dumps({"foo": 1}).encode()) with pytest.raises(AssertionError): case.validate_response(response) test()
Example #28
Source File: schemas.py From schemathesis with MIT License | 5 votes |
def get_all_tests( self, func: Callable, settings: Optional[hypothesis.settings] = None, seed: Optional[int] = None ) -> Generator[Tuple[Endpoint, Union[Callable, InvalidSchema]], None, None]: """Generate all endpoints and Hypothesis tests for them.""" test: Union[Callable, InvalidSchema] for endpoint in self.get_all_endpoints(): test = make_test_or_exception(endpoint, func, settings, seed) yield endpoint, test
Example #29
Source File: test_basic.py From schemathesis with MIT License | 5 votes |
def test_query_strategy(graphql_schema): strategy = graphql_schema.query.as_strategy() @given(case=strategy) @settings(max_examples=10) def test(case): response = case.call() assert response.status_code < 500 test()
Example #30
Source File: test_commands.py From schemathesis with MIT License | 5 votes |
def test_hypothesis_parameters(cli, schema_url): # When Hypothesis options are passed via command line result = cli.run( schema_url, "--hypothesis-deadline=1000", "--hypothesis-derandomize", "--hypothesis-max-examples=1000", "--hypothesis-phases=explicit,generate", "--hypothesis-report-multiple-bugs=0", "--hypothesis-suppress-health-check=too_slow,filter_too_much", "--hypothesis-verbosity=normal", ) # Then they should be correctly converted into arguments accepted by `hypothesis.settings` # Parameters are validated in `hypothesis.settings` assert result.exit_code == ExitCode.OK, result.stdout