Test Driven Software Development Erik Zeitler Dream up

  • Slides: 21
Download presentation
(Test Driven) Software Development Erik Zeitler

(Test Driven) Software Development Erik Zeitler

Dream up a business case § Think of a service you want to see

Dream up a business case § Think of a service you want to see § Figure out how to do it in a way that doesn’t suck § Do it 2021 -06 -12 Erik Zeitler 2

How to do it? § Think about the use cases • How will the

How to do it? § Think about the use cases • How will the service be used § Cut the work into pieces • User Interface, application code, database § Assign pieces within the project group § Use Test Driven Development • Test each piece (unit tests) • Test all pieces together (integration/regression tests) 2021 -06 -12 Erik Zeitler 3

Rapid prototyping § Make a simple prototype ASAP • See what new ideas it

Rapid prototyping § Make a simple prototype ASAP • See what new ideas it gives you • ”Often, users don’t know what they want until they see it” – Steve Jobs § Show your prototype to the other project groups • Give feedback to each other 2021 -06 -12 Erik Zeitler 4

Three-tier architecture § User Interface • What should the user see? • Make drawings

Three-tier architecture § User Interface • What should the user see? • Make drawings § Database • What information will you keep? • Do data modelling, using ER diagrams § Business logic • How will you present the data to the users? • For any non-trivial function, do test driven development 2021 -06 -12 Erik Zeitler 5

Data modelling § Keep persistent data in a database • Customer information • Banking

Data modelling § Keep persistent data in a database • Customer information • Banking information • Inventory § What do you carry, and how much do you have right now? • Past activity § What has the customer bought? § What has the customer looked at? § What information do you want to store? Why? § Make an Entity Relationship diagram • Translate the ER diagram into SQL tables 2021 -06 -12 Erik Zeitler 6

Example of an ER diagram 2021 -06 -12 Erik Zeitler 7

Example of an ER diagram 2021 -06 -12 Erik Zeitler 7

Why ER diagrams? § ”Keep talking about the algorithms, and everyone will stay totally

Why ER diagrams? § ”Keep talking about the algorithms, and everyone will stay totally mystified” § ”Show the ER diagram, and everything else will be obvious” 2021 -06 -12 Erik Zeitler 8

Software development is all about Getting Things Done Priorities • Make 2021 -06 -12

Software development is all about Getting Things Done Priorities • Make 2021 -06 -12 in software development: it work it beautiful it fast Erik Zeitler 9

How? 1. How to make it work? • First write tests, then write code.

How? 1. How to make it work? • First write tests, then write code. This is called Test Driven Development. 2. How to make it beautiful? • Re-factor 3. How to make it fast? • 2021 -06 -12 Don’t worry about performance until performance is a problem. Erik Zeitler 10

Why TDD? § Because • Debugging sucks • Testing rocks § Because does it

Why TDD? § Because • Debugging sucks • Testing rocks § Because does it § A test is a specification • The capabilities of a program is defined by its tests • If the tests pass we know that the program works – for the test cases. § A test is a piece of documentation 2021 -06 -12 Erik Zeitler 11

The rules of TDD are simple 1. You can't write production code unless there

The rules of TDD are simple 1. You can't write production code unless there is a broken test. • First write a test, then write the code 2. When there is a broken test, you change your code to make it pass. 3. When your code is working, you refactor to eliminate any code smells… 2021 -06 -12 Erik Zeitler 12

What is code smell? § ”Something is fishy about the code” § http: //en.

What is code smell? § ”Something is fishy about the code” § http: //en. wikipedia. org/wiki/Code_smell § Examples: • Large method § A function that is > 1 page • Duplicated method § A method, function, or procedure that is very similar to another. • Contrived Complexity § Forced usage of overly complicated design patterns where simpler design would suffice 2021 -06 -12 Erik Zeitler 13

Demonstration yay 2021 -06 -12 Erik Zeitler 14

Demonstration yay 2021 -06 -12 Erik Zeitler 14

A good set of tests 1. Covers all code 1. How many lines of

A good set of tests 1. Covers all code 1. How many lines of code do you have? 2. How many lines are tested? 3. Do you have > 90% test coverage? 2. performs tests on different scale 1. unit – integration – regression 3. tests for all cases, including edge cases and errors 1. e. g. expect. Exception() 2021 -06 -12 Erik Zeitler 15

Different sized tests isolation, speed 2021 -06 -12 Large (regression) Medium (integration) Small (unit)

Different sized tests isolation, speed 2021 -06 -12 Large (regression) Medium (integration) Small (unit) Erik Zeitler confidence in whole system 16

Where is the bug? vs. ”Test failed” 2021 -06 -12 Erik Zeitler 17

Where is the bug? vs. ”Test failed” 2021 -06 -12 Erik Zeitler 17

Does the entire system work? + vs. + + + 2021 -06 -12 Erik

Does the entire system work? + vs. + + + 2021 -06 -12 Erik Zeitler 18

Tests for edge cases and errors § Write tests for invalid input data, like

Tests for edge cases and errors § Write tests for invalid input data, like • • empty strings empty data sets false broken input data • what happens to your functions if they get ’a%76 tr 423£ 4’, when they expected a single character (e. g. ’a’)? Generate exceptions in production code catch exceptions in test code 2021 -06 -12 Erik Zeitler 19

Test your database § Set up a test database, identical to your production database

Test your database § Set up a test database, identical to your production database § Grant privileges to your php test script § Call your stored procedures from your php test script § Validate the result data from the database 2021 -06 -12 Erik Zeitler 20

Further resources § Soon available on the course home page: • The latest PHPUnit

Further resources § Soon available on the course home page: • The latest PHPUnit (simpletest) • These slides • My demonstration examples § IDEs (Eclipse, Netbeans, . . . ) have infrastructure for testing 2021 -06 -12 Erik Zeitler 21