我想从单元测试中提取代码,以使我的测试方法更加清晰:
Check check;
check.Amount = 44.00;
// unit testing on the check goes here
我应该如何提取这个?我应该使用指向检查或其他结构的指针来确保在我使用对象时仍然分配它吗?
我不想使用构造函数,因为我想用生产创建逻辑隔离我的测试创建逻辑。
答案 0 :(得分:0)
在现代单元测试框架中,您通常将测试用例视为
class MyTest: public ::testing::Test {
protected:
MyTest() {}
~MyTest() {}
virtual void SetUp() {
// this will be invoked just before each unit test of the testcase
// place here any preparations or data assembly
check.Amount = 44.00;
}
virtual void TearDown() {
// this will be inkoved just after each unit test of the testcase
// place here releasing of data
}
// any data used in tests
Check check;
};
// single test that use your predefined preparations and releasing
TEST_F(MyTest, IsDefaultInitializedProperly) {
ASSERT_FLOAT_EQ(44., check.Amount);
}
// and so on, SetUp and TearDown will be done from scratch for every new test
您可以在Google Test Framework中找到此类功能(https://github.com/google/googletest/)