测试失败,但通过qtcreator进行

发布于 2025-02-02 19:03:15 字数 5584 浏览 2 评论 0原文

我正在尝试使用cmake在Windows中设置一个简单的QT项目(用QTest编写)。
我可以毫无问题地编译项目,并执行 QT创建者内部测试

当我尝试使用ctest(ctest -c release -o-output-on-failure -test-dir c:\ src \ mytests \ test_so \ build)时,问题就到了。以故障结束(退出代码0xc000000135)。

如何像QT创建者中那样进行测试以通过CTEST进行?
如果可能的话,我想避免将所有需要的DLL复制到可执行文件夹中。

测试输出

QT创建者

“

CTEST

Internal ctest changing into directory: C:/SRC/MyTests/Test_SO/build
Test project C:/SRC/MyTests/Test_SO/build
    Start 1: tst_MyTest
1/1 Test #1: tst_MyTest .......................Exit code 0xc0000135
***Exception:   0.02 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.02 sec

The following tests FAILED:
          1 - tst_MyTest (Exit code 0xc0000135
)

尝试

  1. 安装DIR作为工作目录:- build-run-dir c:\ qt \ 5.12.10 \ msvccc2017_64
  2. 告诉CTEST使用Qt的 QT的安装目录到主cmake文件中的Windows路径:
    set(CUSTOM_PATH "${CMAKE_BASE_DIR}/bin;$ENV{PATH}")
    cmake_path(CONVERT "${CUSTOM_PATH}" TO_NATIVE_PATH_LIST CUSTOM_PATH NORMALIZE)
    string(REPLACE ";" "\\;" CUSTOM_PATH "${CUSTOM_PATH}")
    set(ENV{PATH} "${CUSTOM_PATH}")
  1. 设置环境直接在测试级别
list(APPEND QT_DLL_PATH "${QT_BASE_DIR}/bin")
cmake_path(APPEND_STRING QT_DLL_PATH ";$ENV{PATH}")
cmake_path(CONVERT "${QT_DLL_PATH}" TO_NATIVE_PATH_LIST QT_DLL_PATH NORMALIZE)
string(REPLACE ";" "\\;" QT_DLL_PATH "${QT_DLL_PATH}")

add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}" WORKING_DIRECTORY "${QT_DLL_PATH}")
set_tests_properties("${TEST_EXECUTABLE_NAME}" PROPERTIES ENVIRONMENT "PATH=${QT_DLL_PATH}")

项目结构

src/
|_ CMakeLists.txt
|_ HelloWorld/
|    |_CMakeLists.txt
|    |_HelloWorld.h
|    |_HelloWorld.cpp
|_Tests
     |_CMakeLists.txt
     |_MyTest/
           |_MyTest.cpp

源cmakelists.txt

cmake_minimum_required(VERSION 3.15 FATAL_ERROR)

project(TestSO VERSION 1.0.0 LANGUAGES CXX DESCRIPTION "A test project with CMake")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(BUILD_SHARED_LIBS ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Initial guess for Qt's installation path.
cmake_path(SET QT_BASE_DIR NORMALIZE "C:/Qt/5.12.10/msvc2017_64")
cmake_path(SET QT_INSTALL_DIR NORMALIZE "${QT_BASE_DIR}/lib/cmake/Qt5")
set(Qt5_DIR "${QT_INSTALL_DIR}" CACHE PATH "Path to the Qt installation folder with the CMake configuration files." FORCE)

# TEST STILL FAILS #
# set(CUSTOM_PATH "${CMAKE_BASE_DIR}/bin;$ENV{PATH}")
# cmake_path(CONVERT "${CUSTOM_PATH}" TO_NATIVE_PATH_LIST CUSTOM_PATH NORMALIZE)
# string(REPLACE ";" "\\;" CUSTOM_PATH "${CUSTOM_PATH}")
# set(ENV{PATH} "${CUSTOM_PATH}")

find_package(Qt5 REQUIRED COMPONENTS
    Core
    Widgets
    QuickWidgets
)
set(QT_DIR "${Qt5_DIR}")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

add_subdirectory(HelloWorld)

enable_testing()
add_subdirectory(Tests)

helloworld cmakelists.txt

set(LIB_NAME HelloWorldLib)

add_library("${LIB_NAME}" STATIC
    HelloWorld.h
    HelloWorld.cpp
)

target_link_libraries("${LIB_NAME}" PUBLIC Qt5::Core)
target_include_directories("${LIB_NAME}" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

helloworld.h

#ifndef HELLOWORLD_H
#define HELLOWORLD_H

#include <QObject>
#include <QString>

class HelloWorld final : public QObject
{
    Q_OBJECT
public:
    HelloWorld() = default;

    QString getMsg() const;

private slots:
    void myslot();
};

#endif // HELLOWORLD_H

helloworld helloworld .CPP

#include "HelloWorld.h"

#include <QDebug>

QString HelloWorld::getMsg() const
{
    return "HELLO WORLD";
}

void HelloWorld::myslot()
{
    qDebug() << "SLOT";
}

测试cmakelists.txt

find_package(Qt5 REQUIRED COMPONENTS Test)

# TEST STILL FAILS #
#list(APPEND QT_DLL_PATH "${QT_BASE_DIR}/bin")
#cmake_path(APPEND_STRING QT_DLL_PATH ";$ENV{PATH}")
#cmake_path(CONVERT "${QT_DLL_PATH}" TO_NATIVE_PATH_LIST QT_DLL_PATH NORMALIZE)
## this is the vital line, without it CMake set_tests_properties mangles the ENVIRONMENT
#string(REPLACE ";" "\\;" QT_DLL_PATH "${QT_DLL_PATH}")
#

set(TEST_NAME "MyTest")
set(TEST_EXECUTABLE_NAME "tst_${TEST_NAME}")
add_executable("${TEST_EXECUTABLE_NAME}" "MyTest/${TEST_NAME}.cpp")
target_include_directories("${TEST_EXECUTABLE_NAME}" PRIVATE .)
target_link_libraries("${TEST_EXECUTABLE_NAME}" PRIVATE HelloWorldLib PRIVATE Qt5::Test )
add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}")

# TEST STILL FAILS #
# add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}" WORKING_DIRECTORY "${QT_DLL_PATH}")
#set_tests_properties("${TEST_EXECUTABLE_NAME}" PROPERTIES ENVIRONMENT "PATH=${QT_DLL_PATH}")
#

测试helloworld

#include <QtTest/QTest>

#include "HelloWorld.h"


class MyTest : public QObject
{
    Q_OBJECT
private slots:
    void init()
    {
        qDebug() << "INIT";
    }

    void test_MyTest()
    {
        QCOMPARE(true, true);
        HelloWorld h;
        QCOMPARE(h.getMsg(), "HELLO WORLD");
    }

};

QTEST_MAIN(MyTest);
#include "MyTest.moc"

I am trying to set up a simple Qt project with a unit test (written in QTest) using CMake in Windows.
I can compile the project without problem and execute the test inside Qt Creator.

The problem comes when I try to run the test using CTest (ctest -C Release --output-on-failure --test-dir C:\SRC\MyTests\Test_SO\build), as it ends in failure (exit code 0xc0000135).

How can I make the tests to pass in CTest like they do in Qt Creator?
If possible, I'd like to avoid having to copy all DLLs needed to the executable folder.

Test output

Qt Creator

test_success_qtcreator

CTest

Internal ctest changing into directory: C:/SRC/MyTests/Test_SO/build
Test project C:/SRC/MyTests/Test_SO/build
    Start 1: tst_MyTest
1/1 Test #1: tst_MyTest .......................Exit code 0xc0000135
***Exception:   0.02 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.02 sec

The following tests FAILED:
          1 - tst_MyTest (Exit code 0xc0000135
)

Things tried

  1. Tell CTest to use Qt's installation dir as working directory: --build-run-dir C:\Qt\5.12.10\msvc2017_64\bin
  2. Add Qt's installation directory to the Windows path in the main CMake file:
    set(CUSTOM_PATH "${CMAKE_BASE_DIR}/bin;$ENV{PATH}")
    cmake_path(CONVERT "${CUSTOM_PATH}" TO_NATIVE_PATH_LIST CUSTOM_PATH NORMALIZE)
    string(REPLACE ";" "\\;" CUSTOM_PATH "${CUSTOM_PATH}")
    set(ENV{PATH} "${CUSTOM_PATH}")
  1. Set the ENVIRONMENT property directly at test level:
list(APPEND QT_DLL_PATH "${QT_BASE_DIR}/bin")
cmake_path(APPEND_STRING QT_DLL_PATH ";$ENV{PATH}")
cmake_path(CONVERT "${QT_DLL_PATH}" TO_NATIVE_PATH_LIST QT_DLL_PATH NORMALIZE)
string(REPLACE ";" "\\;" QT_DLL_PATH "${QT_DLL_PATH}")

add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}" WORKING_DIRECTORY "${QT_DLL_PATH}")
set_tests_properties("${TEST_EXECUTABLE_NAME}" PROPERTIES ENVIRONMENT "PATH=${QT_DLL_PATH}")

Project structure

src/
|_ CMakeLists.txt
|_ HelloWorld/
|    |_CMakeLists.txt
|    |_HelloWorld.h
|    |_HelloWorld.cpp
|_Tests
     |_CMakeLists.txt
     |_MyTest/
           |_MyTest.cpp

Source CMakeLists.txt

cmake_minimum_required(VERSION 3.15 FATAL_ERROR)

project(TestSO VERSION 1.0.0 LANGUAGES CXX DESCRIPTION "A test project with CMake")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(BUILD_SHARED_LIBS ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Initial guess for Qt's installation path.
cmake_path(SET QT_BASE_DIR NORMALIZE "C:/Qt/5.12.10/msvc2017_64")
cmake_path(SET QT_INSTALL_DIR NORMALIZE "${QT_BASE_DIR}/lib/cmake/Qt5")
set(Qt5_DIR "${QT_INSTALL_DIR}" CACHE PATH "Path to the Qt installation folder with the CMake configuration files." FORCE)

# TEST STILL FAILS #
# set(CUSTOM_PATH "${CMAKE_BASE_DIR}/bin;$ENV{PATH}")
# cmake_path(CONVERT "${CUSTOM_PATH}" TO_NATIVE_PATH_LIST CUSTOM_PATH NORMALIZE)
# string(REPLACE ";" "\\;" CUSTOM_PATH "${CUSTOM_PATH}")
# set(ENV{PATH} "${CUSTOM_PATH}")

find_package(Qt5 REQUIRED COMPONENTS
    Core
    Widgets
    QuickWidgets
)
set(QT_DIR "${Qt5_DIR}")

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

add_subdirectory(HelloWorld)

enable_testing()
add_subdirectory(Tests)

HelloWorld CMakeLists.txt

set(LIB_NAME HelloWorldLib)

add_library("${LIB_NAME}" STATIC
    HelloWorld.h
    HelloWorld.cpp
)

target_link_libraries("${LIB_NAME}" PUBLIC Qt5::Core)
target_include_directories("${LIB_NAME}" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

HelloWorld.h

#ifndef HELLOWORLD_H
#define HELLOWORLD_H

#include <QObject>
#include <QString>

class HelloWorld final : public QObject
{
    Q_OBJECT
public:
    HelloWorld() = default;

    QString getMsg() const;

private slots:
    void myslot();
};

#endif // HELLOWORLD_H

HelloWorld.cpp

#include "HelloWorld.h"

#include <QDebug>

QString HelloWorld::getMsg() const
{
    return "HELLO WORLD";
}

void HelloWorld::myslot()
{
    qDebug() << "SLOT";
}

Test CMakeLists.txt

find_package(Qt5 REQUIRED COMPONENTS Test)

# TEST STILL FAILS #
#list(APPEND QT_DLL_PATH "${QT_BASE_DIR}/bin")
#cmake_path(APPEND_STRING QT_DLL_PATH ";$ENV{PATH}")
#cmake_path(CONVERT "${QT_DLL_PATH}" TO_NATIVE_PATH_LIST QT_DLL_PATH NORMALIZE)
## this is the vital line, without it CMake set_tests_properties mangles the ENVIRONMENT
#string(REPLACE ";" "\\;" QT_DLL_PATH "${QT_DLL_PATH}")
#

set(TEST_NAME "MyTest")
set(TEST_EXECUTABLE_NAME "tst_${TEST_NAME}")
add_executable("${TEST_EXECUTABLE_NAME}" "MyTest/${TEST_NAME}.cpp")
target_include_directories("${TEST_EXECUTABLE_NAME}" PRIVATE .)
target_link_libraries("${TEST_EXECUTABLE_NAME}" PRIVATE HelloWorldLib PRIVATE Qt5::Test )
add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}")

# TEST STILL FAILS #
# add_test(NAME "${TEST_EXECUTABLE_NAME}" COMMAND "${TEST_EXECUTABLE_NAME}" WORKING_DIRECTORY "${QT_DLL_PATH}")
#set_tests_properties("${TEST_EXECUTABLE_NAME}" PROPERTIES ENVIRONMENT "PATH=${QT_DLL_PATH}")
#

Test HelloWorld

#include <QtTest/QTest>

#include "HelloWorld.h"


class MyTest : public QObject
{
    Q_OBJECT
private slots:
    void init()
    {
        qDebug() << "INIT";
    }

    void test_MyTest()
    {
        QCOMPARE(true, true);
        HelloWorld h;
        QCOMPARE(h.getMsg(), "HELLO WORLD");
    }

};

QTEST_MAIN(MyTest);
#include "MyTest.moc"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文