Changes between Version 16 and Version 17 of Tests


Ignore:
Timestamp:
Mar 20, 2018, 7:39:55 PM (6 years ago)
Author:
raimue (Rainer Müller)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Tests

    v16 v17  
    1 === Intro ===
    2 
    3 The MacPorts testing framework uses [http://wiki.tcl.tk/1502 tcltest] for its unit tests as well as regression tests. The framework has been developed during GSoC 13 by Marius Coțofană.
    4 
    5 To keep things simple, each module of MacPorts ([browser:trunk/base/src/macports1.0 macports1.0], [browser:trunk/base/src/package1.0 package1.0], [browser:trunk/base/src/port1.0 port1.0], [browser:trunk/base/src/registry2.0 registry2.0]) has its own 'tests/' directory. Each Tcl script in a module (e.g. [browser:trunk/base/src/macports1.0/macports.tcl macports.tcl]) has its own test script located in the 'test' directory, with the same name and the '.test' extension (e.g. [browser:trunk/base/src/macports1.0/tests/macports.test macports.test]). Every proc in a script (e.g. [browser:trunk/base/src/macports1.0/macports.tcl#L334 proc macports::findBinary]) should have its own test proc (e.g. [browser:trunk/base/src/macports1.0/tests/macports.test#L334 test findBinary]) in the corresponding test file. Test procs should maintain the order in the original script and should be independent one of another.
    6 
    7 Maintainer: marius [at] macports.org
    8 
    9 === Running tests ===
    10 
    11 Tests can be run only on an installed version of MacPorts (so make sure you have run 'sudo make install').
    12 
    13 The easiest way to run all the tests, is to use the target in the Makefile.
    14 {{{
    15 make test
    16 }}}
    17 
    18 Each 'tests/' directory has a [browser:branches/gsoc13-tests/src/macports1.0/tests/test.tcl test.tcl] file, used by the make target to run all tests and format the output, making it easy to read. The script just runs the tests individually, printing the test file name, the total number of tests, number of passed, skipped, failed as well as constraints or errors of failed tests. This is one possible output when running macports.test:
    19 {{{
    20 Total:31 Passed:31 Failed:00 Skipped:00  macports.test
    21 }}}
    22 
    23 Many tests need root privileges to run correctly, but will be auto skipped in the other case. Constraints are printed just below the final result, together with the number of test cases that require it, as so:
    24 {{{
    25 Total:31 Passed:24 Failed:00 Skipped:07  macports.test
    26     Constraint: 7       root
    27 }}}
    28 
    29 The stack trace of an error that occurs during a test is printed below the constraints (if any).
    30 
    31 The file can be used also to:
    32 * run all tests:
    33 {{{
    34 tclsh test.tcl
    35 }}}
    36 * get debug info:
    37 {{{
    38 tclsh test.tcl -debug \[0-3\]
    39 }}}
    40 * list individual test files:
    41 {{{
    42 tclsh test.tcl -l
    43 }}}
    44 * run specific test files:
    45 {{{
    46 tclsh test.tcl -t macports.test
    47 }}}
    48 * print help message:
    49 {{{
    50 tclsh test.tcl -h
    51 }}}
    52 
    53 Specific test cases can be run using the '-match' argument for the file that contains the test, from its parent directory.
    54 {{{
    55 tclsh macports.test -match mportclose
    56 }}}
    57 
    58 Regression tests can be found in [browser:trunk/base/tests/test trunk/base/tests/test] and can be  run just as unit tests, using 'make test' from the parent directory.
    59 
    60 
    61 === Must know ===
    62 
    63 * regression tests have their own directory, found in [browser:trunk/base/tests/test trunk/base/tests/test]
    64 * 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 ([browser:branches/gsoc13-tests/src/macports1.0/tests/Portfile Portfile], [browser:branches/gsoc13-tests/src/macports1.0/tests/test.tcl test.tcl])
    65 * each file in a module has a corresponding test file (.test extension) in the ‘tests/’ directory
    66 * each proc in a file has a corresponding test case (test proc_name) in the
    67 * each test case must be independent from each other, so they can be run individually if needed
    68 * each test must clean all auxiliary files or directories it creates and revert all ports it installs
    69 * use a single test proceduce for each tested proc; sub-test cases should be included in the same body
    70 * when adding new regression tests, make sure to specify its name in the test_suite list of 'trunk/base/tests/test.tcl'
    71 * variables used in tests can be set at install-time using the '[module]_test_autoconf.tcl.in' file in each module ([browser:trunk/base/src/macports1.0/macports_autoconf.tcl.in macports_autoconf.tcl.in], [browser:trunk/base/src/port1.0/port_autoconf.tcl.in port_autoconf.tcl.in])
    72 * for some tests in package1.0, an update of the ports tree is required; this is done automatically if they are run using the 'test' target in the Makefile, with root privileges
    73 
    74 
    75 === Sample file ===
    76 
    77 {{{
    78 # include required tcltest package and set namespace
    79 package require tcltest 2
    80 namespace import tcltest::*
    81 
    82 # get absolute path to current ‘tests/’ directory
    83 set pwd [file normalize $argv0]
    84 set pwd [eval file join {*}[lrange [file split $pwd] 0 end-1]]
    85 
    86 # the macports_fastload.tcl file needs to be sourced so we
    87 # can directly require packages later on; we can use the autoconf
    88 # file to get the path to the file
    89 source ../port_test_autoconf.tcl
    90 source $macports::autoconf::macports_tcl_dir/macports1.0/macports_fastload.tcl
    91 package require macports 1.0
    92 
    93 # source/require tested/needed files
    94 # source ../../port1.0/portutil.tcl
    95 package require portutil 1.0
    96 
    97 # use custom macports.conf and sources.conf
    98 # you need to provide the sources.conf (see additional files) file
    99 makeDirectory $pwd/tmpdir
    100 makeDirectory $pwd/tmpdir/share
    101 makeDirectory $pwd/tmpdir/var/macports/registry
    102 set fd [open $pwd/tmpdir/macports.conf w+]
    103 puts $fd "portdbpath $pwd/tmpdir/var/macports"
    104 puts $fd "prefix $pwd/tmpdir"
    105 puts $fd "variants_conf $pwd/tmpdir/variants.conf"
    106 puts $fd "sources_conf $pwd/sources.conf"
    107 puts $fd "applications_dir $pwd/tmpdir/Applications"
    108 puts $fd "frameworks_dir $pwd/tmpdir/Library/Frameworks"
    109 close $fd
    110 set env(PORTSRC) $pwd/tmpdir/macports.conf
    111 file link -symbolic $pwd/tmpdir/share/macports $macports::autoconf::prefix/share/macports
    112 close [open $pwd/tmpdir/variants.conf w+]
    113 
    114 # debug options
    115 # ports_debug and ports_verbose are commented out as default
    116 # need to be set before ‘mportinit’
    117 array set ui_options {}
    118 #set ui_options(ports_debug)   yes
    119 #set ui_options(ports_verbose) yes
    120 mportinit ui_options
    121 
    122 # if you need to use procs from macports namespace, that are just aliases, you can
    123 # always source library.tcl (see additional files) which provides a copy macports::worker_init
    124 # without sub-interpreters; it also sets some important environment variables like
    125 # os.platform, os.major, os.arch, workpath, destpath, portpath
    126 # some other option would be to get the $workername from a $mport and use it directly
    127 
    128 # additional procs needed for testing go before the actual test cases
    129 
    130 
    131 # test case example
    132 # the test name must reflect the tested proc (remove namespaces if any)
    133 # the test description should list specific values from the tested proc on which it depends
    134 # or the partial cases it tests
    135 test mportclose {
    136     Mport close unit test.
    137 # this branch is optional and you can use other constraints too
    138 } -constraints {
    139     root
    140 # the setup branch is optional
    141 } -setup {
    142     set mport [mportopen file://.]
    143 # please make output as useful as possible (even error cases)
    144 # all sub-test cases should be part of the body branch
    145 } -body {
    146     if {[catch {mportclose $mport}] != 0} {
    147         return "FAIL: cannot run mportclose"
    148     }
    149     return "Mport close successful."
    150 # the cleanup branch is optional
    151 } -cleanup {
    152     file delete -force $pwd/work
    153 } -result "Mport close successful."
    154 
    155 
    156 # print test results
    157 cleanupTests
    158 }}}
    159 
    160 
    161 === Additional files ===
    162 
    163 * In all tests we use this [browser:branches/gsoc13-tests/src/macports1.0/tests/Portfile Portfile].
    164 * This is the [browser:branches/gsoc13-tests/src/macports1.0/tests/test.tcl test.tcl] file used to run and parse the output of all the tests in a module.
    165 * A worker_init copy, without using sub-interpreters [browser:branches/gsoc13-tests/src/package1.0/tests/library.tcl library.tcl].
    166 * Example of [browser:branches/gsoc13-tests/src/macports1.0/tests/sources.conf sources.conf].
    167 * An example of a test file [browser:branches/gsoc13-tests/src/macports1.0/tests/macports.test macports.test].
    168 * A [browser:branches/gsoc13-tests/tests/test/library.tcl library.tcl] of useful procs in regression testing.
    169 
    170 
    171 === Resources ===
    172 
    173 * [http://wiki.tcl.tk/1502 Tcltest official wiki page]
    174 * [http://web.archive.org/web/20080617153002/www.tclscripting.com/articles/apr06/article1.html Getting started with tcltest]
    175 * [http://www.tcl.tk/man/tcl8.5/TclCmd/tcltest.htm Official tcltest documentation]
     1The contents of this page moved to [https://guide.macports.org/#internals.tests the guide].