How can I do test automation in the Debugger Shell?

Debugging is generally done manually, but for testing and automation you can develop scripts to run in an automated fashion. For this you have to use the Debugger Shell as the command-line debugger and use the TCL as the scripting language. You can perform automation and scripting with the debugger from basic access to memory, to stepping and controlling the execution up to programming the flash memory.

The Debugger shell uses TCL as scripting language. And instead typing in commands, you can save them into a file (for example, myScript.tcl) and execute it with the source command:

source myScript.tcl

For creating a test automation, you have to perform the following steps:

  1. Start the debugging session.
  2. Set the breakpoints at specific locations.
  3. Run the program.
  4. Compare the variables with the expected values.
  5. Print the number of errors.
  6. Exit the debugging session.

Before creating a TCL script file, execute the commands in the Debugger Shell view, as listed below:

  1. Start the debugger:
    debug
  2. Set a breakpoint in the test program. For example, an automatic breakpoint on line 9, column 1:
    bp -auto Test_LED.c 9 1
  3. Run the program:
    # run program
    go

    The debugger stops on line 9, as the following figure shows:

    Figure 1. Running Application with Auto Breakpoint

    Running Application with Auto Breakpoint

  4. Step over the breakpoint (line 9):
    step over

    The debugger jumps to line 10, as the following figure shows:

    Figure 2. Running Application - Stepping Over the Breakpoint

    Running Application - Stepping Over the Breakpoint

  5. Count the number of errors in your test script use an error counter:
    set test_nofErrors 0
  6. Check the value of `reg'. For this, set up a scripting variable test_expected and initialize it with a value of 0×40000 (the expected LED port value):
    set test_expected 0x40000
  7. In a similar way set up a variable to read the reg variable of the application and store it into test_val:
    set test_val [evaluate reg]
  8. The single line code, as listed below compares the actual value with the expected value. If it does not match, it increases the error counter. To reference the TCL variables use the $ prefix:
    if {$test_expected != $test_val} {set test_nofErrors [expr {$test_nofErrors + 1}]}
  9. Clear all breakpoints set:
    bp all off
  10. Print the number of errors:
    puts "Number of errors: $test_nofErrors"
  11. Exit the debugging session:
    kill

You can use the wait command to suspend things to follow the flow while the program runs. For example, to wait for one second:

wait 1000

Now create a script file (myScript.tcl), as listed below:

Listing: Example Automated Test Script - myScript.tcl

01 # start debug session using current active debug configuration
02 debug;

03

04 # wait for 1 second

05 wait 1000;

06 

07 # set auto breakpoint in Test_Led.c at line 9, column 1

08 bp -auto Test_LED.c 9 1;

09 

10 # resume execution, run to breakpoint

11 go;

12 

13 # wait for the breakpoint to get hit for 1 second

14 wait 1000;

15 

16 # remove all breakpoints

17 bp all off;

18 

19 # step over line of code

20 step over;

21

22 # define error counter variable

23 set test_nofErrors 0;

24 

25 #########################################################

26 # TEST: register value shall be 0x40000

27 #########################################################

28 # define test expected value variable to the expected value

29 set test_expected 0x40000;

30 

31 # evaluate 'reg' variable and define a variable with it

32 set test_val [evaluate reg];

33

34 #compare the expected value with the actual value

35 if {$test_expected != $test_val} {set test_nofErrors [expr 
{$test_nofErrors + 1}]};

36

37 #########################################################

38 # TEST: LED shall be off

39 #########################################################

40 step over;

41 set test_expected 0

42 set test_val [evaluate val];

43 if {$test_expected != $test_val} {set test_nofErrors [expr 
{$test_nofErrors + 1}]};

44 

45 #########################################################

46 # TEST: LED shall be on

47 #########################################################

48 step over;

49 step over;

50 set test_expected 1

51 set test_val [evaluate val];

52 if {$test_expected != $test_val} {set test_nofErrors [expr 
{$test_nofErrors + 1}]};

53 

54 #########################################################

55 # print number of errors

56 puts "*** Number of errors: $test_nofErrors ***";

57 

58 # terminate the debug session

59 kill;

The following figure shows the output in the Debugger Shell:

Figure 3. Debugger Shell - myScript.tcl

Debugger Shell - myScript.tcl