| 1 | TODO: |
| 2 | * run all tests method |
| 3 | * makefile test target |
| 4 | * additional files (Portfile, sources.conf) |
| 5 | * regression tests |
| 6 | |
| 7 | === Intro === |
| 8 | |
| 9 | The MacPorts testing framework uses tcltest [0] for its unit tests as well as regression tests. |
| 10 | Maintainer: marius@macports.org |
| 11 | |
| 12 | |
| 13 | === Must know === |
| 14 | |
| 15 | * each module of MacPorts (port1.0, macports1.0, package1.0) has its own ‘tests/’ directory where the test files are located and also additional files needed (Portfile, test.tcl) |
| 16 | * each file in a module has a corresponding test file (.test extension) in the ‘tests/’ directory |
| 17 | * each proc in a file has a corresponding test case (test proc_name) in the |
| 18 | * each test case must be independent from each other, so they can be run individually if needed |
| 19 | * each test must clan all auxiliary files or directories it creates and revert all port it installs |
| 20 | |
| 21 | |
| 22 | === Sample file === |
| 23 | |
| 24 | {{{ |
| 25 | # include required tcltest package and set namespace |
| 26 | package require tcltest 2 |
| 27 | namespace import tcltest::* |
| 28 | |
| 29 | # get absolute path to current ‘tests/’ directory |
| 30 | set pwd [file normalize $argv0] |
| 31 | set pwd [eval file join {*}[lrange [file split $pwd] 0 end-1]] |
| 32 | |
| 33 | # debug options |
| 34 | # ports_debug and ports_verbose are commented out as default |
| 35 | # need to be set before ‘mportinit’ |
| 36 | array set ui_options {} |
| 37 | #set ui_options(ports_debug) yes |
| 38 | #set ui_options(ports_verbose) yes |
| 39 | mportinit ui_options |
| 40 | |
| 41 | # source/require tested/needed files |
| 42 | source ../../port1.0/portutil.tcl |
| 43 | |
| 44 | # additional procs needed for testing |
| 45 | proc registry_exists {name version {revision 0} {variants ""}} { |
| 46 | global macports::registry.format |
| 47 | return [${macports::registry.format}::entry_exists $name $version $revision $variants] |
| 48 | } |
| 49 | |
| 50 | |
| 51 | # test case example |
| 52 | # the test name must reflect the tested proc (remove namespaces if any) |
| 53 | # the test description should list specific values from the tested proc on which it depends |
| 54 | # or the partial cases it tests |
| 55 | test mportclose { |
| 56 | Mport close unit test. |
| 57 | # the setup branch is optional |
| 58 | } -setup { |
| 59 | set mport [mportopen file://.] |
| 60 | # please make output as useful as possible (even error cases) |
| 61 | # all sub-test cases should be part of the body branch |
| 62 | } -body { |
| 63 | if {[catch {mportclose $mport}] != 0} { |
| 64 | return "FAIL: cannot run mportclose" |
| 65 | } |
| 66 | return "Mport close successful." |
| 67 | # the cleanup branch is optional |
| 68 | } -cleanup { |
| 69 | file delete -force $pwd/work |
| 70 | } -result "Mport close successful." |
| 71 | |
| 72 | |
| 73 | # print test results |
| 74 | cleanupTests |
| 75 | }}} |
| 76 | |
| 77 | |
| 78 | === Resources === |
| 79 | |
| 80 | [0] - [[ http://wiki.tcl.tk/1502 | Tcltest official wiki page ]] |
| 81 | [1] - [[ http://web.archive.org/web/20080617153002/www.tclscripting.com/articles/apr06/article1.html | [2] - Getting started with tcltest ]] |
| 82 | [[ http://www.tcl.tk/man/tcl8.5/TclCmd/tcltest.htm | Official tcltest documentation ]] |