def create_robomachine(p): return RoboMachine(list(p.states), list(p.variables), list(p.rules), settings_table=p.settings_table, variables_table=p.variables_table, keywords_table=p.keywords_table)
def test_generation_with_no_tests(self): mock_machine = RoboMachine(states=[], variables=[], rules=[]) output = StringIO() robomachine.generate(machine=mock_machine, output=output, strategy=_MockStrategyWithNoTests) self.assertEqual('*** Test Cases ***', output.getvalue())
def test_to_state_is_start_state(self): at_least_one_test_generated = False for test in self.strategy_class( RoboMachine([State('s1', [], [])], [], []), 4, 's1').tests(): self.assertEqual(([], []), test) at_least_one_test_generated = True break self.assertTrue(at_least_one_test_generated)
def test_generates_all_pairs(self): variables = [ Variable('${A}', list('123')), Variable('${B}', list('456')), Variable('${C}', list('789')) ] tests = list( AllPairsRandomStrategy( RoboMachine([State('s', [], [])], variables, []), 1, 's').tests()) self.assertEqual(len(tests), 9)
def test_can_generate_test_from_variable_machine(self): variables = [Variable('var', ['a', 'b', 'c'])] at_least_one_test_generated = False for test in self.strategy_class( RoboMachine([State('state', [], [])], variables, []), 0).tests(): self.assertEqual([], test[0]) self.assertEqual(1, len(test[1])) self.assertTrue(test[1][0] in ['a', 'b', 'c']) at_least_one_test_generated = True break self.assertTrue(at_least_one_test_generated)
def test_generation_with_same_generated_tests(self): mock_machine = RoboMachine(states=[State('bar', [], [])], variables=[], rules=[]) output = StringIO() robomachine.generate(machine=mock_machine, max_tests=1000, output=output, strategy=_MockStrategyWithSameTests) self.assertEqual( 1, sum(1 for line in output.getvalue().splitlines() if line.startswith('Test ')))
def test_obeys_rules(self): variables = [Variable('${VAR}', ['a', 'b', 'c', 'd', 'e', 'f', 'g'])] rules = [Condition('${VAR}', 'e')] at_least_one_test_generated = False for test in self.strategy_class( RoboMachine([State('state', [], [])], variables, rules), 0).tests(): self.assertEqual([], test[0]) self.assertEqual(1, len(test[1])) self.assertEqual('e', test[1][0]) at_least_one_test_generated = True break self.assertTrue(at_least_one_test_generated)
def create_robomachine(p): # For some reason, p.rules contains only the _first_ rule. Work around it # by finding rule elements based on their type. def is_rule(obj): return isinstance(obj, (EquivalenceRule, ImplicationRule, AndRule, OrRule, NotRule)) rules = [v for v in p if is_rule(v)] return RoboMachine(list(p.states), list(p.variables), rules, # p.rules contains only first rule(!) settings_table=p.settings_table, variables_table=p.variables_table, keywords_table=p.keywords_table)
def test_can_generate_test_from_simple_machine(self): action12 = Action('to state2', 'state2') action21 = Action('to state1', 'state1') states = [ State('state1', [], [action12]), State('state2', [], [action21]) ] at_least_one_test_generated = False for test in self.strategy_class(RoboMachine(states, [], []), 2).tests(): self.assertEqual(([action12, action21], []), test) at_least_one_test_generated = True break self.assertTrue(at_least_one_test_generated)
def test_all_pairs_raises_error_if_rules(self): variables = [ Variable('${A}', list('123')), Variable('${B}', list('456')) ] rules = [ OrRule( [UnequalCondition('${A}', '1'), UnequalCondition('${B}', '4')]) ] self.assertRaises( AssertionError, lambda: AllPairsRandomStrategy( RoboMachine([State('s', [], [])], variables, rules), 1, 's'). tests())
def test_obeys_to_state_circular(self): a12 = Action('a12', 's2') a23 = Action('a23', 's3') a31 = Action('a31', 's1') states = [ State('s1', [], [a12]), State('s2', [], [a23]), State('s3', [], [a31]) ] at_least_one_test_generated = False for test in self.strategy_class(RoboMachine(states, [], []), 4, 's3').tests(): self.assertEqual(states[-1].name, test[0][-1].next_state.name) at_least_one_test_generated = True break self.assertTrue(at_least_one_test_generated)
def test_obeys_to_state(self): a12 = Action('s1->s2', 's2') a21 = Action('s2->s1', 's1') a23 = Action('s2->s3', 's3') a31 = Action('s3->s1', 's1') states = [ State('s1', [], [a12]), State('s2', [], [a21, a23]), State('s3', [], [a31]) ] tests_to_generate = 5 for test in self.strategy_class(RoboMachine(states, [], []), 17, 's3').tests(): self.assertEqual(states[-1].name, test[0][-1].next_state.name) tests_to_generate -= 1 if tests_to_generate == 0: break self.assertEqual(0, tests_to_generate)
def test_empty_machina_model(self): empty_machina = RoboMachine(None, None, None) self.assertEqual([], empty_machina.states) self.assertEqual([], empty_machina.variables) self.assertEqual(None, empty_machina.find_state_by_name('some name'))
def test_empty_machina_model(self): empty_machina = RoboMachine(None, None, None) self.assertEqual([], empty_machina.states) self.assertEqual([], empty_machina.variables) self.assertEqual(None, empty_machina.find_state_by_name('some name'))