Python hypothesis.given() Examples
The following are 30
code examples of hypothesis.given().
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
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_hypothesis.py From schemathesis with MIT License | 7 votes |
def test_valid_headers(base_url, swagger_20, definition): endpoint = Endpoint( "/api/success", "GET", definition=EndpointDefinition({}, {}, "foo"), schema=swagger_20, base_url=base_url, headers={ "properties": {"api_key": definition}, "additionalProperties": False, "type": "object", "required": ["api_key"], }, ) @given(case=get_case_strategy(endpoint)) @settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], deadline=None, max_examples=10) def inner(case): case.call() inner()
Example #2
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_conflicting_inferred_type_variable(): """ User calls two functions on an object, which contradicts the inferred type of the variable. """ skip('Skipping this test until error messages are fixed') program = ''' def return_num(num: int) -> int: return num def return_str(str: str) -> str: return str def f(x): return_num(x) return_str(x) ''' try: module, inferer = cs._parse_text(program) except: skip() call_node = list(module.nodes_of_class(astroid.Call))[1] expected_msg = f'In the Call node in line 8, there was an error in calling the annotated function "return_str":\n' \ f'in parameter (1), the annotated type is str but was given an object of inferred type int.' # TODO: test case redundant because recursive..? assert call_node.inf_type.getValue() == expected_msg
Example #3
Source File: test_subscript.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_subscript_slice(): program = ''' x = List[:] ''' module, _ = cs._parse_text(program) assign_node = next(module.nodes_of_class(astroid.Assign)) assert isinstance(assign_node.inf_type, TypeFail) # TODO: this test needs to be converted, but will also fail # @given(cs.random_list(min_size=2), cs.random_slice_indices()) # def test_subscript_heterogeneous_list_slice(input_list, slice): # """Test visitor of Subscript node representing slicing of heterogeneous list.""" # assume(not isinstance(input_list[0], type(input_list[1]))) # input_slice = ':'.join([str(index) if index else '' for index in slice]) # program = f'{input_list}[{input_slice}]' # module, _ = cs._parse_text(program) # subscript_node = list(module.nodes_of_class(astroid.Subscript))[0] # assert subscript_node.inf_type.getValue() == List[Any]
Example #4
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 #5
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 #6
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_non_annotated_function_call_bad_arguments(): """ User tries to call a non-annotated function on arguments of the wrong type. """ program = f'def add_num(num1, num2):\n' \ f' return num1 + num2\n' \ f'\n' \ f'add_num("bob", 1.0)\n' try: module, inferer = cs._parse_text(program) except: skip() call_node = next(module.nodes_of_class(astroid.Call)) # TODO: This error is flawed because the unification error occurs for both arguments due to our current implementation, # which "chooses" the first valid function type from TypeStore. # Should we fix this implementation first or save it for later and hard-code the correct error message for now? expected_msg = f'In the Call node in line 4, there was an error in calling the function "add_num":\n' \ f'in parameter (1), the function was expecting an object of inferred type ' \ f'int but was given an object of type str.\n' \ f'in parameter (2), the function was expecting an object of inferred type ' \ f'int but was given an object of type float.\n' # TODO: should we use the term inferred? assert call_node.inf_type.getValue() == expected_msg
Example #7
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 #8
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_user_defined_annotated_call_wrong_arguments_type(): """ User tries to call an annotated user-defined function on the wrongly-typed arguments. """ program = f'def add_3(num1: int, num2: int, num3: int) -> int:\n' \ f' return num1 + num2 + num3\n' \ f'\n' \ f'add_3(1, "bob", 1.0)\n' try: module, inferer = cs._parse_text(program) except: skip() call_node = list(module.nodes_of_class(astroid.Call))[0] expected_msg = f'In the Call node in line 4, there was an error in calling the annotated function "add_3":\n' \ f'in parameter (2), the annotated type is int but was given an object of type str.\n' \ f'in parameter (3), the annotated type is int but was given an object of type float.\n' assert call_node.inf_type.getValue() == expected_msg
Example #9
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_conflicting_inferred_type_variable(): """ User calls two functions on an object, which contradicts the inferred type of the variable. """ program = ''' def return_num(num: int) -> int: return num def return_str(str: str) -> str: return str def f(x): return_num(x) return_str(x) ''' try: module, inferer = cs._parse_text(program) except: skip() call_node = list(module.nodes_of_class(astroid.Call))[1] expected_msg = f'In the Call node in line 8, there was an error in calling the annotated function "return_str":\n' \ f'in parameter (1), the annotated type is str but was given an object of inferred type int.' # TODO: test case redundant because recursive..? assert call_node.inf_type.getValue() == expected_msg
Example #10
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_non_annotated_function_call_bad_arguments(): """ User tries to call a non-annotated function on arguments of the wrong type. """ skip('Skipping this test until error messages are fixed') program = f'def add_num(num1, num2):\n' \ f' return num1 + num2\n' \ f'\n' \ f'add_num("bob", 1.0)\n' try: module, inferer = cs._parse_text(program) except: skip() call_node = next(module.nodes_of_class(astroid.Call)) # TODO: This error is flawed because the unification error occurs for both arguments due to our current implementation, # which "chooses" the first valid function type from TypeStore. # Should we fix this implementation first or save it for later and hard-code the correct error message for now? expected_msg = f'In the Call node in line 4, there was an error in calling the function "add_num":\n' \ f'in parameter (1), the function was expecting an object of inferred type ' \ f'int but was given an object of type str.\n' \ f'in parameter (2), the function was expecting an object of inferred type ' \ f'int but was given an object of type float.\n' # TODO: should we use the term inferred? assert call_node.inf_type.getValue() == expected_msg
Example #11
Source File: test_functions.py From pyta with GNU General Public License v3.0 | 6 votes |
def test_user_defined_annotated_call_wrong_arguments_type(): """ User tries to call an annotated user-defined function on the wrongly-typed arguments. """ skip('Skipping this test until error messages are fixed') program = f'def add_3(num1: int, num2: int, num3: int) -> int:\n' \ f' return num1 + num2 + num3\n' \ f'\n' \ f'add_3(1, "bob", 1.0)\n' try: module, inferer = cs._parse_text(program) except: skip() call_node = list(module.nodes_of_class(astroid.Call))[0] expected_msg = f'In the Call node in line 4, there was an error in calling the annotated function "add_3":\n' \ f'in parameter (2), the annotated type is int but was given an object of type str.\n' \ f'in parameter (3), the annotated type is int but was given an object of type float.\n' assert call_node.inf_type.getValue() == expected_msg
Example #12
Source File: test_model.py From flocker with Apache License 2.0 | 6 votes |
def test_manifestations_from_applications(self): """ One cannot construct a ``Node`` where there are manifestations on the ``applications`` attribute that aren't also in the given ``manifestations``. """ m1 = Manifestation(dataset=Dataset(dataset_id=unicode(uuid4())), primary=True) self.assertRaises( InvariantException, Node, hostname=u'node1.example.com', applications={a.name: a for a in [ APP1, Application(name=u'a', image=DockerImage.from_string(u'x'), volume=AttachedVolume( manifestation=m1, mountpoint=FilePath(b"/xxx"))), ]})
Example #13
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 #14
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 #15
Source File: test_model.py From flocker with Apache License 2.0 | 6 votes |
def assert_required_field_set(self, **fields): """ Assert that if one of the given field names is set on a ``NodeState``, all of them must be set or this will be consideted an invariant violation. :param fields: ``NodeState`` attributes that are all expected to be settable as a group, but which cannot be missing if one of the others is set. """ # If all are set, no problems: NodeState(hostname=u"127.0.0.1", uuid=uuid4(), **fields) # If one is missing, an invariant is raised: for name in fields: remaining_fields = fields.copy() del remaining_fields[name] self.assertRaises(InvariantException, NodeState, hostname=u"127.0.0.1", uuid=uuid4(), **remaining_fields)
Example #16
Source File: tough-bonus-problems.py From escape-from-automanual-testing with GNU Affero General Public License v3.0 | 6 votes |
def from_schema(schema): """Returns a strategy for objects that match the given schema.""" check_schema(schema) # TODO: actually handle constraints on number/string/array schemas return dict( null=st.none(), bool=st.booleans(), number=st.floats(allow_nan=False), string=st.text(), array=st.lists(st.nothing()), )[schema["type"]] # `@st.composite` is one way to write this - another would be to define a # bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so # each strategy can be defined individually. Use whichever seems more # natural to you - the important thing in tests is usually readability!
Example #17
Source File: test_model.py From flocker with Apache License 2.0 | 6 votes |
def test_update_node_new(self): """ When doing ``update_node()``, if the given ``Node`` has hostname not in existing ``Deployment`` then just add new ``Node`` to new ``Deployment``. """ node = Node( hostname=u"node1.example.com", applications={ u'postgresql-clusterhq': Application( name=u'postgresql-clusterhq', image=DockerImage.from_string(u"image"))}) another_node = Node( hostname=u"node2.example.com", applications={ u'site-clusterhq.com': Application( name=u'site-clusterhq.com', image=DockerImage.from_string(u"image"))}, ) original = Deployment(nodes=frozenset([node])) updated = original.update_node(another_node) self.assertEqual((original, updated), (Deployment(nodes=frozenset([node])), Deployment(nodes=frozenset([node, another_node]))))
Example #18
Source File: test_mdk.py From mdk with Apache License 2.0 | 6 votes |
def test_log_result_too_low_level(self): """ A LoggedMessageId matching the logged message is returned by logging APIs even when the given level is low enough that a message wasn't sent to the MCP. """ mdk, tracer = create_mdk_with_faketracer() session = mdk.session() session.info("cat", "message") lmid = session.debug("cat", "another message") lmid2 = session.info("cat", "message") # Debug message wasn't set: self.assertEqual([d["level"] for d in tracer.messages], ["INFO", "INFO"]) # But we still got LoggedMessageId for debug message: self.assertEqual((lmid.causalLevel, lmid.traceId, lmid2.causalLevel, lmid2.traceId), ([2], session._context.traceId, [3], session._context.traceId))
Example #19
Source File: test_model.py From flocker with Apache License 2.0 | 5 votes |
def test_no_uuid(self): """ If no UUID is given, a UUID is generated from the hostname. This is done for backwards compatibility with existing tests, and should be removed eventually. """ node = NodeState(hostname=u'1.2.3.4') self.assertIsInstance(node.uuid, UUID)
Example #20
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bitslist_nested_limit(): type_ = [ [Bits10, Bits11, Bits12], [Bits13, Bits14] ] limit_dict = { 0: { 0: range(0xa0,0xb0), 2: range(0xb0,0xc0), }, 1: { 1: range(0xc0,0xd0), }, } 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][0].nbits == 13 assert blist[1][1].nbits == 14 assert 0xa0 <= blist[0][0] <= 0xaf assert 0xb0 <= blist[0][2] <= 0xbf assert 0xc0 <= blist[1][1] <= 0xcf print(blist) actual_test()
Example #21
Source File: test_model.py From flocker with Apache License 2.0 | 5 votes |
def test_stable(self): """ ``ip_to_uuid`` returns the same UUID given the same IP. """ self.assertEqual(ip_to_uuid(u"1.2.3.4"), ip_to_uuid(u"1.2.3.4"))
Example #22
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bits16_limited(): print("") @hypothesis.given( bits = pst.bits(16, min_value=2, max_value=11) ) @hypothesis.settings( max_examples=16 ) def actual_test( bits ): assert bits.nbits == 16 assert 2 <= bits.uint() <= 11 print( bits, bits.uint() ) actual_test()
Example #23
Source File: test_release.py From flocker with Apache License 2.0 | 5 votes |
def test_branch_exists_fails(self): """ Trying to create a release when a branch already exists for the given version fails. """ branch = self.repo.create_head('release/flocker-0.3.0') self.assertRaises( BranchExists, create_release_branch, '0.3.0', base_branch=branch)
Example #24
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bits_signed( nbits ): print("") @hypothesis.given( bits = pst.bits(nbits, True) ) @hypothesis.settings( max_examples=16 ) def actual_test( bits ): assert bits.nbits == nbits print( bits, bits.int() ) actual_test()
Example #25
Source File: strategies_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bits_unsigned( nbits ): print("") @hypothesis.given( bits = pst.bits(nbits) ) @hypothesis.settings( max_examples=16 ) def actual_test( bits ): assert bits.nbits == nbits print( bits, bits.uint() ) actual_test()
Example #26
Source File: TranslationImport_closed_loop_component_test.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def local_do_test( m, data ): closed_loop_component_test( m, data ) # Use @given(st.data()) to draw input vector inside the test function # - also note that data should the rightmost argument of the test function # Set deadline to None to avoid checking how long each test spin is # Set max_examples to limit the number of attempts after multiple successes # Suppress `too_slow` healthcheck to avoid marking a long test as failed
Example #27
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 #28
Source File: test-the-untestable.py From escape-from-automanual-testing with GNU Affero General Public License v3.0 | 5 votes |
def mean(data, as_type=Fraction): """Return the mean of the input list, as the given type.""" # This function is a correct implementation of the arithmetic mean, # so that you can test it according to the metamorphic properties of # that mathematical equation for integers, floats, and fractions. assert as_type in (int, float, Fraction), as_type if as_type == int: return sum(int(n) for n in data) // len(data) # integer division case return sum(as_type(n) for n in data) / len(data) # float or Fraction case # You can use parametrize and given together, but two tips for best results: # 1. Put @parametrize *outside* @given - it doesn't work properly from the inside # 2. Use named arguments to @given - avoids confusing or colliding positional arguments
Example #29
Source File: pbt-101.py From escape-from-automanual-testing with GNU Affero General Public License v3.0 | 5 votes |
def test_sum_of_list_greater_than_max(lst): # TODO: *without* changing the test body, write the most general # argument to @given that will pass for lists of integers. assert max(lst) < sum(lst)
Example #30
Source File: test_polytope.py From Nashpy with MIT License | 5 votes |
def test_creation_of_particular_halfspaces(self): """Test that can create a given halfspace array representation""" A = np.array([[3, 3], [2, 5], [0, 6]]) expected_halfspace = np.array( [ [3.0, 3.0, -1.0], [2.0, 5.0, -1.0], [0.0, 6.0, -1.0], [-1.0, -0.0, 0.0], [-0.0, -1.0, 0.0], ] ) halfspace = build_halfspaces(A) self.assertTrue(np.array_equal(halfspace, expected_halfspace)) B = np.array([[3, 2], [2, 6], [3, 1]]) expected_halfspace = np.array( [ [3.0, 2.0, 3.0, -1.0], [2.0, 6.0, 1.0, -1.0], [-1.0, -0.0, -0.0, 0.0], [-0.0, -1.0, -0.0, 0.0], [-0.0, -0.0, -1.0, 0.0], ] ) halfspace = build_halfspaces(B.transpose()) self.assertTrue(np.array_equal(halfspace, expected_halfspace))