Beispiel #1
0
def get_config_from_txt_config_file(config_file):
    #from robomachine_model import RoboMachine
    from robomachine.parsing import parse
    try:
        with open(config_file, 'r') as inp:
            machine = parse(inp.read())
    except IOError, e:
        sys.exit(unicode(e))
Beispiel #2
0
 def test_machine_parsing(self):
     m = parsing.parse(_LOGIN_MACHINE)
     self.assertEqual('${USERNAME}', m.variables[0].name)
     self.assertEqual('${PASSWORD}', m.variables[1].name)
     self.assertEqual(2, len(m.variables))
     m.apply_variable_values(['demo', 'mode'])
     self.assertEqual('${USERNAME} == demo  and  ${PASSWORD} == mode', str(m.states[0].actions[0].condition))
     self.assertEqual('Welcome Page', m.states[0].actions[0].next_state.name)
     m.apply_variable_values(['invalid', 'invalid'])
     self.assertEqual('otherwise', m.states[0].actions[0].condition)
     self.assertEqual('Error Page', m.states[0].actions[0].next_state.name)
Beispiel #3
0
 def test_machine_parsing(self):
     m = parsing.parse(_LOGIN_MACHINE)
     self.assertEqual('${USERNAME}', m.variables[0].name)
     self.assertEqual('${PASSWORD}', m.variables[1].name)
     self.assertEqual(2, len(m.variables))
     m.apply_variable_values(['demo', 'mode'])
     self.assertEqual('${USERNAME} == demo  and  ${PASSWORD} == mode',
                      str(m.states[0].actions[0].condition))
     self.assertEqual('Welcome Page',
                      m.states[0].actions[0].next_state.name)
     m.apply_variable_values(['invalid', 'invalid'])
     self.assertEqual('otherwise', m.states[0].actions[0].condition)
     self.assertEqual('Error Page', m.states[0].actions[0].next_state.name)
Beispiel #4
0
 def transform(self, text):
     output = StringIO()
     self.generate(parse(text), output=output)
     return output.getvalue()
Beispiel #5
0
def transform(text):
    output = StringIO()
    generate(parse(text), output=output)
    return output.getvalue()
Beispiel #6
0
def main():
    args = parser.parse_args()
    generator = Generator()
    strategy_class = _select_strategy(args.generation_algorithm)
    all_actions = set()

    if args.input.endswith('.txt') and not args.output:
        sys.exit('txt input not allowed when no output')
    try:
        with open(args.input, 'r') as inp:
            machine = parse(inp.read())
    except IOError as e:
        sys.exit(unicode(e))
    except RoboMachineParsingException as e:
        sys.exit(1)

    # File names:
    output_base_name = os.path.splitext(args.output or args.input)[0]
    output_test_file = output_base_name + '.robot'
    output_dot_file = output_base_name + '.dot'

    # Find unique actions:
    for state in machine.states:
        for action in state._actions:
            action._parent_state = state
            all_actions.add(action)

    # DOT Graph:
    if args.generate_dot_graph != 'none':
        # Generate graph in dot format:
        dot_graph = 'digraph TestModel {\n'
        #
        # Nodes:
        for state in machine.states:
            dot_graph += '  {:s}  [label=\"{:s}\"];\n'.format(
                state.name.replace(' ', '_'), state.name)
        #
        # Transitions:
        for action in all_actions:
            state = action._parent_state
            action_name = action.name if action.name != '' else '[tau]'
            dot_state_name = state.name.replace(' ', '_')
            dot_next_state_name = action.next_state.name.replace(' ', '_')
            dot_action_name = re.sub(r'\s\s+', '  ', action_name)

            dot_graph += '  {:s}  -> {:s}  [label="{:s}"];\n'.format(
                dot_state_name, dot_next_state_name, dot_action_name)
        dot_graph += '}\n'
        #
        # Write to STDOUT:
        print('-' * 78)
        print('Dot graph')
        print('---------')
        print(dot_graph)
        print('-' * 78)
        #
        # Write to file:
        with open(output_dot_file, 'w') as out:
            out.write(dot_graph)
        try:
            retcode = subprocess.call(
                ['dot', '-O', '-T' + args.generate_dot_graph, output_dot_file])
        except OSError:
            retcode = -1
        if retcode == 0:
            print('Generated dot files: {:s}, {:s}.{:s}'.format(
                output_dot_file, output_dot_file, args.generate_dot_graph))
        else:
            print(
                'ERROR: Something went wrong during the dot file generation!\n'
                + '       Maybe you haven\'t yet installed the dot tool?')

    # Generate tests:
    with open(output_test_file, 'w') as out:
        generator.generate(machine,
                           max_tests=args.tests_max,
                           max_actions=args.actions_max,
                           to_state=args.to_state,
                           output=out,
                           strategy=strategy_class)
    print('Generated test file: {:s}'.format(output_test_file))

    # Coverage information:
    covered_states = generator.visited_states
    covered_actions = generator.visited_actions
    uncovered_states = set(machine.states).difference(generator.visited_states)
    uncovered_actions = all_actions.difference(generator.visited_actions)
    #
    # Write to STDOUT:
    print('-' * 78)
    #
    # Covered states:
    print('Covered states ({:d}/{:d}):'.format(len(covered_states),
                                               len(machine.states)))
    if covered_states:
        for state in covered_states:
            print('    {:s}'.format(state.name))
    else:
        print('    -none-')
    #
    # Covered actions:
    print('\nCovered actions ({:d}/{:d}):'.format(len(covered_actions),
                                                  len(all_actions)))
    if covered_actions:
        for action in covered_actions:
            action_name = action.name if action.name != '' else '[tau]'
            print('    {:s}  ({:s} -> {:s})'.format(action_name,
                                                    action._parent_state.name,
                                                    action.next_state.name))
    else:
        print('    -none-')
    #
    # Uncovered states:
    if uncovered_states:
        print('\nUncovered states ({:d}/{:d}):'.format(len(uncovered_states),
                                                       len(machine.states)))
        for state in uncovered_states:
            print('    {:s}'.format(state.name))
    #
    # Uncovered actions:
    if uncovered_actions:
        print('\nUncovered actions ({:d}/{:d}):'.format(
            len(uncovered_actions), len(all_actions)))
        for action in uncovered_actions:
            action_name = action.name if action.name != '' else '[tau]'
            print('    {:s} ({:s} -> {:s})'.format(action_name,
                                                   action._parent_state.name,
                                                   action.next_state.name))
    print('-' * 78)

    # Run tests:
    if not args.do_not_execute:
        print('\nRunning generated tests with robot:')
        retcode = subprocess.call(['robot', output_test_file])
        sys.exit(retcode)
Beispiel #7
0
def main():
    args = parser.parse_args()
    generator = Generator()
    strategy_class = _select_strategy(args.generation_algorithm)
    all_actions = set()

    if args.input.endswith('.txt') and not args.output:
        sys.exit('txt input not allowed when no output')
    try:
        with open(args.input, 'r') as inp:
            machine = parse(inp.read())
    except IOError as e:
        sys.exit(unicode(e))
    except RoboMachineParsingException as e:
        sys.exit(1)

    # File names:
    output_base_name = os.path.splitext(args.output or args.input)[0]
    output_test_file = output_base_name + '.robot'
    output_dot_file = output_base_name + '.dot'

    # Find unique actions:
    for state in machine.states:
        for action in state._actions:
            action._parent_state = state
            all_actions.add(action)

    # DOT Graph:
    if args.generate_dot_graph != 'none':
        # Generate graph in dot format:
        dot_graph = 'digraph TestModel {\n'
        #
        # Nodes:
        for state in machine.states:
            dot_graph += '  {:s}  [label=\"{:s}\"];\n'.format(state.name.replace(' ', '_'), state.name)
        #
        # Transitions:
        for action in all_actions:
            state = action._parent_state
            action_name = action.name if action.name != '' else '[tau]'
            dot_state_name = state.name.replace(' ', '_')
            dot_next_state_name = action.next_state.name.replace(' ', '_')
            dot_action_name = re.sub(r'\s\s+', '  ', action_name)

            dot_graph += '  {:s}  -> {:s}  [label="{:s}"];\n'.format(
                dot_state_name, dot_next_state_name, dot_action_name)
        dot_graph += '}\n'
        #
        # Write to STDOUT:
        print('-' * 78)
        print('Dot graph')
        print('---------')
        print(dot_graph)
        print('-' * 78)
        #
        # Write to file:
        with open(output_dot_file, 'w') as out:
            out.write(dot_graph)
        try:
            retcode = subprocess.call(['dot', '-O', '-T' + args.generate_dot_graph, output_dot_file])
        except OSError:
            retcode = -1
        if retcode == 0:
            print('Generated dot files: {:s}, {:s}.{:s}'.format(
                output_dot_file, output_dot_file, args.generate_dot_graph))
        else:
            print('ERROR: Something went wrong during the dot file generation!\n' +
                  '       Maybe you haven\'t yet installed the dot tool?')

    # Generate tests:
    with open(output_test_file, 'w') as out:
        generator.generate(machine,
                           max_tests=args.tests_max,
                           max_actions=args.actions_max,
                           to_state=args.to_state,
                           output=out,
                           strategy=strategy_class)
    print('Generated test file: {:s}'.format(output_test_file))

    # Coverage information:
    covered_states = generator.visited_states
    covered_actions = generator.visited_actions
    uncovered_states = set(machine.states).difference(generator.visited_states)
    uncovered_actions = all_actions.difference(generator.visited_actions)
    #
    # Write to STDOUT:
    print('-' * 78)
    #
    # Covered states:
    print('Covered states ({:d}/{:d}):'.format(len(covered_states), len(machine.states)))
    if covered_states:
        for state in covered_states:
            print('    {:s}'.format(state.name))
    else:
        print('    -none-')
    #
    # Covered actions:
    print('\nCovered actions ({:d}/{:d}):'.format(len(covered_actions), len(all_actions)))
    if covered_actions:
        for action in covered_actions:
            action_name = action.name if action.name != '' else '[tau]'
            print('    {:s}  ({:s} -> {:s})'.format(action_name, action._parent_state.name, action.next_state.name))
    else:
        print('    -none-')
    #
    # Uncovered states:
    if uncovered_states:
        print('\nUncovered states ({:d}/{:d}):'.format(len(uncovered_states), len(machine.states)))
        for state in uncovered_states:
            print('    {:s}'.format(state.name))
    #
    # Uncovered actions:
    if uncovered_actions:
        print('\nUncovered actions ({:d}/{:d}):'.format(len(uncovered_actions), len(all_actions)))
        for action in uncovered_actions:
            action_name = action.name if action.name != '' else '[tau]'
            print('    {:s} ({:s} -> {:s})'.format(action_name, action._parent_state.name, action.next_state.name))
    print('-' * 78)

    # Run tests:
    if not args.do_not_execute:
        print('\nRunning generated tests with pybot:')
        retcode = subprocess.call(['pybot', output_test_file])
        sys.exit(retcode)
Beispiel #8
0
 def test_generate_all_dfs(self):
     m = parsing.parse(_LOGIN_MACHINE)
     out = StringIO()
     robomachine.generate(m, output=out)
     self.assertEqual(_LOGIN_TESTS_GENERATE_ALL_DFS, out.getvalue())
Beispiel #9
0
 def test_generate_all_dfs(self):
     m = parsing.parse(_LOGIN_MACHINE)
     out = StringIO()
     robomachine.generate(m, output=out)
     self.assertEqual(_LOGIN_TESTS_GENERATE_ALL_DFS, out.getvalue())