utest testing framework
Last updated
Last updated
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
utest (unit test) is a unit test framework developed by RT-Thread. The original intention of designing utest is to facilitate RT-Thread developers to use a unified framework interface to write test programs to achieve the purpose of unit testing, coverage testing and integration testing.
A test case (TC) is a single test performed to achieve a specific test objective. It is a specification that includes test input, execution conditions, test process, and expected results. It is a finite loop with clear end conditions and clear test results.
The utest (unit test) testing framework defines the test programs written by users as test cases . A test case contains only one testcase function (similar to the main function) and can contain multiple test unit functions.
Specifically, the test code for a certain function completed through the API provided by the utest testing framework is a test case.
A test unit is a test point after the tested function is subdivided. Each test point can be the smallest measurable unit of the tested function. Of course, different classification methods will result in different test units.
As shown in the figure above, the test case is designed based on the service interface provided by the test framework utest, which supports compiling multiple test cases together for testing. In addition, as can be seen from the figure, a test case corresponds to a unique testcase function, which contains multiple test units.
In order to achieve a unified format of test case code, the test framework utest provides a set of common API interfaces for test case writing.
assert macro
illustrate
uassert_true(value)
If value is true, the test passes, otherwise the test fails
uassert_false(value)
If value is false, the test passes, otherwise the test fails
uassert_null(value)
If value is null, the test passes, otherwise the test fails
uassert_not_null(value)
If value is non-null, the test passes, otherwise the test fails
uassert_int_equal(a, b)
If the values of a and b are equal, the test passes, otherwise the test fails
uassert_int_not_equal(a, b)
If the values of a and b are not equal, the test passes, otherwise the test fails
uassert_str_equal(a, b)
If string a and string b are the same, the test passes, otherwise the test fails
uassert_str_not_equal(a, b)
If string a and string b are not the same, the test passes, otherwise the test fails
uassert_in_range(value, min, max)
If the value is within the range of min and max, the test passes, otherwise the test fails
uassert_not_in_range(value, min, max)
If the value is not within the range of min and max, the test passes, otherwise the test fails
In a test case, use UTEST_UNIT_RUN
the macro to execute the specified test unit function test_unit_func
. The test unit must UTEST_UNIT_RUN
be executed using the macro.
parameter
describe
testcase
Test case main carrier function ( specified to use a function named static void testcase(void)
name
Test case name (unique). Specifies the naming format testcases 目录
to use relative paths .
to connect to the test case.
init
Initialization function before the test case starts
cleanup
Cleanup function after the test case ends
timeout
The estimated test time required for the test case (in seconds)
Test case naming requirements:
Test cases need to be named in the specified format. The naming format is specified to use testcases 目录
the relative path of the current test case for .
connection, and the name contains the file name of the current test case file (excluding the suffix).
Test case naming example:
Assuming that there is a test case file in the test case testcases directory testcases\components\filesystem\dfs\dfs_api_tc.c
, the name of the test case in dfs_api_tc.c is named components.filesystem.dfs.dfs_api_tc
.
The utest test framework relies on the ulog log module for log output, and the utest test framework has already set the log output level. Therefore, you only need to add it to the test case #include "utest.h"
to use all the level interfaces (LOG_D/LOG_I/LOG_E) of the ulog log module.
In addition, the utest test framework adds additional log control interfaces as follows:
Users can use the interface to control the log output level in the test case utest_log_lv_set
. UTEST_LOG_ALL
Configure to output all logs, or UTEST_LOG_ASSERT
configure to output only the logs after uassert fails.
To use the utest test framework, you need to use menuconfig in the ENV tool to configure as follows:
The utest test framework and related APIs were introduced earlier. Here we introduce the basic test case code structure.
The required code blocks of the test case file are as follows:
A basic test case must include the following:
File Comment Header (Copyright)
The test case file must contain a file comment header, including Copyright
time, author, and description information.
utest_tc_init(void)
The initialization function before the test runs is generally used to initialize the environment required for the test.
utest_tc_cleanup(void)
The cleanup function after the test is completed is used to clean up the resources requested during the test (such as memory, threads, semaphores, etc.).
testcase(void)
Test body function: A test case implementation can only contain one testcase function (similar to the main function). Usually this function is only used to run the test unit execution function UTEST_UNIT_RUN
.
A testcase can contain multiple test units, each of which UTEST_UNIT_RUN
is executed by .
UTEST_UNIT_RUN
Test unit execution function.
test_xxx(void)
Test implementation of each functional unit. Users determine the function name and function implementation based on their needs.
uassert_true
Assertion macro used to determine the test result (this assertion macro does not terminate the program). Test cases must use uassert_xxx
macros to determine the test result, otherwise the test framework will not know whether the test passes.
The entire test case is considered to have passed only after all uassert_xxx
macros have passed.
UTEST_TC_EXPORT
Export the testcase function to the test framework.
The test framework utest exports all test cases to UtestTcTab
the code segment. In IAR and MDK compilers, there is no need to define UtestTcTab
the segment in the link script, but when compiling with GCC, the segment needs to be explicitly set in the link script UtestTcTab
.
Therefore, in order for the test case to be compiled and run under GCC, the code segment must first be defined in the GCC link scriptUtestTcTab
.
In the GCC linker script .text
, add UtestTcTab
the definition of the segment, the format is as follows:
The test framework provides the following commands to facilitate users to run test cases in the RT-Thread MSH command line. The commands are as follows:
utest_list command
Lists the test cases supported by the current system, including the name of the test case and the time required for the test. This command has no parameters.
utest_run command
Test case execution command, the command format is as follows:
utest_run command parameters
describe
-thread
Running the test framework in threaded mode
-help
Print help information
testcase name
Specify the test case name. Supports the use of wildcards *
and the first part of the test case name
loop num
Specify the number of test case loops
Test command usage example:
As shown in the figure above, the test case running log is divided into four columns from left to right, namely log header information, result column, attribute column, and detailed information display column. result
The attribute is used in the log to identify the test result (PASSED or FAILED) of the test case.
From the above flowchart, we can get the following:
The utest test framework executes all test units in the testcase function sequentially.
If an assert occurs in the last UTEST_UNIT_RUN macro, all subsequent UTEST_UNIT_RUN macros will be skipped.
Before compiling with GCC, make sure that the link script adds UtestTcTab
a segment - Before compiling, make sure that RT-Thread Kernel -> Kernel Device Object -> (256) the buffer size for console log printf
is at least 256 bytes - The resources (threads, semaphores, timers, memory, etc.) created in the test case need to be released before the end of the test - A test case implementation can only use UTEST_TC_EXPORT
export a test body function (testcase function) - Write a README.md document for the written test case program to guide users to configure the test environment