State and Behavioral Testing

Classifying the testing task at hand aids in identifying the best approach in writing the tests. Determining whether a test will verify state or behavior usefully steers the testing process.

Pure state verification exercises the code and examines the resulting changes in data values associated with the operation. Mathematical functions that simply transform inputs to outputs exemplify the target of state verification. In a functional model, verification simply determines that the return values correlate correctly with the input parameters. In an object-oriented model, you additionally verify that the attributes of the object have changed (or not changed!) appropriately. State verification tests typically supply inputs and verify that the outputs correspond to the inputs in the expected ways.

Behavior verification checks that the code under test performs the right operations: It exhibits the right behavior in ways that go beyond data transformation. The purest example of behavioral verification simply orchestrates activities, as in Listing 3-1. It is the method that only calls other methods that are independently testable in order to coordinate their execution order and to channel the outputs of one to the inputs of another. Behavioral testing relies heavily on test doubles, frequently mocks.

Listing 3-1: A JavaScript example of a purely behavioral function using the jQuery.Deferred() implementation of the Promise pattern to coordinate asynchronous computation. It orchestrates the steps of the process, ensuring that dependencies are satisfied and parallel tasks are coordinated, but does no computation itself.

function submitTestScore() {
  verifyAllQuestionsAnswered();
  $.when(
    computeScore(), // Returns deferred object
    allocateAttemptID() // Server call returns deferred object
  ).done(
    sendScore()
  );
}

Highly decomposed software tends to be either behavioral or stateful. You will encounter cases that justifiably do a little of both, but the majority of the software you test will fit cleanly into one category or the other, guiding you toward the most effective style for testing it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.36.146