def test_add_function(provide_callables_from_fixtures_modules): config.INSTANCE.object_reuse_probability = 0.0 test_case = dtc.DefaultTestCase() generic_function = gao.GenericFunction( function=provide_callables_from_fixtures_modules["triangle"], inferred_signature=InferredSignature( signature=Signature(parameters=[ Parameter(name="x", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), Parameter(name="y", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), Parameter(name="z", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), ]), return_type=None, parameters={ "x": int, "y": int, "z": int }, ), ) cluster = MagicMock(TestCluster) cluster.select_concrete_type.side_effect = lambda x: x factory = tf.TestFactory(cluster) result = factory.add_function(test_case, generic_function, position=0) assert isinstance(result.variable_type, type(None)) assert test_case.size() <= 4
def test_attempt_generation_for_none_type_with_no_probability( test_cluster_mock): config.INSTANCE.none_probability = 0.0 factory = tf.TestFactory(test_cluster_mock) result = factory._attempt_generation(dtc.DefaultTestCase(), MagicMock(tf.TestFactory), 0, 0, True) assert result is None
def test_attempt_generation_for_none_type(test_cluster_mock): config.configuration.none_probability = 1.0 factory = tf.TestFactory(test_cluster_mock) result = factory._attempt_generation( dtc.DefaultTestCase(), MagicMock(tf.TestFactory), 0, 0, True ) assert result.distance == 0
def test_add_primitive(test_case_mock): statement = MagicMock(prim.PrimitiveStatement) statement.clone.return_value = statement factory = tf.TestFactory(MagicMock(TestCluster)) factory.add_primitive(test_case_mock, statement) statement.clone.assert_called_once() test_case_mock.add_statement.assert_called_once()
def test_add_method(provide_callables_from_fixtures_modules, test_cluster_mock): test_case = dtc.DefaultTestCase() object_ = Monkey("foo") methods = inspect.getmembers(object_, inspect.ismethod) generic_method = gao.GenericMethod( owner=provide_callables_from_fixtures_modules["Monkey"], method=methods[3][1], inferred_signature=InferredSignature( signature=Signature(parameters=[ Parameter( name="sentence", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=str, ), ]), return_type=provide_callables_from_fixtures_modules["Monkey"], parameters={"sentence": str}, ), ) test_cluster_mock.select_concrete_type.side_effect = lambda x: x factory = tf.TestFactory(test_cluster_mock) config.INSTANCE.none_probability = 1.0 result = factory.add_method(test_case, generic_method, position=0) assert result.variable_type == provide_callables_from_fixtures_modules[ "Monkey"] assert test_case.size() == 3
def test_insert_random_call_success(test_case_mock): test_cluster = MagicMock(TestCluster) acc = MagicMock(gao.GenericAccessibleObject) test_cluster.get_random_accessible.return_value = acc test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "append_generic_statement") as append_mock: assert test_factory.insert_random_call(test_case_mock, 0) append_mock.assert_called_with(test_case_mock, acc, 0)
def test_add_call_for_unknown(method_mock, variable_reference_mock, test_case_mock): test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) unknown = MagicMock(gao.GenericAccessibleObject) unknown.is_method.return_value = False unknown.is_field.return_value = False with pytest.raises(RuntimeError): test_factory.add_call_for(test_case_mock, variable_reference_mock, unknown, 0)
def __init__(self, executor: TestCaseExecutor, test_cluster: TestCluster) -> None: """ :param test_cluster: A cluster storing the available types and methods for test generation""" self._executor = executor self._test_cluster = test_cluster self._test_factory = tf.TestFactory(test_cluster)
def test_insert_random_call_on_object_at_no_accessible( test_case_mock, variable_reference_mock): test_cluster = MagicMock(TestCluster) test_cluster.get_random_call_for.side_effect = ConstructionFailedException( ) test_factory = tf.TestFactory(test_cluster) variable_reference_mock.variable_type = float assert not test_factory.insert_random_call_on_object_at( test_case_mock, variable_reference_mock, 0)
def test_insert_random_call_on_object_at_assertion(test_case_mock, variable_reference_mock): test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) variable_reference_mock.variable_type = None with pytest.raises(AssertionError): test_factory.insert_random_call_on_object_at(test_case_mock, variable_reference_mock, 0)
def test_create_primitive(type_, statement_type): factory = tf.TestFactory(MagicMock(TestCluster)) result = factory._create_primitive( dtc.DefaultTestCase(), type_, position=0, recursion_depth=0, ) assert result.variable_type == statement_type
def test_change_random_call_primitive(function_mock): test_case = dtc.DefaultTestCase() float_prim = prim.FloatPrimitiveStatement(test_case, 5.0) test_case.add_statement(float_prim) test_cluster = MagicMock(TestCluster) test_cluster.get_generators_for.return_value = {function_mock} test_factory = tf.TestFactory(test_cluster) assert not test_factory.change_random_call(test_case, float_prim)
def test__get_possible_calls_no_match(test_case_mock, function_mock): cluster = MagicMock(TestCluster) cluster.get_generators_for.return_value = {function_mock} assert ( tf.TestFactory(cluster)._get_possible_calls( float, [vri.VariableReferenceImpl(test_case_mock, int)] ) == [] )
def test_insert_random_call_on_object_no_success(): test_case = dtc.DefaultTestCase() test_cluster = MagicMock(TestCluster) test_cluster.num_accessible_objects_under_test.return_value = 0 test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "_select_random_variable_for_call") as select_mock: select_mock.return_value = None assert not test_factory.insert_random_call_on_object(test_case, 0) select_mock.assert_called_with(test_case, 0)
def test_change_call_function(function_mock): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.FloatPrimitiveStatement(test_case, 3.5)) to_replace = prim.NoneStatement(test_case, float) test_case.add_statement(to_replace) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) test_factory.change_call(test_case, to_replace, function_mock) assert test_case.statements[1].accessible_object() == function_mock assert test_case.statements[1].return_value is to_replace.return_value
def test_attempt_generation_for_type(test_case_mock): def mock_method(t, g, position, recursion_depth, allow_none): assert position == 0 assert recursion_depth == 1 assert allow_none factory = tf.TestFactory(MagicMock(TestCluster)) factory.append_generic_statement = mock_method factory._attempt_generation_for_type( test_case_mock, 0, 0, True, {MagicMock(gao.GenericAccessibleObject)})
def test_add_call_for_method(method_mock, variable_reference_mock, test_case_mock): test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "add_method") as add_field: assert test_factory.add_call_for( test_case_mock, variable_reference_mock, method_mock, 0 ) add_field.assert_called_with( test_case_mock, method_mock, 0, callee=variable_reference_mock )
def test_insert_random_call_on_object_at_success(test_case_mock, variable_reference_mock, result): test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) variable_reference_mock.variable_type = float with mock.patch.object(test_factory, "add_call_for") as call_mock: call_mock.return_value = result assert (test_factory.insert_random_call_on_object_at( test_case_mock, variable_reference_mock, 0) == result)
def test_change_call_method(constructor_mock, method_mock): test_case = dtc.DefaultTestCase() test_case.add_statement(par_stmt.ConstructorStatement(test_case, constructor_mock)) test_case.add_statement(prim.IntPrimitiveStatement(test_case, 3)) to_replace = prim.NoneStatement(test_case, float) test_case.add_statement(to_replace) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) test_factory.change_call(test_case, to_replace, method_mock) assert test_case.statements[2].accessible_object() == method_mock assert test_case.statements[2].return_value is to_replace.return_value
def __init__(self, executor: TestCaseExecutor, test_cluster: TestCluster) -> None: """Initialises the test-generation strategy. Args: executor: The executor the execute the generated test cases test_cluster: A cluster storing the available types and methods for test generation """ self._executor = executor self._test_cluster = test_cluster self._test_factory = tf.TestFactory(test_cluster)
def test_attempt_generation_for_type_from_cluster(test_case_mock): def mock_method(t, position, recursion_depth, allow_none, type_generators): assert position == 0 assert recursion_depth == 0 assert allow_none assert isinstance(type_generators, gao.GenericAccessibleObject) cluster = TestCluster() cluster.get_generators_for = lambda t: MagicMock(gao.GenericAccessibleObject) factory = tf.TestFactory(cluster) factory._attempt_generation_for_type = mock_method factory._attempt_generation(test_case_mock, MagicMock(tf.TestFactory), 0, 0, True)
def test_insert_random_statement_empty_call(): test_case = dtc.DefaultTestCase() test_cluster = MagicMock(TestCluster) test_cluster.num_accessible_objects_under_test.return_value = 1 test_factory = tf.TestFactory(test_cluster) with mock.patch("pynguin.utils.randomness.next_float") as float_mock: float_mock.return_value = 0.0 with mock.patch.object(test_factory, "insert_random_call") as ins_mock: ins_mock.return_value = True assert (test_factory.insert_random_statement( test_case, test_case.size()) == 0) ins_mock.assert_called_with(test_case, 0)
def test_change_call_unknown(): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.FloatPrimitiveStatement(test_case, 3.5)) to_replace = prim.NoneStatement(test_case, float) test_case.add_statement(to_replace) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) acc = MagicMock(gao.GenericAccessibleObject) acc.is_method.return_value = False acc.is_constructor.return_value = False acc.is_function.return_value = False with pytest.raises(AssertionError): test_factory.change_call(test_case, to_replace, acc)
def test_insert_random_statement_no_success(): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5)) test_cluster = MagicMock(TestCluster) test_cluster.num_accessible_objects_under_test.return_value = 1 test_factory = tf.TestFactory(test_cluster) with mock.patch("pynguin.utils.randomness.next_float") as float_mock: float_mock.return_value = 0.0 with mock.patch.object(test_factory, "insert_random_call") as ins_mock: ins_mock.return_value = False assert (test_factory.insert_random_statement( test_case, test_case.size()) == -1) assert ins_mock.call_args_list[0].args[1] in range( test_case.size() + 1)
def test_insert_random_call_on_object_success(variable_reference_mock): test_case = dtc.DefaultTestCase() test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "_select_random_variable_for_call") as select_mock: select_mock.return_value = variable_reference_mock with mock.patch.object( test_factory, "insert_random_call_on_object_at") as insert_mock: insert_mock.return_value = True assert test_factory.insert_random_call_on_object(test_case, 0) select_mock.assert_called_with(test_case, 0) insert_mock.assert_called_with(test_case, variable_reference_mock, 0)
def test_append_statement(test_case_mock, statement): called = False def mock_method(t, s, position=0, allow_none=True): nonlocal called called = True factory = tf.TestFactory(MagicMock(TestCluster)) factory.add_constructor = mock_method factory.add_method = mock_method factory.add_function = mock_method factory.add_field = mock_method factory.add_primitive = mock_method factory.append_statement(test_case_mock, statement) assert called
def test_append_generic_statement(test_case_mock, statement): called = False def mock_method(t, s, position=0, allow_none=True, recursion_depth=11): nonlocal called called = True return None factory = tf.TestFactory(MagicMock(TestCluster)) factory.add_constructor = mock_method factory.add_method = mock_method factory.add_function = mock_method factory.add_field = mock_method factory.add_primitive = mock_method result = factory.append_generic_statement(test_case_mock, statement) assert result is None assert called
def test_insert_random_call_rollback(test_case_mock): def side_effect(tc, f, p, callee=None): tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) raise ConstructionFailedException() test_case = dtc.DefaultTestCase() int0 = prim.IntPrimitiveStatement(test_case, 3) test_case.add_statement(int0) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "append_generic_statement") as append_generic_mock: append_generic_mock.side_effect = side_effect assert not test_factory.insert_random_call(test_case, 0) assert test_case.statements == [int0]
def test_add_call_for_rollback(method_mock, variable_reference_mock): def side_effect(tc, f, p, callee=None): tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) tc.add_statement(prim.IntPrimitiveStatement(tc, 5), position=p) raise ConstructionFailedException() test_case = dtc.DefaultTestCase() int0 = prim.IntPrimitiveStatement(test_case, 3) test_case.add_statement(int0) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "add_method") as add_field: add_field.side_effect = side_effect assert not test_factory.add_call_for( test_case, variable_reference_mock, method_mock, 0) assert test_case.statements == [int0]
def test_change_random_call_success(function_mock, method_mock, constructor_mock): test_case = dtc.DefaultTestCase() float_prim = prim.FloatPrimitiveStatement(test_case, 5.0) int0 = prim.IntPrimitiveStatement(test_case, 2) float_function1 = par_stmt.FunctionStatement(test_case, function_mock, [float_prim.return_value]) const = par_stmt.ConstructorStatement(test_case, constructor_mock) test_case.add_statement(float_prim) test_case.add_statement(int0) test_case.add_statement(const) test_case.add_statement(float_function1) test_cluster = MagicMock(TestCluster) test_cluster.get_generators_for.return_value = {function_mock, method_mock} test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "change_call") as change_mock: assert test_factory.change_random_call(test_case, float_function1) change_mock.assert_called_with(test_case, float_function1, method_mock)