繁体   English   中英

VSCode 中的 Google 测试设置帮助

[英]Google Test setup help in VSCode

我不是一个非常有经验的程序员,当我在我的计算机上编写代码时,我使用 Visual Studio。 几周前,我了解了 Visual Studio Code,我非常兴奋并且非常喜欢它。 然而,当谈到测试代码时,这让我很头疼。

在 Visual Studio 中,我使用 Google Test 测试了我的代码,但我发现在 VSCode 中它要复杂得多,我尝试了一切,我看到的每个教程都是为了使用它。 没有运气,而且每次都或多或少发生相同的情况。

例如,假设我根据googletest/README.md上的教程进行设置。 我创建了一个名为 example 的解决方案,其中包含 example.cpp、example.h 和 test.cpp,我们可以在其中找到单元测试。

示例.h:

#pragma once

int Add(int a, int b);

例子.cpp:

#include "example.h"

int Add(int a, int b) {
  return a + b;
}

测试.cpp

#include <gtest/gtest.h>
#include "example.h"

TEST(ExampleTest, AddTest)
{
    EXPECT_EQ(Add(2, 2), 4);
}

然后,我将 googletest 克隆到示例中,创建并打开了一个名为 build 的粘贴,并在终端上使用了$ cmake ../googletest 此时,示例如下所示:

example/
--build/
--googletest/
--example.cpp
--example.h
--test.cpp

当我尝试运行或调试项目时,终端以代码 1 结束并显示错误fatal error C1083: Cannot open include file: 'gtest/gtest.h': No such file or directory ,这很奇怪,因为即使是 IntelliSense可以检测gtest。

然后我尝试使用 CMake 构建,并在示例中创建了一个 CMakeLists.txt,并将其配置为作为可执行文件运行。

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(example VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(example test.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

但是,CMake 抱怨说CMake Error: The source "C:/[...]/Projects/example/CMakeLists.txt" does not match the source "C:/[...]/Projects/example/googletest/CMakeLists.txt" used to generate cache. Re-run cmake with a different source directory. CMake Error: The source "C:/[...]/Projects/example/CMakeLists.txt" does not match the source "C:/[...]/Projects/example/googletest/CMakeLists.txt" used to generate cache. Re-run cmake with a different source directory.

我阅读了 Stack Overflow 和其他网站上的几乎所有内容,但没有任何帮助。 我究竟做错了什么?

编辑

另外,这是tasks.json:

{
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: cl.exe build active file",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/Fe:",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "${file}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$msCompile"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ],
  "version": "2.0.0"
}
编辑 - -

当我使用 /IC:/[...]/Projects/example/googletest/googletest/include/link C:/[...]/Projects/example/example/build/Debug/example.lib C:/[...]/Projects/example/test.obj (我将 example.cpp 和 example.h 移动到名为 example 的文件中,并通过 CMake 构建了一个库),现在我得到了这个:

 
 
 
 
  
  
  /debug /out:c:\\[...]\\Projects\\example/test.exe C:/[...]/Projects/example/example/build/Debug/example.lib C:/[...]/Projects/example/test.obj test.obj test.obj : warning LNK4042: object specified more than once; extras ignored LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library test.obj : error LNK2019: unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue@internal@testing@@YA_N_N@Z) referenced in function "public: static void (__cdecl*__cdecl testing::internal::SuiteApiResolver<class testing::Test>::GetSetUpCaseOrSuite(char const *,int))(void)" (?GetSetUpCaseOrSuite@?$SuiteApiResolver@VTest@testing@@@internal@testing@@SAP6AXXZPEBDH@Z) test.obj : error LNK2019: unresolved external symbol "public: __cdecl testing::internal::GTestLog::GTestLog(enum testing::internal::GTestLogSeverity,char const *,int)" (??0GTestLog@internal@testing@@QEAA@W4GTestLogSeverity@12@PEBDH@Z) referenced in function "public: static void (__cdecl*__cdecl testing::internal::SuiteApiResolver<class testing::Test>::GetSetUpCaseOrSuite(char const *,int))(void)" (?GetSetUpCaseOrSuite@?$SuiteApiResolver@VTest@testing@@@internal@testing@@SAP6AXXZPEBDH@Z) test.obj : error LNK2019: unresolved external symbol "public: __cdecl testing::internal::GTestLog::~GTestLog(void)" (??1GTestLog@internal@testing@@QEAA@XZ) referenced in function "public: static void (__cdecl*__cdecl testing::internal::SuiteApiResolver<class testing::Test>::GetSetUpCaseOrSuite(char const *,int))(void)" (?GetSetUpCaseOrSuite@?$SuiteApiResolver@VTest@testing@@@internal@testing@@SAP6AXXZPEBDH@Z) test.obj : error LNK2019: unresolved external symbol "public: __cdecl testing::Message::Message(void)" (??0Message@testing@@QEAA@XZ) referenced in function "private: virtual void __cdecl ExampleTest_AddTest_Test::TestBody(void)" (?TestBody@ExampleTest_AddTest_Test@@EEAAXXZ) test.obj : error LNK2019: unresolved external symbol "class testing::AssertionResult __cdecl testing::internal::EqFailure(char const *,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?EqFailure@internal@testing@@YA?AVAssertionResult@2@PEBD0AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@1_N@Z) referenced in function "class testing::AssertionResult __cdecl testing::internal::CmpHelperEQFailure<int,int>(char const *,char const *,int const &,int const &)" (??$CmpHelperEQFailure@HH@internal@testing@@YA?AVAssertionResult@1@PEBD0AEBH1@Z) test.obj : error LNK2019: unresolved external symbol "void const * __cdecl testing::internal::GetTestTypeId(void)" (?GetTestTypeId@internal@testing@@YAPEBXXZ) referenced in function "void __cdecl `dynamic initializer for 'private: static class testing::TestInfo * ExampleTest_AddTest_Test::test_info_''(void)" (??__E?test_info_@ExampleTest_AddTest_Test@@0QEAVTestInfo@testing@@EA@@YAXXZ) test.obj : error LNK2019: unresolved external symbol "class testing::TestInfo * __cdecl testing::internal::MakeAndRegisterTestInfo(char const *,char const *,char const *,char const *,struct testing::internal::CodeLocation,void const *,void (__cdecl*)(void),void (__cdecl*)(void),class testing::internal::TestFactoryBase *)" (?MakeAndRegisterTestInfo@internal@testing@@YAPEAVTestInfo@2@PEBD000UCodeLocation@12@PEBXP6AXXZ3PEAVTestFactoryBase@12@@Z) referenced in function "void __cdecl `dynamic initializer for 'private: static class testing::TestInfo * ExampleTest_AddTest_Test::test_info_''(void)" (??__E?test_info_@ExampleTest_AddTest_Test@@0QEAVTestInfo@testing@@EA@@YAXXZ) test.obj : error LNK2019: unresolved external symbol "class testing::AssertionResult __cdecl testing::AssertionSuccess(void)" (?AssertionSuccess@testing@@YA?AVAssertionResult@1@XZ) referenced in function "class testing::AssertionResult __cdecl testing::internal::CmpHelperEQ<int,int>(char const *,char const *,int const &,int const &)" (??$CmpHelperEQ@HH@internal@testing@@YA?AVAssertionResult@1@PEBD0AEBH1@Z) test.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl testing::Test::~Test(void)" (??1Test@testing@@UEAA@XZ) referenced in function "public: virtual __cdecl ExampleTest_AddTest_Test::~ExampleTest_AddTest_Test(void)" (??1ExampleTest_AddTest_Test@@UEAA@XZ) test.obj : error LNK2019: unresolved external symbol "protected: __cdecl testing::Test::Test(void)" (??0Test@testing@@IEAA@XZ) referenced in function "public: __cdecl ExampleTest_AddTest_Test::ExampleTest_AddTest_Test(void)" (??0ExampleTest_AddTest_Test@@QEAA@XZ) test.obj : error LNK2001: unresolved external symbol "protected: virtual void __cdecl testing::Test::SetUp(void)" (?SetUp@Test@testing@@MEAAXXZ) test.obj : error LNK2001: unresolved external symbol "protected: virtual void __cdecl testing::Test::TearDown(void)" (?TearDown@Test@testing@@MEAAXXZ) test.obj : error LNK2019: unresolved external symbol "public: __cdecl testing::internal::AssertHelper::AssertHelper(enum testing::TestPartResult::Type,char const *,int,char const *)" (??0AssertHelper@internal@testing@@QEAA@W4Type@TestPartResult@2@PEBDH1@Z) referenced in function "private: virtual void __cdecl ExampleTest_AddTest_Test::TestBody(void)" (?TestBody@ExampleTest_AddTest_Test@@EEAAXXZ) test.obj : error LNK2019: unresolved external symbol "public: __cdecl testing::internal::AssertHelper::~AssertHelper(void)" (??1AssertHelper@internal@testing@@QEAA@XZ) referenced in function "private: virtual void __cdecl ExampleTest_AddTest_Test::TestBody(void)" (?TestBody@ExampleTest_AddTest_Test@@EEAAXXZ) test.obj : error LNK2019: unresolved external symbol "public: void __cdecl testing::internal::AssertHelper::operator=(class testing::Message const &)const " (??4AssertHelper@internal@testing@@QEBAXAEBVMessage@2@@Z) referenced in function "private: virtual void __cdecl ExampleTest_AddTest_Test::TestBody(void)" (?TestBody@ExampleTest_AddTest_Test@@EEAAXXZ) c:\\[...]\\Projects\\example\\test.exe : fatal error LNK1120: 15 unresolved externals The terminal process "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -Command cl.exe /Zi /EHsc /Fe: c:\\[...]\\Projects\\example/test.exe c:\\[...]\\Projects\\example\\test.cpp /IC:/[...]/Projects/example/googletest/googletest/include '/link C:/[...]/Projects/example/example/build/Debug/example.lib C:/[...]/Projects/example/test.obj'" terminated with exit code: 1.
 
 
 

我想出了如何解决这个问题。 长话短说,我使用 CMake 将 Google Test 包含在项目中。

首先,我将 example.cpp 和 example.h 移动到一个名为 example 的新文件夹中,并创建了一个 CMakeLists.txt 以编译为一个库。

示例/CMakeLists.txt:

add_library(example_lib example.cpp example.h)

然后,我将根目录中的 CMakeLists.txt 修改为:

cmake_minimum_required(VERSION 3.18.2) # Set your version.
# If you don't, wou'll get a LNK1169 error and many others (don't know why)

project(example)

include(CTest)
enable_testing()

add_executable(example test.cpp)

add_subdirectory(googletest)
add_subdirectory(example)

target_link_libraries(example PUBLIC example_lib gtest gtest_main gmock gmock_main)

include(GoogleTest) # To use gtest_discover_tests

gtest_discover_tests(example) # Runs GTest with CTest

现在您可以配置、构建和运行! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM