Download presentation
Presentation is loading. Please wait.
Published byAngel Anthony Modified over 9 years ago
1
Or ways to enhance coding enjoyment, productivity and, most of all, preserve your sanity. Nicolas Connault Web developer Moodle HQ February 19 th 2008 Unit testing
2
2 Lecture Outline Introduction to Unit Testing Development principles Unit Test Frameworks Unit tests for legacy code Resources
3
3 Introduction to Unit Testing Overview: Why write unit tests? Why not? What are bugs? What unit tests won't do for you
4
4 Introduction to Unit Testing Why write Unit Tests? Clean, bug-free, documented code Quality software: less complaints Coding simplicity: less stress Why not? Not enough time! Overview
5
5 Introduction to Unit Testing The less tests, the more bugs The more bugs, the more complaints The more complaints, the more stress The more stress, the less tests we write Overview
6
6 Introduction to Unit Testing Can we write bug-free code? Overview ? ? ? ? ? ? ?
7
7 Introduction to Unit TestingOverview What is a bug? An unwanted feature (memory leak, feature creep etc.) A "broken" feature A misunderstood feature (by the client or the coder) "It's not a bug, it’s a feature!"
8
8 Introduction to Unit TestingOverview What Unit Tests don’t do: Performance issues Integration errors User interface issues
9
9 Introduction to Unit TestingStatistics According to some very reliable sources, programmers tend to spend over 80% of their working time looking for causes of bugs in existing code, 15% fixing these bugs, and 5% designing and developing new code. (source: N.Connault's astute observations, 2008).
10
10 Introduction to Unit TestingCollateral Damage Code Library 1Code Library 2 Identify Bug Fix bug Patch library 1 Test library 1 Result: Waste of time NEW HIDDEN BUG !!! Dependencies
11
11 Introduction to Unit TestingSolution Code Library 1Code Library 2 Identify Bug Fix bug Patch libraries Test all libraries Result: Better code When all tests pass... Dependencies
12
12 Coding principles that apply to unit testing...
13
13 Coding principles If you don't know why your modification fixed a bug, Ignorance is the enemy you're almost guaranteed to be the creator of at least one new bug
14
14 Coding principles Development and testing are not separate tasks Holistic development
15
15 Coding principles The goal of programming is to reduce the amount of repetitive work and the number of simple decisions humans need to make. Testing code is repetitive, and it is usually simple. Tests should be automated. Automation
16
16 Coding principles Each unit test must test a single unit of code. A unit of code is usually a simple function or a simple method. Isolation Example of an overloaded function: solveWorldHunger(money)
17
17 Coding principles One unit test must never affect another. The order in which they are run must be absolutely inconsequential Independence
18
18 Coding principles When you need new functionality, first write a failing unit test, then implement the new code. Test-driven development
19
19 Coding principles Never introduce bugs in your unit tests. Write them with the same rigour as with your regular code. Reliability
20
20 Coding principles Write mock objects for: Objects whose behaviour is non-deterministic (news feed) Objects that are complex/difficult to set up Objects that are slow Objects that have or are a User Interface Objects whose behaviour is hard to trigger Objects that haven't yet been implemented Mock objects
21
21 Coding principles NEVER, NEVER, NEVER commit code if any unit test fails !!! Golden Rule
22
22 Coding principles Test boundary conditions as much as is possible and reasonable, including heavy load conditions. Thoroughness
23
23 Coding principles Don't write tests for code that will obviously never fail Prioritise
24
24 Questions and answers ?+!
25
25 Unit test Frameworks Automate the generation of unit testing code Simplify the entire process Provide advanced testing features (mock objects, fancy reports, expectations etc…) Advantages
26
26 Unit Tests in legacy code Too big a job to “catch up” all at once Create unit tests to verify bug reports Use unit tests to fix bugs Refactor code when needed Commit bug fix when all unit tests pass
27
27 Resources Andrew Hunt and David Thomas. Pragmatic Unit Testing in Java with JUnit. The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, 2004. Venkat Subramaniam and Andy Hunt. Practices of an Agile Developer working in the Real World, The Pragmatic Bookshelf, 2006. http://blog.daveastels.com/2005/07/05/a-new-look-at-test-driven-development http://www.ibm.com/developerworks/java/library/j-cq09187/index.html http://video.google.com/videoplay?docid=8135690990081075324
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.