(Test Driven) Software Development 11/16/2018 (Test Driven) Software Development Erik Zeitler
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 2018-11-16 Erik Zeitler
How to do it? Think about the use cases Cut the work into pieces 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) 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler
Example of an ER diagram 2018-11-16 Erik Zeitler
Why ER diagrams? ”Keep talking about the algorithms, and everyone will stay totally mystified” ”Show the ER diagram, and everything else will be obvious” 2018-11-16 Erik Zeitler
Software development is all about Getting Things Done Priorities in software development: Make it work Make it beautiful Make it fast 2018-11-16 Erik Zeitler
How? How to make it work? How to make it beautiful? First write tests, then write code. This is called Test Driven Development. How to make it beautiful? Re-factor How to make it fast? Don’t worry about performance until performance is a problem. 2018-11-16 Erik Zeitler
Why TDD? Because Because does it A test is a specification 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 2018-11-16 Erik Zeitler
The rules of TDD are simple You can't write production code unless there is a broken test. First write a test, then write the code When there is a broken test, you change your code to make it pass. When your code is working, you refactor to eliminate any code smells… 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler
Demonstration yay 2018-11-16 Erik Zeitler
A good set of tests Covers all code performs tests on different scale How many lines of code do you have? How many lines are tested? Do you have > 90% test coverage? performs tests on different scale unit – integration – regression tests for all cases, including edge cases and errors e.g. expectException() 2018-11-16 Erik Zeitler
Different sized tests Large (regression) Medium (integration) Small (unit) isolation, speed confidence in whole system 2018-11-16 Erik Zeitler
Where is the bug? vs. ”Test failed” ”Test failed” 2018-11-16 Erik Zeitler
Does the entire system work? + vs. + + + 2018-11-16 Erik Zeitler
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%76tr423£4’, when they expected a single character (e.g. ’a’)? Generate exceptions in production code catch exceptions in test code 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler
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 2018-11-16 Erik Zeitler