gpt4 book ai didi

c++ - 用于 Google 测试的 CMake

转载 作者:行者123 更新时间:2023-12-01 14:48:31 25 4
gpt4 key购买 nike

我正在尝试按照本教程运行 Google 测试文件,但我在使用 CMakeLists.txt 时遇到了一些问题。 .

https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

这里的 runTests 到底是什么?它是我程序的 main() 文件吗?我应该如何根据我的程序替换它?目前,我在使用 make 时遇到此错误:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/runTests.dir/build.make:95: recipe for target 'runTests' failed
make[2]: *** [runTests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all

main.cpp:(我也尝试将这部分移动到tests.cpp文件的末尾,但仍然没有用)
#include <iostream>
#include "Player.h"
#include "gtest/gtest.h"

using namespace std;

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

播放器.h
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
using namespace std;

class Player
{

int inventory;

public:
Player();
int decreaseInventory(int numOfBeers);
void setInventory(int newInventory);
int getBackOrder();
int getCost();
int getInventory();

bool operator ==(Player& p);
};

Player::Player()
{
cout << " Default Player Constructor\n";
inventory = 12;
backorder = 0;
cost = 0;
orderDelay = 0;
shipmentDeplay = 0;
}

void Player::setInventory(int newInventory)
{
inventory = newInventory;
}

int Player::decreaseInventory(int numOfBeers)
{
inventory = inventory - numOfBeers;
}

int Player::getInventory()
{
return inventory;
}


#endif

测试.cpp
#include "gtest/gtest.h"
#include "Player.h"

TEST(playerTest, decreaseInventoryTest ) {

Player p;
int curr_inv = p.getInventory();
EXPECT_EQ(curr_inv-3, p.decreaseInventory(3));

}

我怎样才能运行我的测试?

最佳答案

what exactly is runTests here? Is it my program's main() file? How should I replace it according to my program?


runTests是您尝试构建的可执行文件的名称。它需要一个 main就像任何可执行文件一样,有两种选择:1) 自己编写或 2) 使用 gtest 提供的那个。如果您想使用选项 1,请添加 main.cppadd_executable线。
add_executable(runTests tests.cpp main.cpp)

我认为更好的选择是使用 main由 GTest 提供,因为它为您提供了一些命令行参数。您可以通过将它添加到您的 target_link_libraries 来使用它。线。
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)

关于c++ - 用于 Google 测试的 CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60637558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com