Allow output of logging when running unit tests (#13556)

* Initial pass at enabling logging for unit tests

* Add to docs

* Bind debug for more test types

* Force everything

* Tidy up slightly
This commit is contained in:
Joel Challis 2021-07-25 17:18:09 +01:00 committed by GitHub
parent b69e7431aa
commit fc9fb2c775
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 6 deletions

View file

@ -24,7 +24,6 @@ GTEST_INTERNAL_INC :=\
$(GTEST_OUTPUT)_SRC :=\ $(GTEST_OUTPUT)_SRC :=\
googletest/src/gtest-all.cc\ googletest/src/gtest-all.cc\
googletest/src/gtest_main.cc\
googlemock/src/gmock-all.cc googlemock/src/gmock-all.cc
$(GTEST_OUTPUT)_DEFS := $(GTEST_OUTPUT)_DEFS :=
@ -35,7 +34,8 @@ CREATE_MAP := no
VPATH +=\ VPATH +=\
$(LIB_PATH)/googletest\ $(LIB_PATH)/googletest\
$(LIB_PATH)/googlemock $(LIB_PATH)/googlemock\
$(LIB_PATH)/printf
all: elf all: elf
@ -43,6 +43,10 @@ VPATH += $(COMMON_VPATH)
PLATFORM:=TEST PLATFORM:=TEST
PLATFORM_KEY:=test PLATFORM_KEY:=test
ifeq ($(strip $(DEBUG)), 1)
CONSOLE_ENABLE = yes
endif
ifneq ($(filter $(FULL_TESTS),$(TEST)),) ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include tests/$(TEST)/rules.mk include tests/$(TEST)/rules.mk
endif endif
@ -55,6 +59,11 @@ ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include build_full_test.mk include build_full_test.mk
endif endif
$(TEST)_SRC += \
tests/test_common/main.c \
$(LIB_PATH)/printf/printf.c \
$(COMMON_DIR)/printf.c
$(TEST_OBJ)/$(TEST)_SRC := $($(TEST)_SRC) $(TEST_OBJ)/$(TEST)_SRC := $($(TEST)_SRC)
$(TEST_OBJ)/$(TEST)_INC := $($(TEST)_INC) $(VPATH) $(GTEST_INC) $(TEST_OBJ)/$(TEST)_INC := $($(TEST)_INC) $(VPATH) $(GTEST_INC)
$(TEST_OBJ)/$(TEST)_DEFS := $($(TEST)_DEFS) $(TEST_OBJ)/$(TEST)_DEFS := $($(TEST)_DEFS)

View file

@ -28,7 +28,7 @@ For compatible platforms, [QMK Toolbox](https://github.com/qmk/qmk_toolbox) can
Prefer a terminal based solution? [hid_listen](https://www.pjrc.com/teensy/hid_listen.html), provided by PJRC, can also be used to display debug messages. Prebuilt binaries for Windows,Linux,and MacOS are available. Prefer a terminal based solution? [hid_listen](https://www.pjrc.com/teensy/hid_listen.html), provided by PJRC, can also be used to display debug messages. Prebuilt binaries for Windows,Linux,and MacOS are available.
## Sending Your Own Debug Messages ## Sending Your Own Debug Messages :id=debug-api
Sometimes it's useful to print debug messages from within your [custom code](custom_quantum_functions.md). Doing so is pretty simple. Start by including `print.h` at the top of your file: Sometimes it's useful to print debug messages from within your [custom code](custom_quantum_functions.md). Doing so is pretty simple. Start by including `print.h` at the top of your file:

View file

@ -36,12 +36,20 @@ Note how there's several different tests, each mocking out a separate part. Also
## Running the Tests ## Running the Tests
To run all the tests in the codebase, type `make test`. You can also run test matching a substring by typing `make test:matchingsubstring` Note that the tests are always compiled with the native compiler of your platform, so they are also run like any other program on your computer. To run all the tests in the codebase, type `make test:all`. You can also run test matching a substring by typing `make test:matchingsubstring` Note that the tests are always compiled with the native compiler of your platform, so they are also run like any other program on your computer.
## Debugging the Tests ## Debugging the Tests
If there are problems with the tests, you can find the executable in the `./build/test` folder. You should be able to run those with GDB or a similar debugger. If there are problems with the tests, you can find the executable in the `./build/test` folder. You should be able to run those with GDB or a similar debugger.
To forward any [debug messages](unit_testing.md#debug-api) to `stderr`, the tests can run with `DEBUG=1`. For example
```console
make test:all DEBUG=1
```
Alternatively, add `CONSOLE_ENABLE=yes` to the tests `rules.mk`.
## Full Integration Tests ## Full Integration Tests
It's not yet possible to do a full integration test, where you would compile the whole firmware and define a keymap that you are going to test. However there are plans for doing that, because writing tests that way would probably be easier, at least for people that are not used to unit testing. It's not yet possible to do a full integration test, where you would compile the whole firmware and define a keymap that you are going to test. However there are plans for doing that, because writing tests that way would probably be easier, at least for people that are not used to unit testing.

View file

@ -0,0 +1,32 @@
#include "gtest/gtest.h"
extern "C" {
#include "stdio.h"
#include "debug.h"
int8_t sendchar(uint8_t c) {
fprintf(stderr, "%c", c);
return 0;
}
__attribute__((weak)) debug_config_t debug_config = {0};
void init_logging(void) {
print_set_sendchar(sendchar);
// Customise these values to desired behaviour
// debug_enable = true;
// debug_matrix = true;
// debug_keyboard = true;
// debug_mouse = true;
debug_config.raw = 0xFF;
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
init_logging();
return RUN_ALL_TESTS();
}

View file

@ -7,10 +7,10 @@
#include "action_tapping.h" #include "action_tapping.h"
extern "C" { extern "C" {
#include "debug.h"
#include "eeconfig.h"
#include "action_layer.h" #include "action_layer.h"
}
extern "C" {
void set_time(uint32_t t); void set_time(uint32_t t);
void advance_time(uint32_t ms); void advance_time(uint32_t ms);
} }
@ -21,6 +21,10 @@ using testing::Between;
using testing::Return; using testing::Return;
void TestFixture::SetUpTestCase() { void TestFixture::SetUpTestCase() {
// The following is enough to bootstrap the values set in main
eeconfig_init_quantum();
eeconfig_update_debug(debug_config.raw);
TestDriver driver; TestDriver driver;
EXPECT_CALL(driver, send_keyboard_mock(_)); EXPECT_CALL(driver, send_keyboard_mock(_));
keyboard_init(); keyboard_init();