Google VSCode 中的测试设置帮助
Google Test setup help in VSCode
我不是一个非常有经验的程序员,当我在我的电脑上写代码时,我使用 Visual Studio。几周前,我学习了 Visual Studio 代码,我非常兴奋,我非常喜欢它。但是,一提到测试代码,就很头疼了。
在 Visual Studio 中,我用 Google Test 测试了我的代码,但我发现在 VSCode 中它要复杂得多,我尝试了所有方法,我看到的每个教程为了使用它。运气不好,每次都差不多。
例如,假设我根据 googletest/README.md 上的教程进行设置。我创建了一个名为 example 的解决方案,其中包含 example.cpp、example.h 和 test.cpp,我们可以在其中找到单元测试。
example.h:
#pragma once
int Add(int a, int b);
example.cpp:
#include "example.h"
int Add(int a, int b) {
return a + b;
}
test.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.
我几乎阅读了 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 测试。
首先,我将 example.cpp 和 example.h 移动到一个名为 example 的新文件夹中,并创建了一个 CMakeLists.txt 来编译为一个库。
example/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
现在您可以配置、构建和 运行! :)
我不是一个非常有经验的程序员,当我在我的电脑上写代码时,我使用 Visual Studio。几周前,我学习了 Visual Studio 代码,我非常兴奋,我非常喜欢它。但是,一提到测试代码,就很头疼了。
在 Visual Studio 中,我用 Google Test 测试了我的代码,但我发现在 VSCode 中它要复杂得多,我尝试了所有方法,我看到的每个教程为了使用它。运气不好,每次都差不多。
例如,假设我根据 googletest/README.md 上的教程进行设置。我创建了一个名为 example 的解决方案,其中包含 example.cpp、example.h 和 test.cpp,我们可以在其中找到单元测试。
example.h:
#pragma once
int Add(int a, int b);
example.cpp:
#include "example.h"
int Add(int a, int b) {
return a + b;
}
test.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.
我几乎阅读了 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 测试。
首先,我将 example.cpp 和 example.h 移动到一个名为 example 的新文件夹中,并创建了一个 CMakeLists.txt 来编译为一个库。
example/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
现在您可以配置、构建和 运行! :)