官术网_书友最值得收藏!

Unit testing

Unit testing is a method by which individual pieces of source code are tested to verify that the design and implementation for that unit have been correctly implemented. There are four phases executed in sequence in a unit test case are the following:

  • Setup: The test case initializes the test fixture, that is the before picture required for the SUT to exhibit the expected behavior.
  • Exercise: The test case interacts with the SUT, getting some outcome from it as a result. The SUT usually queries another component, named the Depended-On Component (DOC).
  • Verify: The test case determines whether the expected outcome has been obtained using assertions (also known as predicates).
  • Teardown: The test case tears down the test fixture to put the SUT back into the initial state.

These phases and its relationship with the SUT and DOC is illustrated as follows:

Unit test generic structure

Unit testing is done with the unit under test in isolation, that is, without interacting its DOCs. To that aim, test doubles are employed to replace any components on which the SUT depends. There are several kinds of test doubles:

  • A dummy object simply satisfies the real object API but it is never actually used. The typical use case for dummy objects is when they are passed as parameters to meet the method signature, but then the dummy object is not actually used.
  • A fake object replaces the real object with a simpler implementation, for example, an in-memory database.
  • A stub object replaces the real object providing hard-coded values as responses.
  • A mock object also replaces the real object, but this time with programmed expectations as responses.
  • A spy object is a partial mock object, meaning that some of its methods are programmed with expectations, but the others use the real object's implementation.
主站蜘蛛池模板: 武隆县| 武夷山市| 称多县| 高碑店市| 西贡区| 襄樊市| 昌宁县| 井陉县| 屏边| 渝中区| 连山| 广州市| 平潭县| 本溪| 南靖县| 乌鲁木齐市| 山阴县| 文山县| 嘉定区| 柘荣县| 江北区| 靖西县| 昌吉市| 阜新市| 湖州市| 莲花县| 大丰市| 德昌县| 鄂托克旗| 马鞍山市| 长阳| 沂南县| 都江堰市| 泗水县| 梁平县| 乌兰浩特市| 年辖:市辖区| 岑溪市| 娱乐| 星子县| 襄樊市|