| 1 | = Tests = |
| 2 | |
| 3 | Support for provision of (unit) Test code is built into the cobra language. |
| 4 | |
| 5 | Tests are specified as clauses on Class members (methods and properties) or on Class types(?). |
| 6 | |
| 7 | They are given as an indented block of standalone code prefixed by the '''test''' keyword suffix with an optional test name.[[BR]] |
| 8 | The indented code block does the setup for a test (object instantiation and state initialisation), executes the test case (or cases), verifies ( usually by [wiki:Assert assertions]) that the test passes followed by any necessary teardown. |
| 9 | |
| 10 | Tests are executed whenever the program or code is run (unless tests are suppressed at build by the -include-tests:no compiler option).[[BR]] |
| 11 | |
| 12 | Failures how up as an assert failure and a stack trace. |
| 13 | |
| 14 | == Example == |
| 15 | {{{ |
| 16 | #!cobra |
| 17 | class Incrementer |
| 18 | |
| 19 | var counter =0 |
| 20 | var val = 0 |
| 21 | |
| 22 | test |
| 23 | incr = Incrementer() |
| 24 | assert incr.counter == 0 |
| 25 | assert incr.val == 0 |
| 26 | i = incr.bump(2) |
| 27 | assert i == 2 |
| 28 | assert incr.counter == 1 |
| 29 | assert incr.val == 2 |
| 30 | i = incr.bump(3) |
| 31 | assert i == 5 |
| 32 | assert incr.counter == 2 |
| 33 | assert incr.val == 5 |
| 34 | |
| 35 | |
| 36 | def bump(inc as int) as int |
| 37 | test |
| 38 | incr = Incrementer() |
| 39 | incr.bump(1) |
| 40 | assert incr.counter == 1 |
| 41 | assert incr.val == 1 |
| 42 | incr.bump(2) |
| 43 | assert incr.counter == 2 |
| 44 | assert incr.val == 3 |
| 45 | body |
| 46 | .counter += 1 |
| 47 | .val += inc |
| 48 | return .val |
| 49 | }}} |
| 50 | |