Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用Cmake和Ctest建立Qt测试_C++_Qt_Unit Testing_Cmake_Ctest - Fatal编程技术网

C++ 用Cmake和Ctest建立Qt测试

C++ 用Cmake和Ctest建立Qt测试,c++,qt,unit-testing,cmake,ctest,C++,Qt,Unit Testing,Cmake,Ctest,我最近开始探索使用QT库进行单元测试,所以我只想重复its中的简单代码,并使用Cmake构建它(我有msvc编译器,并使用CLion IDE) Cmake: cmake_minimum_required(VERSION 3.17) project(unit_tests) find_package(Qt5Test REQUIRED) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) enable_testing(true) ad

我最近开始探索使用QT库进行单元测试,所以我只想重复its中的简单代码,并使用Cmake构建它(我有msvc编译器,并使用CLion IDE)

Cmake:

cmake_minimum_required(VERSION 3.17)
project(unit_tests)
find_package(Qt5Test REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)

enable_testing(true)

add_executable(mytest tst_mytest.cpp)
add_test(mytest mytest)

target_link_libraries(mytest PRIVATE Qt5::Test)
tst_mytest.cpp:

#include <QObject>
#include <QtTest/QTest>
class MyFirstTest: public QObject
{
    Q_OBJECT

private:
    bool myCondition()
    {
        return true;
    }

private slots:
    void initTestCase()
    {
        qDebug("Called before everything else.");
    }

    void myFirstTest()
    {
        QVERIFY(true);
        QCOMPARE(1, 1);
    }

    void mySecondTest()
    {
        QVERIFY(myCondition());
        QVERIFY(1 != 2);
    }

    void cleanupTestCase()
    {
        qDebug("Called after myFirstTest and mySecondTest.");
    }
};
QTEST_MAIN(MyFirstTest)
#include "tst_mytest.moc"
由于某些原因,该试验不适合夹具要求。 我寻找了一些其他的和关于单元测试结构的信息,但没有发现我的代码有很多不同之处。将Qt目录添加到Path环境变量后,出现链接错误:

CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

这里出了什么问题?

退出代码
0xc0000135
表示未找到DLL。您需要将Qt binary directory添加到PATH环境变量。您能告诉我如何在CLion中执行此操作吗?这有帮助吗?确切地说,我应该在“环境”字段中写入path=?还有一个问题:我在哪里可以找到qt_目录?假设您的qt位于C:\qt\5.15.2\msvc2019_64中,请将PATH=C:\qt\5.15.2\msvc2019_64\bin添加到此字段。尝试检查Qt安装目录的cmakcache.txt。
CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.