Run test on windows then build to ESP32

Hi I working with ESP32C3 C++ and used to work in Eclipse-Espressif IDE + manually launched Google Tests for not embedded specific buisness logic. Testing without uploading speeds up some parts. I want to switch to VSC/Platformio.

I want to have option to add selected files and test them by running on windows.

How can I achieve this? My first idea was to have 2 envs in .ini file:

[env:native]
platform = native
test_framework = googletest

[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = espidf
monitor_speed = 115200
;test_framework = googletest

But after launching test icon from bottom bar it includes main.cpp and fails.

Actually I dont want to include main.cpp in Uni Tests. In the mentioned manual solutions I had cmake in which I was adding files to be testd like so:

cmake_minimum_required(VERSION 3.14)

# set project name - will be used later
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(${PROJECT_NAME}
  ${PROJECT_SOURCE_DIR}/test_main.cpp
  ${PROJECT_SOURCE_DIR}/test_ringbuffer.cpp
  ${PROJECT_SOURCE_DIR}/test_service_data.cpp
  ${PROJECT_SOURCE_DIR}/test_commands_protocol.cpp
  ${PROJECT_SOURCE_DIR}/test_netw_objects.cpp
  ${PROJECT_SOURCE_DIR}/test_payload.cpp
  
  ${PROJECT_SOURCE_DIR}/../Company/Src/RingBuffer.cpp
  ${PROJECT_SOURCE_DIR}/../Company/Src/service_data.cpp
  ${PROJECT_SOURCE_DIR}/../Company/Src/Payload.cpp
  ${PROJECT_SOURCE_DIR}/../Company/Src/networking_objects.cpp
  ${PROJECT_SOURCE_DIR}/../Company/Src/gui_event.cpp
  ${PROJECT_SOURCE_DIR}/../cJSON/Src/cJSON.c
)

target_link_libraries(${PROJECT_NAME}
  GTest::gtest_main
)

target_include_directories(${PROJECT_NAME}
PRIVATE ${PROJECT_SOURCE_DIR}/../Company/Inc
PRIVATE ${PROJECT_SOURCE_DIR}/../cJSON/Inc
)

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME})

Seems I partially solved my problem.
I added subdir in test folder, added buisness logic in as library, main.cpp is only entrance with 2 parth separated by buil flag, and finally ini file:

[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = espidf
monitor_speed = 115200
test_framework = googletest
test_ignore = test_desktop test_animations
build_flags = -D USING_ESPIDF=1

[env:native]
platform = native
test_framework = googletest
build_flags = -D USING_NATIVE=1

Now if I use test icon with native filter tests are compiled and run on native, and pressing build/launch compiles for target and uploads.

The entrance is not so handsome but works:

#ifdef USING_ESPIDF
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Device.h"

Device device;

extern "C" {
    void app_main() {
        device.init();

        while (1) {
            device.tick();
            vTaskDelay(1);
        }
    }
}
#endif

#ifdef USING_NATIVE
int main(int argc, char **argv) {
    return 0;
}
#endif

After many tries and errors, digging internet and chatGPT help i created template for Google Test for native components with ESP-IDF target. Hope it will be helpfull: