如何解决如何将测试创建逻辑提取到共享方法中
| 我有要从单元测试中提取的代码,以使测试方法更清晰:Check check;
check.Amount = 44.00;
// unit testing on the check goes here
我应该如何提取呢?我应该使用指向支票或其他结构的指针来确保在使用对象时仍分配该支票吗?
我不想使用构造函数,因为我想将测试创建逻辑与生产创建逻辑隔离。
解决方法
在现代的单元测试框架中,通常将测试用例作为
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测试框架(https://github.com/google/googletest/)中找到此类功能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。