Unit testing for platform

Hello everyone

I am building my package for openwrt platform and I want to create tests for my product in C++17.

Can you please tell me if there are any instructions or manuals on how to install any test systems on openwrt, such as google test, catch2 and similar?

Thanks

Just repeat good work. For super small systems you will have to interpolate that they would work from 2x bigger system.

Problem solved!

For memory and for those who will face a similar problem I publish here the solution on implementing Google Test:

Project structure

app_name/
app_name/src/ your_app_name .cpp
app_name/src/hpp/ your_app_name .hpp
app_name/src/libs/ your_lib.cpp
app_name/src/test/test_ your_app_name .hpp
app_name/Makefile

app_name/Makefile

include $(TOPDIR)/rules.mk

PKG_NAME:=your_app_name
PKG_VERSION:=0.0.1
PKG_RELEASE:=alpha-1

PKG_MAINTAINER:=your_name
PKG_LICENSE:=type_your_licence
PKG_LICENSE_FILES:=path_to_you_licence_file

# Enable default test collection
BUILD_TESTING:=1

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:= Your title here
    URL:= https://github.com/you_github_login/you_app
    DEPENDS:=+libstdcpp
endef

define Package/$(PKG_NAME)/description
    Your app’s description here 
endef

# Переменные для Google Tests
GTEST_DIR := $(PKG_BUILD_DIR)
GTEST_LAST_VER=1.12.1
GTEST_URL := https://github.com/google/googletest/archive/refs/tags/release-$(GTEST_LAST_VER).tar.gz
GTEST_TAR := googletest-release-$(GTEST_LAST_VER).tar.gz
GTEST_EXTRACTED_DIR := $(GTEST_DIR)/googletest-release-$(GTEST_LAST_VER)

define Build/Prepare

    mkdir -p $(PKG_BUILD_DIR)
    cp -r $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
    echo "===> Build Architecture: $(CONFIG_ARCH)"

    # Google Test Preparing
    if [ ! -d $(GTEST_EXTRACTED_DIR) ]; then \
        mkdir -p $(GTEST_EXTRACTED_DIR); \
        wget $(GTEST_URL) -O $(GTEST_DIR)/$(GTEST_TAR); \
        tar -xzf $(GTEST_DIR)/$(GTEST_TAR) -C $(GTEST_DIR); \
        rm -f $(GTEST_DIR)/$(GTEST_TAR); \
    fi
endef

define Build/Configure

    # Google Test Configuration
    if [ "$(BUILD_TESTING)" = "1" ]; then \
        echo "===> Google Test Configuration..." && \
        mkdir -p $(GTEST_EXTRACTED_DIR)/build && \
        cd $(GTEST_EXTRACTED_DIR)/build && \
        cmake .. \
            -DCMAKE_C_COMPILER=$(TARGET_CC) \
            -DCMAKE_CXX_COMPILER=$(TARGET_CXX) \
            -DCMAKE_SYSTEM_NAME=Linux \
            -DCMAKE_SYSTEM_PROCESSOR=$(CONFIG_ARCH) \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_BUILD_TYPE=Release \
            -DBUILD_SHARED_LIBS=OFF \
            -G "Unix Makefiles"; \
    fi
    
endef

define Build/Compile

    # Сборка Google Test
    if [ "$(BUILD_TESTING)" = "1" ]; then \
        echo "===> Google Test compilation..."; \
        $(MAKE) -C $(GTEST_EXTRACTED_DIR)/build; \
    fi

    # Compilation of the your app 
    echo "===>  Compilation of the main program..."; \
    $(TARGET_CXX) $(TARGET_CPPFLAGS) $(TARGET_CXXFLAGS) \
        -o $(PKG_BUILD_DIR)/$(PKG_NAME) \
        $(PKG_BUILD_DIR)/$(PKG_NAME).cpp \
        $(PKG_BUILD_DIR)/libs/your_lib.cpp \
        $(TARGET_LDFLAGS) -std=c++17 -Wall \
        -I$(PKG_BUILD_DIR)/hpp

    # Test compilation
    if [ "$(BUILD_TESTING)" = "1" ]; then \
        echo "===> Test compilation..."; \
        $(TARGET_CXX) \
        $(TARGET_CPPFLAGS) \
        -I$(PKG_BUILD_DIR)/hpp \
        -I$(GTEST_EXTRACTED_DIR)/googletest/include \
        -I$(GTEST_EXTRACTED_DIR)/googletest \
        $(PKG_BUILD_DIR)/tests/test_$(PKG_NAME).cpp \
        $(PKG_BUILD_DIR)/libs/your_lib.cpp \
        -o $(PKG_BUILD_DIR)/test_$(PKG_NAME) \
        $(TARGET_LDFLAGS) \
        $(GTEST_EXTRACTED_DIR)/build/lib/libgtest.a \
        $(GTEST_EXTRACTED_DIR)/build/lib/libgtest_main.a \
        -lpthread \
        -DUNIT_TEST; \
    fi
endef

define Package/$(PKG_NAME)/install

    echo "===> Package installing $(PKG_NAME)..."
    $(INSTALL_DIR) $(1)/opt/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/opt/bin/

    # Google Test installing
    if [ "$(BUILD_TESTING)" = "1" ]; then \
        echo "===> Google Test installing..."
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/test_$(PKG_NAME) $(1)/opt/bin/; \
    fi
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

With the above project structure and the contents of the manifest file, the application and tests are fully built and running on the target device.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.