Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow output of logging when running unit tests #13556

Merged
merged 5 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions build_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ GTEST_INTERNAL_INC :=\

$(GTEST_OUTPUT)_SRC :=\
googletest/src/gtest-all.cc\
googletest/src/gtest_main.cc\
googlemock/src/gmock-all.cc

$(GTEST_OUTPUT)_DEFS :=
Expand All @@ -35,14 +34,19 @@ CREATE_MAP := no

VPATH +=\
$(LIB_PATH)/googletest\
$(LIB_PATH)/googlemock
$(LIB_PATH)/googlemock\
$(LIB_PATH)/printf

all: elf

VPATH += $(COMMON_VPATH)
PLATFORM:=TEST
PLATFORM_KEY:=test

ifeq ($(strip $(DEBUG)), 1)
CONSOLE_ENABLE = yes
endif

ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include tests/$(TEST)/rules.mk
endif
Expand All @@ -55,6 +59,11 @@ ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include build_full_test.mk
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)_INC := $($(TEST)_INC) $(VPATH) $(GTEST_INC)
$(TEST_OBJ)/$(TEST)_DEFS := $($(TEST)_DEFS)
Expand Down
2 changes: 1 addition & 1 deletion docs/faq_debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

## 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:

Expand Down
10 changes: 9 additions & 1 deletion docs/unit_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ Note how there's several different tests, each mocking out a separate part. Also

## 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

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

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.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_common/main.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 6 additions & 2 deletions tests/test_common/test_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include "action_tapping.h"

extern "C" {
#include "debug.h"
#include "eeconfig.h"
#include "action_layer.h"
}

extern "C" {
void set_time(uint32_t t);
void advance_time(uint32_t ms);
}
Expand All @@ -21,6 +21,10 @@ using testing::Between;
using testing::Return;

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;
EXPECT_CALL(driver, send_keyboard_mock(_));
keyboard_init();
Expand Down