简体   繁体   English

如何让 google-test 课程与我的课程成为朋友?

[英]How to make google-test classes friends with my classes?

I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members.我听说有可能让 google-test TestCase 类朋友加入我的类,从而使测试能够访问我的私有/受保护成员。

How to accomplish that?如何做到这一点?

Try this (straight from Google Test docs...):试试这个(直接来自谷歌测试文档......):

FRIEND_TEST(TestCaseName, TestName);

For example:例如:

// foo.h
#include <gtest/gtest_prod.h>

// Defines FRIEND_TEST.
class Foo {
  ...
 private:
  FRIEND_TEST(FooTest, BarReturnsZeroOnNull);
  int Bar(void* x);
};

// foo_test.cc
...
TEST(FooTest, BarReturnsZeroOnNull) {
  Foo foo;
  EXPECT_EQ(0, foo.Bar(NULL));
  // Uses Foo's private member Bar().
}

I know this is old but I was searching for the same answer today.我知道这是旧的,但我今天正在寻找相同的答案。 " gtest_prod.h " just introduces a simple macro to reference test classes. gtest_prod.h ”只是引入了一个简单的宏来引用测试类。

#define FRIEND_TEST(test_case_name, test_name)\
friend class test_case_name##_##test_name##_Test

So FRIEND_TEST(FooTest, BarReturnsZeroOnNull);所以FRIEND_TEST(FooTest, BarReturnsZeroOnNull); is equivalent to:相当于:

friend class FooTest_BarReturnsZeroOnNull_Test;

This works because each test is its own class as mentioned in the previous answer.这是有效的,因为每个测试都是它自己的类,如上一个答案中所述。

A far better strategy is to not allow friend tests among your unit tests.更好的策略是在单元测试中不允许友元测试。

Allowing friend tests accessing private members will lead to a code base that is hard to maintain.允许朋友测试访问私有成员将导致难以维护的代码库。 Tests that break whenever a component's inner implementation details are refactored is not what you want.每当组件的内部实现细节被重构时就会中断的测试不是您想要的。 If extra effort is instead put into getting a design where components can be tested through their public interface, you will get tests that only need updating whenever the public interface of a component is updated.如果付出额外的努力来获得可以通过其公共接口测试组件的设计,您将获得仅在组件的公共接口更新时才需要更新的测试。

Tests relying on gtest/gtest_prod.h should be seen as a sign of poor design.依赖于gtest/gtest_prod.h测试应该被视为糟糕设计的标志。

When your tested class and your test class are in a different namespace (eg your tests are in the global namespace), you may need to forward-declare your test class and to add your namespace prefix in FRIEND_TEST:当你的测试类和你的测试类在不同的命名空间中时(例如你的测试在全局命名空间中),你可能需要向前声明你的测试类并在 FRIEND_TEST 中添加你的命名空间前缀:

// foo.h
#include <gtest/gtest_prod.h>

class FooTest_BarReturnsZeroOnNull_Test;

// Defines FRIEND_TEST.
class my_namespace::Foo {
  ...
 private:
  FRIEND_TEST(::FooTest, BarReturnsZeroOnNull);
  int Bar(void* x);
};

// foo_test.cc
using namespace my_namespace;

...
TEST(FooTest, BarReturnsZeroOnNull) {
  Foo foo;
  EXPECT_EQ(0, foo.Bar(NULL));
  // Uses Foo's private member Bar().
}

I know that friend unit tests (or the friendliness in C++ in general) and white-box testing are a controversial subject, but when you work on complex, scientific algorithms, each step of which you need to test and validate, but that you don't want to expose in public (or even protected) interfaces, friend tests appear to me as a simple and pragmatic solution, especially in a test-driven development approach.我知道友元单元测试(或一般 C++ 中的友好性)和白盒测试是一个有争议的主题,但是当您研究复杂的科学算法时,您需要测试和验证其中的每一步,但您不知道不想在公共(甚至受保护)接口中公开,朋友测试在我看来是一种简单实用的解决方案,尤其是在测试驱动的开发方法中。 It is always possible to refactor the code later (or to completely remove white-box tests) if it's against one's religion to use friendliness or white-box testing.如果使用友好性或白盒测试违背了一个人的宗教信仰,那么以后总是可以重构代码(或完全删除白盒测试)。

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

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