无法通过Google测试工作

无法通过Google测试工作

本文介绍了无法通过Google测试工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "gtest/gtest.h"

TEST(BattleUnitTest, CountryReturnsProperName) {
    EXPECT_EQ(1, 1);
}

int main(int argc, char* argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

我似乎无法让Google测试正常工作.我运行Nuget软件包管理器来获取gtest.它不断给我这些错误:

I can't seem to get google test to work. I ran Nuget package manager to get gtest. It keeps giving me these errors:

Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue@internal@testing@@YA_N_N@Z) referenced in function "public: void __thiscall testing::internal::scoped_ptr<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::reset(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?reset@?$scoped_ptr@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@internal@testing@@QAEXPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

.

    Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "public: __thiscall testing::Message::Message(void)" (??0Message@testing@@QAE@XZ) referenced in function "private: virtual void __thiscall BattleUnitTest_CountryReturnsProperName_Test::TestBody(void)" (?TestBody@BattleUnitTest_CountryReturnsProperName_Test@@EAEXXZ)    Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

.

Severity    Code    Description Project File    Line
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@PBD0ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@1_N@Z) 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@PBD0ABH1@Z) Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

我可以发布所有错误,但几乎所有错误都与LNK2019有关.有人知道如何解决这些错误吗?

I could post all the errors but almost all of them have to do with LNK2019. Does anyone know how to fix these errors?

推荐答案

看起来您需要对测试项目进行一些其他配置:

Looks like you need some additional configuration of your test project:

  1. 将gtest库名称添加到项目属性(项目属性"->配置属性"->链接器"->输入").请注意,您可能需要同时为Debug和Release配置添加此内容,并且库名称是不同的(对于Release,它们是gtest.lib;对于Debug,它们是gtestd.lib;在名称末尾的区别是"d"点).
  2. gtest库的路径添加到项目属性(项目属性"->配置属性"->"VC ++目录"->库目录").对于Debug和Release配置,这也需要单独完成,因为目录也不同.
  1. Add gtest library name to you project properties (Project Properties->Configuration Properties->Linker->Input). Be aware that you probably need to add this thing for both Debug and Release configurations and that library names are different (they are gtest.lib for Release and gtestd.lib for Debug; the difference is "d" at the end of the name before dot).
  2. Add the path to the gtest library to your project properties (Project Properties->Configuration Properties->VC++ Directories->Library Directories). This also needs to be done separately for Debug and Release configurations, because the directories are different as well.

如果出现其他问题,请文章可能真的有用.它包含在不使用Nuget的情况下在Visual Studio中为项目设置gtest的完整方案.请注意,Nuget基本上将gtest的已编译版本设置为项目/解决方案的打包"子目录.

If something else goes wrong, this article might be really useful. It contains the full scenario of setting up gtest for a project in Visual Studio without using Nuget. Just be aware that Nuget basically sets up already compiled version of gtest to "package" sub-directory of your project/solution.

这篇关于无法通过Google测试工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:54