Test-driven Development Steps Test-driven Development Methodology

J.E.D.I adding passfail criteria for the behavior of the software. Each of these passfail criteria adds to the knowledge of how the software must behave. As more unit tests are added because of new features or correction, the set of unit tests becomes a representative of a set of required behaviors of the software. 6. It centralizes knowledge. The unit tests serve as a repository that provides information about the design decisions that went into the design of the module. The unit tests provide a list of requirements while the source code provides the implementation of the requirements. Using these two sources of information makes it a lot easier for developers to understand the module and make changes that wont introduce bugs. 7. It improves software design. Requiring the developer to write the unit tests for the module before the code helps them define the behavior of the software better and helps them to think about the software from a users point of view.

6.5.1 Test-driven Development Steps

Test-driven development basically is composed of the following steps: 1. Write a test that defines how the software should behave. As an example, suppose that Ang Bulilit Liga has the following requirements, The software must be able to compute the shooting average of every athlete by game and for the season. There are a number of behaviors the software needs to have in order to implement this requirement such as: • The software should identify an athlete. • There must be a way to input the data needed to calculate the shooting average. • There must be a way to identify the game. • There must be a way to get a athletes shooting average for a game. • There must be a way to get a athletes shooting average for the entire season. After making some assumptions like athletes will be identified by their ids and games by dates, you could write the test case. As an example, a fraction of the unit test code is shown in Text 15. Software Engineering 286 J.E.D.I 2. Make the test run as easily and quickly as possible. You dont need to be concerned about the design of the source code; just get it to work. In the case of the example, the source code we are referring to is the class AthleteStatistics which contains the methods that compute the shooting average of an athlete by game and season. Once the unit test is written, it is executed to verify for execution failure. The code fails because the code to implement the behavior has not yet been written but this is an important step because this will verify that unit test is working correctly. Once the unit test has been verified, the code to implement the behavior is written and the unit test is run against it to make sure that the software works as expected. An initial AthleteStatistics class is shown below. Software Engineering 287 Athlete timmy = PCLAthleteList.getAthlete3355; AthleteStatistics timmyStatistics = new AthleteStatisticstimmy; Add shooting average statistics shooting 1 for 9 attempts last 892004 game. timmyStatistics.addShootingAverage892004,1,9; Add shooting average statistics shooting 4 for 10 attempts last 8162004 game. timmyStatistics.addShootingAverage8162004,4,10; Add shooting average statistics shooting 7 for 10 attempts last 8252004 game. timmyStatistics.addShootingAverage8252004,7,10; Compute for the shooting average for game 816 float gameShootingAverage = timmyStatistics.getGameShootingAverage“8162005; Compute for the shooting average for season float seasonShootingAverage = timmyStatistics.getSeasonShootingAverage; Check game shooting average assertEquals200, gameShootingAverage; Check game shooting average assertEquals214, seasonShootingAverage; Text 15 Sample Unit Test for Computing Shooting Average J.E.D.I 3. Clean up the code. Now that the code is working correctly, take a step back and refactor the codes to remove any duplication or any other problem that was introduced to get the test running. As developers, you know that the first pass at writing a piece of code is usually not the best implementation. Do not be afraid to refactor your code. If you break your code, the unit test will let you know immediately.

6.5.2 Testing Java Classes with JUnit