【发布时间】:2021-06-17 05:46:04
【问题描述】:
我是 Gtest 的初学者,在访问 TEST_F 中的类变量时遇到分段错误。以下是我的代码的 sn-p,请帮助指出这种行为背后的原因。
main.cpp
#include <Foo.h>
#include "gtest/gtest.h"
int main(int argc, char* argv[])
{
InitalRegister();
::testing::InitGoogleTest(&argc, argv);
int rc = RUN_ALL_TESTS();
return rc;
}
Foo.h
void InitalRegister();
class TestFoo // Required for registration
{
//Do nothing
};
Foo.cpp
#include "gtest/gtest.h"
#include <Foo.h>
#include <Bar.h>
void InitalRegister()
{
//Doing few registration
}
Bar* newBar;
void Bar_called_init(void *inValue) // will be called as part of InitialRegsiter and inValue will be holding a valid value
{
newBar = (Bar)*inValue;
}
class UnitTestFoo : public::testing::Test
{
public:
Bar* inValue;
Bar* GetValue();
protected:
virtual void SetUp() override
{
inValue = GetValue();//able to access inValue here
}
};
Bar* UnitTestFoo::GetValue()
{
inValue = newBar;
}
TEST_F(UnitTestFoo, Foo1)
{
if (inValue != NULL) // Getting segfault when accessing inValue here
{
}
}
【问题讨论】:
标签: c++ unit-testing segmentation-fault googletest