Aug 23, 2021

[TDD][book note] Modern C++ programming with TDD

Section 1

Managing test lists is particularly useful.

Uncle Bob describes TDD with three rules:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

i.e
- Write only enough of a unit test to fail.
- Write only enough production code to make the failing unit test pass.

One assert per test. One behavior per test.

Use expect_that instead of assert.

Follow the Single Responsibility Principle(SRP).


Section 2

Unit test:
1. (Optional) statements that set up a context for execution.
2. One or more statements to invoke the behavior you want to verify.
3. One or more statements to verify the expected outcome.
4. (Optional) cleanup statements.


Section 3

TDD cycle

Red-Green-Refactor
1. write a test(Red)
2. Get the test to pass(Green)
3. Optimize the design(Refactor)

Test behavior, not methods.
Keep it simple.
Sticking to the cycle.

In unit test

Arrange-Act-Assert / Given-When-Then

In TDD, unit test should be FAST. i.e no datbase/IO access as possible;
those tests belongs to integration test.

GTest allows you to run subset of the tests by passing in argument to the test binary.
e.g (RTFM)
./test --gtest_filter=MyTest.*


Test private data member

Don't. Violates Tell-Don't-Ask design. If must; add an accessor with returned copy of that value.


Section 4

Test doubles(In C++, we have several choices. Inheritance, template, CRTP, injection.


Section 5

Quality test

Fast
Isolated
Repeatable
Self-verifying
Timely


Section 6

Threading/Concurrent code

1. Separate threading logic from application logic.
2. Sleep is bad.
3. Throttle down to single-threaded for application-specific tests.
4. Demonstrate concurrency issues before introducing concurrency controls.(Hmm...)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.