27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Systems with Finite <strong>Model</strong>s 145<br />

Here is an example from Figure 8.4, the case where DoAction handles the server’s<br />

Bind action. Here the socket s and the host and port have been declared and assigned<br />

in the stepper.<br />

case("ServerBind"):<br />

s.Bind(host,port);<br />

return null;<br />

Here is a more complex case, where DoAction handles the server’s Send action.<br />

This code extracts the argument from the action term using the indexer expression<br />

a[0], casts it to double, and passes it to the implementation. The modeling library<br />

provides action arguments through a C# indexer, which accesses elements by subscripts.<br />

(Appendix A.3 explains this and other facilities for working with action<br />

terms.)<br />

case("ServerSend"):<br />

// s.Send sends a double, not a string<br />

s.Send((double) action[0]);<br />

return null;<br />

The most complex cases occur where there is a split action (that has outputs).<br />

The test runner passes DoAction a term that represents the start action. DoAction<br />

invokes the corresponding method in the implementation. When that method returns,<br />

DoAction collects the output, constructs a term that represents the finish action using<br />

Action.Create, and returns the finish action to the test runner. Here is an example,<br />

the case where DoAction handles the client’s Receive action:<br />

case("ClientReceive_Start"):<br />

// c.Receive returns a double, not a string<br />

return Action.Create("ClientReceive_Finish", c.Receive());<br />

The DoAction code could also read implementation state variables, and include<br />

their values in the term for the finish action. To make use of this information, the<br />

corresponding action method of the model would have to return the values of model<br />

state variables in out parameters.<br />

The test runner indicates a test failure if the finish action returned by DoAction<br />

does not match the finish action generated by the model program (e.g., if the<br />

implementation returns a different value than the model program computed). That<br />

is, how the model program acts as a test oracle.<br />

The test runner also indicates a test failure if DoAction throws an exception. For<br />

this reason, DoAction usually does not handle exceptions thrown by the implementation.<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!