Search code examples
cmakeemscriptenctestcatch2

how to use CTest with Node js command, for testing JS file compiled from C++ using emscripten, and use Catch2?


I am try to use Catch2 library for testing and compile it with emscripten and run the test. The directory structure of my project look like this

|- CMakeLists.txt
|- build
|   |- ...
|   |- try-test.js
|   |- try-test.wasm
|   |- try-test.wast
|- test
|   |- main.cpp
|- third_party
    |- Catch2
        |- ...

when I am move to build directory and run node try-test.js, it success. but when i am run ctest, it fail. Below is the output message.

Test project /Users/janucaria/Projects/junk/em-cpp-unit-test/build
Start 1: trytest
Could not find executable node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/try-test.js
Looked in the following places:
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Release/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Release/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Debug/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Debug/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/MinSizeRel/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/MinSizeRel/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/RelWithDebInfo/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/RelWithDebInfo/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Deployment/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Deployment/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Development/try-test.js
node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/Development/try-test.js
Unable to find executable: node /Users/janucaria/Projects/junk/em-cpp-unit-test/build/try-test.js
1/1 Test #1: trytest ..........................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.01 sec

The following tests FAILED:
          1 - trytest (Not Run)
Errors while running CTest

Am I missing something here?

here is my test/main.cpp

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

TEST_CASE("Try test")
{
  int foo = 1;
  REQUIRE(foo == 1);
}

and here the CMakeLists.txt

cmake_minimum_required(VERSION 3.11)
project(trytest)

enable_testing()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(try-test "test/main.cpp")

target_include_directories(try-test
PRIVATE
  "${PROJECT_SOURCE_DIR}/third_party/Catch2/single_include"
)

target_compile_options(try-test PRIVATE "-Wall" "-Wpedantic" "-stdlib=libc++")

add_test(
  NAME trytest
  COMMAND "node ${CMAKE_CURRENT_BINARY_DIR}/try-test.js")

I would be grateful for any help you are able to provide.


Solution

  • I figure it out. I am use add_test wrong. It should be

    add_test(
      NAME trytest
      COMMAND node "${CMAKE_CURRENT_BINARY_DIR}/try-test.js")
    

    so the ctest will run command node with arguments ${CMAKE_CURRENT_BINARY_DIR}/try-test.js