Chapter 3. Automating the Remaining Parking Lots

As Tony finished the first tests for the Valet Parking lot, he was rather confident that he could automate the remaining four parking lot examples one by one in a few hours. Therefore he continued to do so directly, starting with the examples for the Short-Term Parking lot, then the Economy lot, the Long-Term Garage parking lot, and finally automating the Long-Term Surface Parking lot examples.

Short-Term Parking Lot

The Short-Term Parking lot examples from the end of the workshop are displayed in Table 3.1.

Table 3.1. Short-Term Parking Lot Examples

Image

Tony starts automating these by using the Valet Parking examples as a basis. He creates Short-Term.feature with the contents listed in Listing 3.1.

Listing 3.1. The Short-Term Parking lot automated examples

 1 Feature: Short-Term Parking feature
 2   The parking lot calculator can calculate costs for
      Short-Term Parking.
 3
 4   Scenario Outline: Calculate Short-Term Parking Cost
 5     When I park my car in the Short-Term Parking Lot for
       <parking duration>
 6     Then I will have to pay <parking costs>
 7
 8   Examples:
 9   | parking duration   | parking costs |
10   | 30 minutes         |  $ 2.00       |
11   | 1 hour             |  $ 2.00       |
12   | 1 hour 30 minutes  |  $ 3.00       |
13   | 2 hours            |  $ 4.00       |
14   | 3 hours 30 minutes |  $ 7.00       |
15   | 12 hours 30 minutes|  $ 24.00      |
16   | 1 day 30 minutes   |  $ 25.00      |
17   | 1 day 1 hour       |  $ 26.00      |

Line 5 does not work immediately. None of the current step definitions match the “Short-Term Parking” part. Tony therefore inspects the step definitions from earlier. He realizes that he can reuse the step definitions nearly completely by just introducing a new pattern match for the parking lot used. First of all he renames step definitions/valet_steps.rb to step_definitions/parking_lot_steps.rb and introduces the parking lot parameter in the when keyword definition. The result is listed in Listing 3.2.

Listing 3.2. The step definitions after Tony generalized them

1 When /^I park my car in the (.*) Lot for (.*)$/ do |
      parking_lot, duration|
2   $parkcalc.select(parking_lot)
3   $parkcalc.enter_parking_duration(duration)
4 end
5
6 Then /^I will have to pay (.*)$/ do |price|
7   $parkcalc.parking_costs.should == price
8 end

The final step for Tony for the Short-Term Parking tests is to extend the durationMap in the ParkCalcPage class. For the Short-Term Parking examples he needs to introduce examples for 1 hour, 1 hour 30 minutes, 2 hours, 3 hours 30 minutes, 12 hours 30 minutes, 1 day 30 minutes, and 1 day 1 hour. The resulting durationMap from lib/parkcalc.rb can be seen in Listing 3.3.

Listing 3.3. The ParkCalcPage class with the extended durationMap for all Valet and Short-Term Parking examples

 1 class ParkCalcPage
 2
 3 ...
 4   @@durationMap = {
 5     '30 minutes' => ['05/04/2010', '12:00', 'AM', '05/04/2010'
           , '12:30', 'AM'],
 6     '1 hour' => ['05/04/2010', '12:00', 'AM', '05/04/2010',
           '01:00', 'AM'],
 7     '1 hour 30 minutes' => ['05/04/2010', '12:00', 'AM',
           '05/04/2010', '01:30', 'AM'],
 8     '2 hours' => ['05/04/2010', '12:00', 'AM', '05/04/2010',
           '02:00', 'AM'],
 9     '3 hours' => ['05/04/2010', '12:00', 'AM', '05/04/2010',
           '03:00', 'AM'],
10     '3 hours 30 minutes' => ['05/04/2010', '12:00', 'AM',
           '05/04/2010', '03:30', 'AM'],
11     '5 hours' => ['05/04/2010', '12:00', 'AM', '05/04/2010',
           '05:00', 'AM'],
12     '5 hours 1 minute' => ['05/04/2010', '12:00', 'AM',
           '05/04/2010', '05:01', 'AM'],
13     '12 hours' => ['05/04/2010', '12:00', 'AM', '05/04/2010',
           '12:00', 'PM'],
14     '12 hours 30 minutes' => ['05/04/2010', '12:00', 'AM',
           '05/04/2010', '12:30', 'PM'],
15     '24 hours' => ['05/04/2010', '12:00', 'AM', '05/05/2010',
           '12:00', 'AM'],
16     '1 day 1 minute' => ['05/04/2010', '12:00', 'AM',
           '05/05/2010', '12:01', 'AM'],
17     '1 day 30 minutes' => ['05/04/2010', '12:00', 'AM',
           '05/05/2010', '12:30', 'AM'],
18     '1 day 1 hour' => ['05/04/2010', '12:00', 'AM',
           '05/05/2010', '01:00', 'AM'],
19     '3 days' => ['05/04/2010', '12:00', 'AM', '05/07/2010',
           '12:00', 'AM'],
20     '1 week' => ['05/04/2010', '12:00', 'AM', '05/11/2010',
           '12:00', 'AM']
21   }
22 ...

Tony executes all the Short-Term Parking examples, and as he sees that they all pass, then he executes the whole suite with the Valet Parking as well as the Short-Term Parking examples again. He executes both because he is not sure whether the two test suites might interact with each other. As Tony sees both of them pass, he feels safe checking in his code to the version control system. Tony congratulates himself because he saved the company a lot of money by reusing the already existing automation code rather than re-inventing the wheel.

Economy Parking Lot

Tony takes a look at the Economy Parking examples next. The results from the workshop are listed in Table 3.2.

Table 3.2. Economy Parking Lot Examples

Image

Tony creates the file Economy.feature where he describes the Economy Parking lot feature. The contents are listed in Listing 3.4.

Listing 3.4. The Economy Parking Lot automated examples

 1 Feature: Economy Parking feature
 2   The parking lot calculator can calculate costs for Economy
       parking.
 3
 4   Scenario Outline: Calculate Economy Parking Cost
 5     When I park my car in the Economy Parking Lot for <parking
        duration>
 6     Then I will have to pay <parking costs>
 7
 8   Examples:
 9   | parking duration    | parking costs |
10   | 30 minutes          | $ 2.00        |
11   | 1 hour              | $ 2.00        |
12   | 4 hours             | $ 8.00        |
13   | 5 hours             | $ 9.00        |
14   | 6 hours             | $ 9.00        |
15   | 24 hours            | $ 9.00        |
16   | 1 day, 1 hour       | $ 11.00       |
17   | 1 day, 3 hours      | $ 15.00       |
18   | 1 day, 5 hours      | $ 18.00       |
19   | 6 days              | $ 54.00       |
20   | 6 days, 1 hour      | $ 54.00       |
21   | 7 days              | $ 54.00       |
22   | 1 week, 2 days      | $ 72.00       |
23   | 3 weeks             | $ 162.00      |

As a last step, Tony extends the durationMap in the ParkCalcPage class to include the new values. I will leave this extension to the ambitious reader as an exercise. Before checking the Economy examples in the version control system, Tony executes all tests again.

Tony repeats the cycle for the two variations of the Long-Term Parking lot. I will leave this as an exercise for the enthusiastic reader. You can find the full examples on the github repository for this part of the book.1

Summary

Tony was able to automate the remaining examples quickly. Since Alex and Tony worked in a modular way, automating the remaining four parking lots was easy to do—even for Tony, who is a rather inexperienced programmer. The resulting automated tests describe the specification of the parking cost calculator as agreed upon with the customer. The examples are automated, and can be used for future adaptions of the underlying logic.

Tony also noticed that he could reuse most of the automation code that was available from the Valet Parking examples. Rather than coming up with a different approach here, he simply extended the lookup map for the different parking durations, varied the picking of the parking lot, and then noted the examples.

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

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