Unit tests are special pieces of code that apply known inputs to the feature code and check the results to see if they are correct. They are crucial for writing robust, bug-free code.
Flipper Zero firmware includes a separate app called unit_tests. It is run directly on Flipper devices in order to employ their hardware features and rule out any platform-related differences.
When contributing code to the Flipper Zero firmware, it is highly desirable to supply unit tests along with the proposed features. Running existing unit tests is useful to ensure that the new code doesn't introduce any regressions.
To run the unit tests, follow these steps:
./fbt FIRMWARE_APP_SET=unit_tests updater_package
.build/latest/resources
).unit_tests
command.NOTE: To run a particular test (and skip all others), specify its name as the command argument. Test names match application names defined here.
The common entry point for all tests is the unit_tests app. Test-specific code is packaged as a PLUGIN
app placed in a subdirectory of tests
in the unit_tests
mother-app and referenced in the common application.fam
. Look at other tests for an example.
Some unit tests require external data in order to function. These files (commonly called assets) reside in the unit_tests directory in their respective subdirectories. Asset files can be of any type (plain text, FlipperFormat (FFF), binary, etc.).
Each infrared protocol has a corresponding set of unit tests, so it makes sense to implement one when adding support for a new protocol. To add unit tests for your protocol, follow these steps:
test_<your_protocol_name>.irtest
in the assets directory.Each unit test has three sections:
decoder
— takes in a raw signal and outputs decoded messages.encoder
— takes in decoded messages and outputs a raw signal.encoder_decoder
— takes in decoded messages, turns them into a raw signal, and then decodes again.Infrared test asset files have an .irtest
extension and are regular .ir
files with a few additions. Decoder input data has signal names decoder_input_N
, where N is a test sequence number. Expected data goes under the name decoder_expected_N
. When testing the encoder, these two are switched.
Decoded data is represented in arrays (since a single raw signal may be decoded into several messages). If there is only one signal, then it has to be an array of size 1. Use the existing files as syntax examples.
Recording raw IR signals is possible using Flipper Zero. Launch the CLI session, run ir rx raw
, then point the remote towards the Flipper's receiver and send the signals. The raw signal data will be printed to the console in a convenient format.