【问题标题】:How to extract testing creation logic into a shared method如何将测试创建逻辑提取到共享方法中
【发布时间】:2011-04-13 21:07:23
【问题描述】:

我想从单元测试中提取代码以使我的测试方法更清晰:

Check check;
check.Amount = 44.00;

// unit testing on the check goes here

我应该如何提取这个?我应该使用指向检查或其他结构的指针来确保在使用对象时它仍然被分配吗?

我不想使用构造函数,因为我想将我的测试创建逻辑与生产创建逻辑隔离开来。

【问题讨论】:

  • 哪个单元测试框架?你能发布更多代码吗?

标签: c++ unit-testing


【解决方案1】:

在现代单元测试框架中,您通常将测试用例作为

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/) 中找到此类功能

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2021-03-25
    • 2015-06-03
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多