简体   繁体   English

每个类在 Gtest 中只调用一次代码

[英]Call code only once in Gtest per class

I am using gtest as my testing framework.我使用gtest作为我的测试框架。 I am looking for a specific function that will be called only once.我正在寻找一个只会被调用一次的特定函数。

So far, I have seen SetUp and the constructor of the class.到目前为止,我已经看到了SetUp和类的构造函数。 But it seems like they are both called for each test.但似乎每次测试都会调用它们。 According to this example , the constructor is where,根据这个例子,构造函数在哪里,

You can do set-up work for each test here您可以在此处为每个测试进行设置工作

leading me to believe that it's called once per test case.让我相信每个测试用例都会调用一次。 For the SetUp function,对于SetUp功能,

Code here will be called immediately after the constructor (right before each test).此处的代码将在构造函数之后立即调用(就在每次测试之前)。

Does that mean both are called for each test?这是否意味着每次测试都会调用两者? If so, where can I place code that will be called only once for the lifetime of the test class?如果是这样,我在哪里可以放置在测试类的生命周期内只调用一次的代码?

Use Test suite setup : SetUpTestSuite() .使用测试套件设置SetUpTestSuite()

And yes, both the constructor and SetUp() method are called for each test method.是的,每个测试方法都会调用构造函数和 SetUp() 方法。 In GTest, the test functions are implemented as subclasses of the Test Case class.在 GTest 中,测试函数是作为 Test Case 类的子类实现的。 That's why Test Case constructor is naturally called before execution of each of its children.这就是为什么在执行其每个子项之前自然会调用测试用例构造函数的原因。 See this FAQ question about what to use: constructor or SetUp().请参阅有关使用什么的常见问题解答:构造函数或 SetUp()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM