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

Google tests run twice #31027

Closed
mejtejt opened this issue Dec 28, 2020 · 7 comments
Closed

Google tests run twice #31027

mejtejt opened this issue Dec 28, 2020 · 7 comments
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@mejtejt
Copy link

mejtejt commented Dec 28, 2020

Describe the bug
I am unit testing my C++ code with google test framework in Zephyr. When, inside of the tests folder prj.conf file, I enable
CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y

tests are executed twice.

Expected behavior
I want tests to run once.

Logs and console output
The following screen shot represents the structure of my project:
image

The following is the content of CMakeLists.txt file in test folder.

cmake_minimum_required(VERSION 3.13.1)

set(CMAKE_CXX_COMPILER /usr/bin/g++ CACHE PATH "" FORCE)

set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)

set(BOARD native_posix)
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr)
set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
set(ZEPHYR_EXTRA_MODULES $ENV{ZEPHYR_BASE}/../greyp-zephyr-packages/modules/googletest
                         $ENV{ZEPHYR_BASE}/../greyp-zephyr-packages/modules/nanopb
                        )

include_directories("../impl/src/greyp_connectivity"
                    "../impl/src/greyp_messages"
                    "../impl/src/conversion_utilities"
                    "../impl/src/serial_driver"
                    "../impl/src/micro_messaging"
                    $ENV{ZEPHYR_BASE}/../greyp-zephyr-packages/build
                    )

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(cyklaer_test)

FILE(GLOB app_sources ../impl/src/greyp_connectivity/*.cpp
                      ../impl/src/greyp_messages/*.cpp
                      ../impl/src/micro_messaging/*.cpp
                      )
FILE(GLOB test_sources src/*.cpp)
target_sources(app PRIVATE ${test_sources} ${app_sources})
target_link_libraries(app PUBLIC gtest_main)

At this point, prj.conf file in test folder is empty. Running the tests results in the following output:
image

However, when in test prj.conf file, I add

CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y

tests are executed twice:
image

I cannot figure out what is the root cause of this behavior. Is -gtest_repeat=2 set somehow? I don't know really how to debug this and find the reason. Any help is greatly appreciated.

Thanks.

@mejtejt mejtejt added the bug The issue is a bug, or the PR is fixing a bug label Dec 28, 2020
@nashif nashif added the priority: low Low impact/importance bug label Jan 5, 2021
@mejtejt
Copy link
Author

mejtejt commented Jan 7, 2021

There is another piece of information I came across, which I also do not understand why is it happening, and for which I think it is a pre-requisite for C++ configs to work.

In in test/prj.conf file, instead of having

CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y

I put

CONFIG_EXTERNAL_LIBC=n
CONFIG_NEWLIB_LIBC=y

and try to build that, I get the following output:

image

And I cannot figure out why is always external lib taken as C library. How do I instruct it to use new lib instead?

@pabigot
Copy link
Collaborator

pabigot commented Jan 12, 2021

Are you using native_posix as the target board? That's probably the only environment where gtest is going to function at all. For that board you can't select newlib.

@pabigot
Copy link
Collaborator

pabigot commented Jan 12, 2021

One likely cause: Zephyr initializes C++ static objects during the kernel startup code, rather than when the C runtime infrastructure iterates over them before starting. If gtest uses static objects (probably does) then they may be being initialized twice, which might cause them to run twice.

I don't think gtest is likely to work well with C++ in Zephyr, where C++ is not a first-class citizen.

@mejtejt
Copy link
Author

mejtejt commented Jan 19, 2021

Yes, I am using native_posix as target board. So basically, the only way to keep using gtest and C++ would be to somehow pass gtest_repeat = 1 from command line, before starting tests?

@pabigot
Copy link
Collaborator

pabigot commented Jan 19, 2021

If that works, yes. I've never used gtest with Zephyr.

@pabigot
Copy link
Collaborator

pabigot commented Jan 22, 2021

I'm going to close this, because there's a workaround even though the use of gtest with Zephyr and C++ isn't really supported. If there are objections it can be re-opened as a low-priority enhancement.

@pabigot pabigot closed this as completed Jan 22, 2021
@sidmodi-mw
Copy link

@mejtejt I am trying to integrate gtest into my Zephyr project and was hoping you could share some insight on how you integrated the library - did you use gmock as well? And were you able to get past this double running issue? I'm finding it really hard to find any resources on how to do this integration. Any pointers would be much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

No branches or pull requests

4 participants