如何将 CTest 与 Node js 命令一起使用,以测试使用 emscripten 从 C++ 编译的 JS 文件,并使用 Catch2?

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

我正在尝试使用 Catch2 库进行测试,并使用 emscripten 和 运行 进行编译。我项目的目录结构是这样的

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

当我移动到 build 目录和 运行 node try-test.js 时,它成功了。但是当我 运行 ctest 时,它失败了。下面是输出消息。

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

我是不是漏掉了什么?

这是我的 test/main.cpp

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

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

这里是 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")

如果您能提供任何帮助,我将不胜感激。

我明白了。我使用 add_test 错误。应该是

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

因此 ctest 将 运行 使用参数 ${CMAKE_CURRENT_BINARY_DIR}/try-test.js

命令 node