When creating test suites, it is strongly recommended to not create dependencies between test cases, i.e. letting test cases depend on the result of previous test cases. There are various reasons for this, for example:
There are often sufficient means to work around the need for test case dependencies. Generally, the problem is related to the state of the system under test (SUT). The action of one test case may alter the state of the system and for some other test case to run properly, the new state must be known.
Instead of passing data between test cases, it is recommended that the test cases read the state from the SUT and perform assertions (i.e. let the test case run if the state is as expected, otherwise reset or fail) and/or use the state to set variables necessary for the test case to execute properly. Common actions can often be implemented as library functions for test cases to call to set the SUT in a required state. (Such common actions may of course also be separately tested if necessary, to ensure they are working as expected). It is sometimes also possible, but not always desirable, to group tests together in one test case, i.e. let a test case perform a "scenario" test (a test that consists of subtests).
Consider for example a server application under test. The following functionality is to be tested:
There are obvious dependencies between the listed functions. We can't configure the server if it hasn't first been started, we can't connect a client until the server has been properly configured, etc. If we want to have one test case for each of the functions, we might be tempted to try to always run the test cases in the stated order and carry possible data (identities, handles, etc) between the cases and therefore introduce dependencies between them. To avoid this we could consider starting and stopping the server for every test. We would implement the start and stop action as common functions that may be called from init_per_testcase and end_per_testcase. (We would of course test the start and stop functionality separately). The configuration could perhaps also be implemented as a common function, maybe grouped with the start function. Finally the testing of connecting and disconnecting a client may be grouped into one test case. The resulting suite would look something like this:
-module(my_server_SUITE). -compile(export_all). -include_lib("ct.hrl"). %%% init and end functions... suite() -> [{require,my_server_cfg}]. init_per_testcase(start_and_stop, Config) -> Config; init_per_testcase(config, Config) -> [{server_pid,start_server()} | Config]; init_per_testcase(_, Config) -> ServerPid = start_server(), configure_server(), [{server_pid,ServerPid} | Config]. end_per_testcase(start_and_stop, _) -> ok; end_per_testcase(_, _) -> ServerPid = ?config(server_pid), stop_server(ServerPid). %%% test cases... all() -> [start_and_stop, config, connect_and_disconnect]. %% test that starting and stopping works start_and_stop(_) -> ServerPid = start_server(), stop_server(ServerPid). %% configuration test config(Config) -> ServerPid = ?config(server_pid, Config), configure_server(ServerPid). %% test connecting and disconnecting client connect_and_disconnect(Config) -> ServerPid = ?config(server_pid, Config), {ok,SessionId} = my_server:connect(ServerPid), ok = my_server:disconnect(ServerPid, SessionId). %%% common functions... start_server() -> {ok,ServerPid} = my_server:start(), ServerPid. stop_server(ServerPid) -> ok = my_server:stop(), ok. configure_server(ServerPid) -> ServerCfgData = ct:get_config(my_server_cfg), ok = my_server:configure(ServerPid, ServerCfgData), ok.
There might be situations where it is impossible, or infeasible at least, to implement independent test cases. Maybe it is simply not possible to read the SUT state. Maybe resetting the SUT is impossible and it takes much too long to restart the system. In situations where test case dependency is necessary, CT offers a structured way to carry data from one test case to the next. The same mechanism may also be used to carry data from one test suite to the next.
The mechanism for passing data is called
To save
from
To pass data from one test suite to another, the same mechanism is used. The data
should be saved by the
Example:
-module(server_b_SUITE). -compile(export_all). -include_lib("ct.hrl"). %%% init and end functions... init_per_suite(Config) -> %% read config saved by previous test suite {server_a_SUITE,OldConfig} = ?config(saved_config, Config), %% extract server identity (comes from server_a_SUITE) ServerId = ?config(server_id, OldConfig), SessionId = connect_to_server(ServerId), [{ids,{ServerId,SessionId}} | Config]. end_per_suite(Config) -> %% save config for server_c_SUITE (session_id and server_id) {save_config,Config} %%% test cases... all() -> [allocate, deallocate]. allocate(Config) -> {ServerId,SessionId} = ?config(ids, Config), {ok,Handle} = allocate_resource(ServerId, SessionId), %% save handle for deallocation test NewConfig = [{handle,Handle}], {save_config,NewConfig}. deallocate(Config) -> {ServerId,SessionId} = ?config(ids, Config), {allocate,OldConfig} = ?config(saved_config, Config), Handle = ?config(handle, OldConfig), ok = deallocate_resource(ServerId, SessionId, Handle).
It is also possible to save
The result will be that the test case is skipped with
It is possible that test cases depend on each other so that
if one case fails, the following test(s) should not be executed.
Typically, if the
A sequence of test cases is defined as a test case group
with a
For example, if we would like to make sure that if
groups() -> [{alloc_and_dealloc, [sequence], [alloc,dealloc]}].
Let's also assume the suite contains the test case
all() -> [{group,alloc_and_dealloc}, get_resource_status].
If
Test cases in a sequence will be executed in order until they have all succeeded or until one case fails. If one fails, all following cases in the sequence are skipped. The cases in the sequence that have succeeded up to that point are reported as successful in the log. An arbitrary number of sequences may be specified. Example:
groups() -> [{scenarioA, [sequence], [testA1, testA2]}, {scenarioB, [sequence], [testB1, testB2, testB3]}]. all() -> [test1, test2, {group,scenarioA}, test3, {group,scenarioB}, test4].
It is possible to have sub-groups in a sequence group. Such sub-groups can have
any property, i.e. they are not required to also be sequences. If you want the status
of the sub-group to affect the sequence on the level above, return