summaryrefslogtreecommitdiff
path: root/chromium/docs/patterns/fortesting-methods.md
blob: cad25a805be1f288823c4610225dca9906345e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# The ForTesting Methods Pattern

The ForTesting pattern involves creating public helper methods on a class to
provide access to test-only functionality, and giving them a specific type of
name (`XForTesting` or `XForTest`) to provide a signal at the call site that they
are not intended for regular use.

## Use this pattern when:

You have a widely-used object that you need to expose a small amount of test
functionality on.

## Don't use this pattern when:

* You have lots of different ForTesting methods: consider the [TestApi] pattern
  instead.
* Only a small set of test cases need access: consider the [friend the tests]
  pattern instead, to avoid polluting the public API.

## Alternatives / See also:

* [Friend the tests]
* [TestApi]
* [ForTesting in the style guide](../../styleguide/c++/c++.md)

## How to use this pattern:

```cpp
class Foo {
 public:
  // ... regular public API ...

  void DoStuffForTesting();
};
```

The ForTesting suffix indicates to code reviewers that the method should not be
called in production code. There is a very similar antipattern in which the
suffix is missing:

```cpp
class Foo {
 public:
  // ... regular public API ...

  // Do not call! Only for testing!
  void DoStuff();
};
```

[testapi]: testapi.md
[friend the tests]: friend-the-tests.md