blob: c0a2ee894d1247e9c71669aa2003023b14109263 [file] [log] [blame]
Neels Hofmeyrc0827c42017-04-23 15:04:19 +02001== Test API
2
Pau Espin Pedrolab78df22020-06-04 19:45:13 +02003All tests run by {app-name} are python script files. On top of usual python
4standard features, {app-name} provides a set of public APIs and tools that
5these tests can use in order to interact with the core of {app-name}, like
6creating object classes, run processes synchronously or asynchronously, wait for
7events, retrieve specific configuration, etc. This section aims at documenting
8the most relevant tools available for tests.
9
10First of all, it is important to avoid blocking out of the core's main event loop in
11the test, since doing that will prevent {app-name} core functionalities to work
12properly, such as control of asynchronous processes.
13
14To get access to those functionalities, a test must import a test environment
15previously prepared by {app-name} before the test was started:
16[source,python]
17----
18#!/usr/bin/env python3
19from osmo_gsm_tester.testenv import *
20----
21
22After the test environment is imported, aome usual functionalities are available
23directly under the global scope. Specially noticeable is the existence of object
24_tenv_, which provides access to most of the functionalities.
25
26The test can simply ask {app-name} to sleep some time, while giving control back
27to {app-name} core's mainloop:
28[source,python]
29----
30sleep(3) # sleep for 3 seconds
31----
32
33One can also wait for events in the background, for instance launch a child
34process locally in the same host and wait for its termination:
35[source,python]
36----
Pau Espin Pedrolcedee0a2020-06-16 11:39:19 +020037proc = process.Process('process_description_name', working_dir_to_store_logs, 'sleep 4') # <1>
38tenv.remember_to_stop(proc) # <2>
39proc.launch() # <3>
40proc.wait() # <4>
Pau Espin Pedrolab78df22020-06-04 19:45:13 +020041----
42<1> Create process object. This line doesn't yet runs it.
43<2> Make sure the core will kill the process if this test fails
44<3> Start process asynchronously
45<4> wait until process is done. One could waiting generically here too: _wait(proc.terminated)_
46
47If running asynchronously is not needed, one can run synchronously in an easy
48way:
49[source,python]
50----
51proc = process.Process('process_description_name', working_dir_to_store_logs, 'sleep 4')
52proc.launch_sync()
53----
54
55One can also log output using either the regular _print_ function from python,
56or using {app-name} specific functions available:
57[source,python]
58----
59log('this is a regular log message')
60dbg('this is a dbg message, only printed on outputs where dbg is enabled')
61err('outputs log message for non-expected events')
62print('this is the same as log()')
63----
64
65The test also gains access to suite and/or test specific configuration through
66different APIs:
67[source,python]
68----
69test_config = tenv.config_test_specific()
70threshold = int(test_config.get('threshold', 2))
71suite_config = tenv.config_suite_specific()
72foobar = suite_config['foobar']
73----
74
75A test requiring a really specific config file for an object class it is going
76to run can provide its own template files by overlaying an own directory
77containing them on top of the usual default directory where object class
78templates are (_osmo-gsm-tester.git/src/osmo_gsm_tester/obj/templates/_):
79[source,python]
80----
81tenv.set_overlay_template_dir(os.path.join(os.path.dirname(__file__), 'mytemplatedir'))
82----
83
84Several tests in a suite can also share code by using some APIs provided by
85{app-names}. The shared python code must be placed in files under the 'lib/'
86subdirectory in the suite directory where the test belongs to.
87[source,python]
88----
89# File containing function foobar() available under ${suite_dir}/lib/testlib.py:
90import testlib
91tenv.test_import_modules_register_for_cleanup(testlib)
92from testlib import foobar
93----
94
95For a complete set of features and how to use them, one can have a look at real
96examples present in {app-name} git repository under the _sysmocom/_ directory.
97Besides those, have a look too a _testenv.py_ file, which implements the 'tenv'
98object available to tests.
99
100=== Test verdict
101
102In general, a test reaching the end of the file and returning control to
103{app-name} core will be flagged as a successful test (PASS).
104
105If an exception is thrown from within the test file and propagated to
106{app-name}, the test will be considered as failed and {app-name} will store all
107failure related information from the caught exception.