.TH test_server 3erl "test_server 3.5.1" "Ericsson AB" "Erlang Module Definition" .SH NAME test_server \- This module provides support for test suite authors. .SH DESCRIPTION .LP The \fItest_server\fR\& module aids the test suite author by providing various support functions\&. The supported functionality includes: .RS 2 .TP 2 * Logging and timestamping .LP .TP 2 * Capturing output to stdout .LP .TP 2 * Retrieving and flushing the message queue of a process .LP .TP 2 * Watchdog timers, process sleep, time measurement and unit conversion .LP .TP 2 * Private scratch directory for all test suites .LP .TP 2 * Start and stop of slave- or peer nodes .LP .RE .LP For more information on how to write test cases and for examples, please see the Test Server User\&'s Guide\&. .SH "TEST SUITE SUPPORT FUNCTIONS" .LP The following functions are supposed to be used inside a test suite\&. .SH EXPORTS .LP .B os_type() -> OSType .br .RS .LP Types: .RS 3 OSType = term() .br .RS 2 This is the same as returned from \fIos:type/0\fR\& .RE .RE .RE .RS .LP This function can be called on controller or target node, and it will always return the OS type of the target node\&. .RE .LP .B fail() .br .B fail(Reason) .br .RS .LP Types: .RS 3 Reason = term() .br .RS 2 The reason why the test case failed\&. .RE .RE .RE .RS .LP This will make the test suite fail with a given reason, or with \fIsuite_failed\fR\& if no reason was given\&. Use this function if you want to terminate a test case, as this will make it easier to read the log- and HTML files\&. \fIReason\fR\& will appear in the comment field in the HTML log\&. .RE .LP .B timetrap(Timout) -> Handle .br .RS .LP Types: .RS 3 Timeout = integer() | {hours,H} | {minutes,M} | {seconds,S} .br H = M = S = integer() .br Pid = pid() .br .RS 2 The process that is to be timetrapped (\fIself()\fR\&by default) .RE .RE .RE .RS .LP Sets up a time trap for the current process\&. An expired timetrap kills the process with reason \fItimetrap_timeout\fR\&\&. The returned handle is to be given as argument to \fItimetrap_cancel\fR\& before the timetrap expires\&. If \fITimeout\fR\& is an integer, it is expected to be milliseconds\&. .LP .RS -4 .B Note: .RE If the current process is trapping exits, it will not be killed by the exit signal with reason \fItimetrap_timeout\fR\&\&. If this happens, the process will be sent an exit signal with reason \fIkill\fR\& 10 seconds later which will kill the process\&. Information about the timetrap timeout will in this case not be found in the test logs\&. However, the error_logger will be sent a warning\&. .RE .LP .B timetrap_cancel(Handle) -> ok .br .RS .LP Types: .RS 3 Handle = term() .br .RS 2 Handle returned from \fItimetrap\fR\& .RE .RE .RE .RS .LP This function cancels a timetrap\&. This must be done before the timetrap expires\&. .RE .LP .B timetrap_scale_factor() -> ScaleFactor .br .RS .LP Types: .RS 3 ScaleFactor = integer() .br .RE .RE .RS .LP This function returns the scale factor by which all timetraps are scaled\&. It is normally 1, but can be greater than 1 if the test_server is running \fIcover\fR\&, using a larger amount of scheduler threads than the amount of logical processors on the system, running under purify, valgrind or in a debug-compiled emulator\&. The scale factor can be used if you need to scale you own timeouts in test cases with same factor as the test_server uses\&. .RE .LP .B sleep(MSecs) -> ok .br .RS .LP Types: .RS 3 MSecs = integer() | float() | infinity .br .RS 2 The number of milliseconds to sleep .RE .RE .RE .RS .LP This function suspends the calling process for at least the supplied number of milliseconds\&. There are two major reasons why you should use this function instead of \fItimer:sleep\fR\&, the first being that the module \fItimer\fR\& may be unavailable at the time the test suite is run, and the second that it also accepts floating point numbers\&. .RE .LP .B adjusted_sleep(MSecs) -> ok .br .RS .LP Types: .RS 3 MSecs = integer() | float() | infinity .br .RS 2 The default number of milliseconds to sleep .RE .RE .RE .RS .LP This function suspends the calling process for at least the supplied number of milliseconds\&. The function behaves the same way as \fItest_server:sleep/1\fR\&, only \fIMSecs\fR\& will be multiplied by the \&'multiply_timetraps\&' value, if set, and also automatically scaled up if \&'scale_timetraps\&' is set to true (which it is by default)\&. .RE .LP .B hours(N) -> MSecs .br .B minutes(N) -> MSecs .br .B seconds(N) -> MSecs .br .RS .LP Types: .RS 3 N = integer() .br .RS 2 Value to convert to milliseconds\&. .RE .RE .RE .RS .LP Theese functions convert \fIN\fR\& number of hours, minutes or seconds into milliseconds\&. .LP Use this function when you want to \fItest_server:sleep/1\fR\& for a number of seconds, minutes or hours(!)\&. .RE .LP .B format(Format) -> ok .br .B format(Format, Args) .br .B format(Pri, Format) .br .B format(Pri, Format, Args) .br .RS .LP Types: .RS 3 Format = string() .br .RS 2 Format as described for \fIio_:format\fR\&\&. .RE Args = list() .br .RS 2 List of arguments to format\&. .RE .RE .RE .RS .LP Formats output just like \fIio:format\fR\& but sends the formatted string to a logfile\&. If the urgency value, \fIPri\fR\&, is lower than some threshold value, it will also be written to the test person\&'s console\&. Default urgency is 50, default threshold for display on the console is 1\&. .LP Typically, the test person don\&'t want to see everything a test suite outputs, but is merely interested in if the test cases succeeded or not, which the test server tells him\&. If he would like to see more, he could manually change the threshold values by using the \fItest_server_ctrl:set_levels/3\fR\& function\&. .RE .LP .B capture_start() -> ok .br .B capture_stop() -> ok .br .B capture_get() -> list() .br .RS .LP These functions makes it possible to capture all output to stdout from a process started by the test suite\&. The list of characters captured can be purged by using \fIcapture_get\fR\&\&. .RE .LP .B messages_get() -> list() .br .RS .LP This function will empty and return all the messages currently in the calling process\&' message queue\&. .RE .LP .B timecall(M, F, A) -> {Time, Value} .br .RS .LP Types: .RS 3 M = atom() .br .RS 2 The name of the module where the function resides\&. .RE F = atom() .br .RS 2 The name of the function to call in the module\&. .RE A = list() .br .RS 2 The arguments to supply the called function\&. .RE Time = integer() .br .RS 2 The number of seconds it took to call the function\&. .RE Value = term() .br .RS 2 Value returned from the called function\&. .RE .RE .RE .RS .LP This function measures the time (in seconds) it takes to call a certain function\&. The function call is \fInot\fR\& caught within a catch\&. .RE .LP .B do_times(N, M, F, A) -> ok .br .B do_times(N, Fun) .br .RS .LP Types: .RS 3 N = integer() .br .RS 2 Number of times to call MFA\&. .RE M = atom() .br .RS 2 Module name where the function resides\&. .RE F = atom() .br .RS 2 Function name to call\&. .RE A = list() .br .RS 2 Arguments to M:F\&. .RE .RE .RE .RS .LP Calls MFA or Fun N times\&. Useful for extensive testing of a sensitive function\&. .RE .LP .B m_out_of_n(M, N, Fun) -> ok | exit({m_out_of_n_failed, {R,left_to_do}} .br .RS .LP Types: .RS 3 N = integer() .br .RS 2 Number of times to call the Fun\&. .RE M = integer() .br .RS 2 Number of times to require a successful return\&. .RE .RE .RE .RS .LP Repeatedly evaluates the given function until it succeeds (doesn\&'t crash) M times\&. If, after N times, M successful attempts have not been accomplished, the process crashes with reason {m_out_of_n_failed, {R,left_to_do}}, where R indicates how many cases that was still to be successfully completed\&. .LP For example: .LP \fIm_out_of_n(1,4,fun() -> tricky_test_case() end)\fR\& .br Tries to run tricky_test_case() up to 4 times, and is happy if it succeeds once\&. .LP \fIm_out_of_n(7,8,fun() -> clock_sanity_check() end)\fR\& .br Tries running clock_sanity_check() up to 8 times,and allows the function to fail once\&. This might be useful if clock_sanity_check/0 is known to fail if the clock crosses an hour boundary during the test (and the up to 8 test runs could never cross 2 boundaries) .RE .LP .B call_crash(M, F, A) -> Result .br .B call_crash(Time, M, F, A) -> Result .br .B call_crash(Time, Crash, M, F, A) -> Result .br .RS .LP Types: .RS 3 Result = ok | exit(call_crash_timeout) | exit({wrong_crash_reason, Reason}) .br Crash = term() .br .RS 2 Crash return from the function\&. .RE Time = integer() .br .RS 2 Timeout in milliseconds\&. .RE M = atom() .br .RS 2 Module name where the function resides\&. .RE F = atom() .br .RS 2 Function name to call\&. .RE A = list() .br .RS 2 Arguments to M:F\&. .RE .RE .RE .RS .LP Spawns a new process that calls MFA\&. The call is considered successful if the call crashes with the gives reason (\fICrash\fR\&) or any reason if not specified\&. The call must terminate within the given time (default \fIinfinity\fR\&), or it is considered a failure\&. .RE .LP .B temp_name(Stem) -> Name .br .RS .LP Types: .RS 3 Stem = string() .br .RE .RE .RS .LP Returns a unique filename starting with \fIStem\fR\& with enough extra characters appended to make up a unique filename\&. The filename returned is guaranteed not to exist in the filesystem at the time of the call\&. .RE .LP .B break(Comment) -> ok .br .RS .LP Types: .RS 3 Comment = string() .br .RE .RE .RS .LP \fIComment\fR\& is a string which will be written in the shell, e\&.g\&. explaining what to do\&. .LP This function will cancel all timetraps and pause the execution of the test case until the user executes the \fIcontinue/0\fR\& function\&. It gives the user the opportunity to interact with the erlang node running the tests, e\&.g\&. for debugging purposes or for manually executing a part of the test case\&. .LP When the \fIbreak/1\fR\& function is called, the shell will look something like this: .LP .nf --- SEMIAUTOMATIC TESTING --- The test case executes on process <0.51.0> "Here is a comment, it could e.g. instruct to pull out a card" ----------------------------- Continue with --> test_server:continue(). .fi .LP The user can now interact with the erlang node, and when ready call \fItest_server:continue()\&.\fR\& .LP Note that this function can not be used if the test is executed with \fIts:run/0/1/2/3/4\fR\& in \fIbatch\fR\& mode\&. .RE .LP .B continue() -> ok .br .RS .LP This function must be called in order to continue after a test case has called \fIbreak/1\fR\&\&. .RE .LP .B run_on_shielded_node(Fun, CArgs) -> term() .br .RS .LP Types: .RS 3 Fun = function() (arity 0) .br .RS 2 Function to execute on the shielded node\&. .RE CArg = string() .br .RS 2 Extra command line arguments to use when starting the shielded node\&. .RE .RE .RE .RS .LP \fIFun\fR\& is executed in a process on a temporarily created hidden node with a proxy for communication with the test server node\&. The node is called a shielded node (should have been called a shield node)\&. If \fIFun\fR\& is successfully executed, the result is returned\&. A peer node (see \fIstart_node/3\fR\&) started from the shielded node will be shielded from test server node, i\&.e\&. they will not be aware of each other\&. This is useful when you want to start nodes from earlier OTP releases than the OTP release of the test server node\&. .LP Nodes from an earlier OTP release can normally not be started if the test server hasn\&'t been started in compatibility mode (see the \fI+R\fR\& flag in the \fIerl(1)\fR\& documentation) of an earlier release\&. If a shielded node is started in compatibility mode of an earlier OTP release than the OTP release of the test server node, the shielded node can start nodes of an earlier OTP release\&. .LP .RS -4 .B Note: .RE You \fImust\fR\& make sure that nodes started by the shielded node never communicate directly with the test server node\&. .LP .RS -4 .B Note: .RE Slave nodes always communicate with the test server node; therefore, \fInever\fR\& start \fIslave nodes\fR\& from the shielded node, \fIalways\fR\& start \fIpeer nodes\fR\&\&. .RE .LP .B start_node(Name, Type, Options) -> {ok, Node} | {error, Reason} .br .RS .LP Types: .RS 3 Name = atom() | string() .br .RS 2 Name of the slavenode to start (as given to -sname or -name) .RE Type = slave | peer .br .RS 2 The type of node to start\&. .RE Options = [{atom(), term()] .br .RS 2 Tuplelist of options .RE .RE .RE .RS .LP This functions starts a node, possibly on a remote machine, and guarantees cross architecture transparency\&. Type is set to either \fIslave\fR\& or \fIpeer\fR\&\&. .LP \fIslave\fR\& means that the new node will have a master, i\&.e\&. the slave node will terminate if the master terminates, TTY output produced on the slave will be sent back to the master node and file I/O is done via the master\&. The master is normally the target node unless the target is itself a slave\&. .LP \fIpeer\fR\& means that the new node is an independent node with no master\&. .LP \fIOptions\fR\& is a tuplelist which can contain one or more of .RS 2 .TP 2 .B \fI{remote, true}\fR\&: Start the node on a remote host\&. If not specified, the node will be started on the local host (with some exceptions, as for the case of VxWorks, where all nodes are started on a remote host)\&. Test cases that require a remote host will fail with a reasonable comment if no remote hosts are available at the time they are run\&. .TP 2 .B \fI{args, Arguments}\fR\&: Arguments passed directly to the node\&. This is typically a string appended to the command line\&. .TP 2 .B \fI{wait, false}\fR\&: Don\&'t wait until the node is up\&. By default, this function does not return until the node is up and running, but this option makes it return as soon as the node start command is given\&.\&. .br Only valid for peer nodes .TP 2 .B \fI{fail_on_error, false}\fR\&: Returns \fI{error, Reason}\fR\& rather than failing the test case\&. .br Only valid for peer nodes\&. Note that slave nodes always act as if they had \fIfail_on_error=false\fR\& .TP 2 .B \fI{erl, ReleaseList}\fR\&: Use an Erlang emulator determined by ReleaseList when starting nodes, instead of the same emulator as the test server is running\&. ReleaseList is a list of specifiers, where a specifier is either {release, Rel}, {prog, Prog}, or \&'this\&'\&. Rel is either the name of a release, e\&.g\&., "r12b_patched" or \&'latest\&'\&. \&'this\&' means using the same emulator as the test server\&. Prog is the name of an emulator executable\&. If the list has more than one element, one of them is picked randomly\&. (Only works on Solaris and Linux, and the test server gives warnings when it notices that nodes are not of the same version as itself\&.) .br .br When specifying this option to run a previous release, use \fIis_release_available/1\fR\& function to test if the given release is available and skip the test case if not\&. .br .br In order to avoid compatibility problems (may not appear right away), use a shielded node (see \fIrun_on_shielded_node/2\fR\&) when starting nodes from different OTP releases than the test server\&. .TP 2 .B \fI{cleanup, false}\fR\&: Tells the test server not to kill this node if it is still alive after the test case is completed\&. This is useful if the same node is to be used by a group of test cases\&. .TP 2 .B \fI{env, Env}\fR\&: \fIEnv\fR\& should be a list of tuples \fI{Name, Val}\fR\&, where \fIName\fR\& is the name of an environment variable, and \fIVal\fR\& is the value it is to have in the started node\&. Both \fIName\fR\& and \fIVal\fR\& must be strings\&. The one exception is \fIVal\fR\& being the atom \fIfalse\fR\& (in analogy with \fIos:getenv/1\fR\&), which removes the environment variable\&. Only valid for peer nodes\&. Not available on VxWorks\&. .RE .RE .LP .B stop_node(NodeName) -> bool() .br .RS .LP Types: .RS 3 NodeName = term() .br .RS 2 Name of the node to stop .RE .RE .RE .RS .LP This functions stops a node previously started with \fIstart_node/3\fR\&\&. Use this function to stop any node you start, or the test server will produce a warning message in the test logs, and kill the nodes automatically unless it was started with the \fI{cleanup, false}\fR\& option\&. .RE .LP .B is_commercial() -> bool() .br .RS .LP This function test whether the emulator is commercially supported emulator\&. The tests for a commercially supported emulator could be more stringent (for instance, a commercial release should always contain documentation for all applications)\&. .RE .LP .B is_release_available(Release) -> bool() .br .RS .LP Types: .RS 3 Release = string() | atom() .br .RS 2 Release to test for .RE .RE .RE .RS .LP This function test whether the release given by \fIRelease\fR\& (for instance, "r12b_patched") is available on the computer that the test_server controller is running on\&. Typically, you should skip the test case if not\&. .LP Caution: This function may not be called from the \fIsuite\fR\& clause of a test case, as the test_server will deadlock\&. .RE .LP .B is_native(Mod) -> bool() .br .RS .LP Types: .RS 3 Mod = atom() .br .RS 2 A module name .RE .RE .RE .RS .LP Checks whether the module is natively compiled or not .RE .LP .B app_test(App) -> ok | test_server:fail() .br .B app_test(App,Mode) .br .RS .LP Types: .RS 3 App = term() .br .RS 2 The name of the application to test .RE Mode = pedantic | tolerant .br .RS 2 Default is pedantic .RE .RE .RE .RS .LP Checks an applications \&.app file for obvious errors\&. The following is checked: .RS 2 .TP 2 * required fields .LP .TP 2 * that all modules specified actually exists .LP .TP 2 * that all requires applications exists .LP .TP 2 * that no module included in the application has export_all .LP .TP 2 * that all modules in the ebin/ dir is included (If \fIMode==tolerant\fR\& this only produces a warning, as all modules does not have to be included) .LP .RE .RE .LP .B comment(Comment) -> ok .br .RS .LP Types: .RS 3 Comment = string() .br .RE .RE .RS .LP The given String will occur in the comment field of the table on the HTML result page\&. If called several times, only the last comment is printed\&. comment/1 is also overwritten by the return value {comment,Comment} from a test case or by fail/1 (which prints Reason as a comment)\&. .RE .SH "TEST SUITE EXPORTS" .LP The following functions must be exported from a test suite module\&. .SH EXPORTS .LP .B all(suite) -> TestSpec | {skip, Comment} .br .RS .LP Types: .RS 3 TestSpec = list() .br Comment = string() .br .RS 2 This comment will be printed on the HTML result page .RE .RE .RE .RS .LP This function must return the test specification for the test suite module\&. The syntax of a test specification is described in the Test Server User\&'s Guide\&. .RE .LP .B init_per_suite(Config0) -> Config1 | {skip, Comment} .br .RS .LP Types: .RS 3 Config0 = Config1 = [tuple()] .br Comment = string() .br .RS 2 Describes why the suite is skipped .RE .RE .RE .RS .LP This function is called before all other test cases in the suite\&. \fIConfig\fR\& is the configuration which can be modified here\&. Whatever is returned from this function is given as \fIConfig\fR\& to the test cases\&. .LP If this function fails, all test cases in the suite will be skipped\&. .RE .LP .B end_per_suite(Config) -> void() .br .RS .LP Types: .RS 3 Config = [tuple()] .br .RE .RE .RS .LP This function is called after the last test case in the suite, and can be used to clean up whatever the test cases have done\&. The return value is ignored\&. .RE .LP .B init_per_testcase(Case, Config0) -> Config1 | {skip, Comment} .br .RS .LP Types: .RS 3 Case = atom() .br Config0 = Config1 = [tuple()] .br Comment = string() .br .RS 2 Describes why the test case is skipped .RE .RE .RE .RS .LP This function is called before each test case\&. The \fICase\fR\& argument is the name of the test case, and \fIConfig\fR\& is the configuration which can be modified here\&. Whatever is returned from this function is given as \fIConfig\fR\& to the test case\&. .RE .LP .B end_per_testcase(Case, Config) -> void() .br .RS .LP Types: .RS 3 Case = atom() .br Config = [tuple()] .br .RE .RE .RS .LP This function is called after each test case, and can be used to clean up whatever the test case has done\&. The return value is ignored\&. .RE .LP .B Case(doc) -> [Decription] .br .B Case(suite) -> [] | TestSpec | {skip, Comment} .br .B Case(Config) -> {skip, Comment} | {comment, Comment} | Ok .br .RS .LP Types: .RS 3 Description = string() .br .RS 2 Short description of the test case .RE TestSpec = list() .br Comment = string() .br .RS 2 This comment will be printed on the HTML result page .RE Ok = term() .br Config = [tuple()] .br .RS 2 Elements from the Config parameter can be read with the ?config macro, see section about test suite support macros .RE .RE .RE .RS .LP The \fIdocumentation clause\fR\& (argument \fIdoc\fR\&) can be used for automatic generation of test documentation or test descriptions\&. .LP The \fIspecification clause\fR\& (argument \fIspec\fR\&) shall return an empty list, the test specification for the test case or \fI{skip,Comment}\fR\&\&. The syntax of a test specification is described in the Test Server User\&'s Guide\&. .LP \fINote that the specification clause always is executed on the controller host\&.\fR\& .LP The \fIexecution clause\fR\& (argument \fIConfig\fR\&) is only called if the specification clause returns an empty list\&. The execution clause is the real test case\&. Here you must call the functions you want to test, and do whatever you need to check the result\&. If something fails, make sure the process crashes or call \fItest_server:fail/0/1\fR\& (which also will cause the process to crash)\&. .LP You can return \fI{skip,Comment}\fR\& if you decide not to run the test case after all, e\&.g\&. if it is not applicable on this platform\&. .LP You can return \fI{comment,Comment}\fR\& if you wish to print some information in the \&'Comment\&' field on the HTML result page\&. .LP If the execution clause returns anything else, it is considered a success, unless it is \fI{\&'EXIT\&',Reason}\fR\& or \fI{\&'EXIT\&',Pid,Reason}\fR\& which can\&'t be distinguished from a crash, and thus will be considered a failure\&. .LP A \fIconf test case\fR\& is a group of test cases with an init and a cleanup function\&. The init and cleanup functions are also test cases, but they have special rules: .RS 2 .TP 2 * They do not need a specification clause\&. .LP .TP 2 * They must always have the execution clause\&. .LP .TP 2 * They must return the \fIConfig\fR\& parameter, a modified version of it or \fI{skip,Comment}\fR\& from the execution clause\&. .LP .TP 2 * The cleanup function may also return a tuple \fI{return_group_result,Status}\fR\&, which is used to return the status of the conf case to Test Server and/or to a conf case on a higher level\&. (\fIStatus = ok | skipped | failed\fR\&)\&. .LP .TP 2 * \fIinit_per_testcase\fR\& and \fIend_per_testcase\fR\& are not called before and after these functions\&. .LP .RE .RE .SH "TEST SUITE LINE NUMBERS" .LP If a test case fails, the test server can report the exact line number at which it failed\&. There are two ways of doing this, either by using the \fIline\fR\& macro or by using the \fItest_server_line\fR\& parse transform\&. .LP The \fIline\fR\& macro is described under TEST SUITE SUPPORT MACROS below\&. The \fIline\fR\& macro will only report the last line executed when a test case failed\&. .LP The \fItest_server_line\fR\& parse transform is activated by including the headerfile \fItest_server_line\&.hrl\fR\& in the test suite\&. When doing this, it is important that the \fItest_server_line\fR\& module is in the code path of the erlang node compiling the test suite\&. The parse transform will report a history of a maximum of 10 lines when a test case fails\&. Consecutive lines in the same function are not shown\&. .LP The attribute \fI-no_lines(FuncList)\&.\fR\& can be used in the test suite to exclude specific functions from the parse transform\&. This is necessary e\&.g\&. for functions that are executed on old (i\&.e\&.