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.

Overview 27<br />

// Client requests one temperature sample, server responds<br />

[Test]<br />

[Category("ClientServer")]<br />

public void LogOneSample()<br />

{<br />

const int port = 8000;<br />

const string host = "127.0.0.1"; // localhost<br />

string received, command = "T"; // get temperature sample<br />

double temperature = 212;<br />

double response;<br />

Client client = new Client();<br />

Server server = new Server();<br />

server.Socket();<br />

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

server.Listen();<br />

client.Socket();<br />

client.Connect(host,port); // Connect before Accept is OK<br />

server.Accept(); // Accept after Connect does not block<br />

client.Send(command);<br />

received = server.Receive();<br />

server.Send(temperature);<br />

response = client.Receive();<br />

Assert.AreEqual(command, received);<br />

Console.WriteLine("Server sent {0,5:F1}, client received {0,5:F1}",<br />

temperature, response);<br />

Assert.AreEqual(temperature, response);<br />

client.Close();<br />

server.CloseConnection();<br />

server.Close();<br />

} // LogOneSample<br />

Figure 2.10. Test method for client/server scenario.<br />

method’s behavior. But many methods are not state-independent. This introduces<br />

complications, as we shall now see.<br />

2.8 A more complex scenario<br />

<strong>We</strong> would like to write tests that exercise more of the behavior exhibited by the<br />

applications. Our test method LogOneSample tests the smallest realistic run: the<br />

client requests one temperature sample and the server responds (Figure 2.10).<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!