我想使用 Visual Studio 的测试资源管理器来运行我的 Google 测试。当我创建控制台项目并添加默认 Google 测试项目并构建解决方案时,它会按预期找到测试。
现在我想创建自己的类,其中所有内容都在头文件中设置。
class foo
{
public:
foo() : the_count(0) {}
~foo() = default;
void count_plus() { the_count++; };
int get_count() { return the_count; };
private:
int the_count;
};
然后我修改我的test.cpp文件(由 Visual Studio 的 Google 测试项目创建的默认文件)以使用我的新类。
#include "pch.h"
#include <iostream>
#include "..\ConsoleApplication2\foo.h"
class tester : public testing::Test {
public:
foo bar;
void SetUpTestSuite() {
std::cout << "Setup..\n";
}
void TearDownTestSuite() {
std::cout << "Teardown..\n";
}
};
TEST_F(tester, TestFixture1)
{
EXPECT_EQ(bar.get_count(), 0);
bar.count_plus();
EXPECT_EQ(bar.get_count(), 1);
}
构建此解决方案还会自动检测测试并成功运行它们。
现在它变得有趣了......当我将我的实现移动foo到一个.cpp文件时。
foo.h
class foo
{
public:
foo();
~foo() = default;
void count_plus();
int get_count();
private:
int the_count;
};
foo.cpp
#include "foo.h"
foo::foo()
{
the_count = 0;
}
void
foo::count_plus()
{
the_count++;
}
int
foo::get_count()
{
return the_count;
}
然后我构建了解决方案,我最初收到一个链接器错误,抱怨未解决的外部问题。
但是,如果我将测试项目的链接器设置更改为指向另一个项目,如下所示:
Properties -> Linker -> Input -> Additional Dependencies -> add $(SolutionDir)ConsoleApplication2\$(IntDir)*.obj
我从这个答案中得到的,我可以成功地构建项目。
但是,在我完成项目构建后,我不再能够查看或运行我的测试。
我在这里做错了吗?还是 Visual Studio 刚刚坏了?



