A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.

Slides:



Advertisements
Similar presentations
Acceptance Testing vs. Unit Testing: A Developers Perspective Owen Rogers Rob Styles
Advertisements

SPL/2010 Test-Driven Development (TDD) 1. SPL/
Test-First Programming. The tests should drive you to write the code, the reason you write code is to get a test to succeed, and you should only write.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
A practical guide John E. Boal TestDrivenDeveloper.com.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
CIT 590 Unit testing.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Local Touch – Global Reach The New Tester Matthew Eakin, Manager Managed Testing Practice Sogeti, USA.
Review: Agile Software Testing in Large-Scale Project Talha Majeed COMP 587 Spring 2011.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
Test Driven Development Derived from Dr. Fawcett’s notes Phil Pratt-Szeliga Fall 2009.
By John Boal  Continuous Integration [CI] ◦ Automating the build process ◦ Build the entire system each time any new.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
Terms: Test (Case) vs. Test Suite
TDD,BDD and Unit Testing in Ruby
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Test Driven Development TDD. Testing ”Testing can never demonstrate the absence of errors in software, only their presence” Edsger W. Dijkstra (but it.
Test Driven Development An approach to writing better code Jimmy Zimmerman Intel Corporation.
Chapter 8 – Software Testing Lecture 1 1Chapter 8 Software testing The bearing of a child takes nine months, no matter how many women are assigned. Many.
Testing Especially Unit Testing. V-model Wikipedia:
Sadegh Aliakbary Sharif University of Technology Spring 2012.
T-unit: Tcl Unit Test Package Automated Unit Test Package For Tcl Procedures Final Presentation Joseph Boyle Loyola Marymount University.
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
Test Driven Development Arrange, Act, Assert… Awesome Jason Offutt Software Engineer Central Christian Church
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Software testing Main issues: There are a great many testing techniques Often, only the final code is tested.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Sofia Bulgaria Summer School IST eXPERT: Best Practice on e-Project Development 30 June - 2 July 2003 eXtreme programming.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Coding.
TEST-1 6. Testing & Refactoring. TEST-2 How we create classes? We think about what a class must do We focus on its implementation We write fields We write.
November Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent groups,
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
1 Legacy Code From Feathers, Ch 2 Steve Chenoweth, RHIT Right – Your basic Legacy, from Subaru, starting at $ 20,295, 24 city, 32 highway.
A tool for test-driven development
Chapter 7 The Practices: dX. 2 Outline Iterative Development Iterative Development Planning Planning Organizing the Iterations into Management Phases.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Test-Driven Development Eduard Miric ă. The problem.
 Wes McClure  
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Software testing techniques Software testing techniques Software Testability Presentation on the seminar Kaunas University of Technology.
1 Presentation Title Test-driven development (TDD) Overview David Wu.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Refactoring and Integration Testing or Strategy, introduced reliably by TDD The power of automated tests.
Unit Testing with FlexUnit
Unit testing with NUnit Anne Lam & Chris James CMPS 4113 – Software Engineering April 15, 2015.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Automated Testing with PHPUnit. How do you know your code works?
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Testing. Aspects of Quality Software Flexibility Extensibility Correctness Robustness Reliability Safety Usability.
Continuous Delivery and Quality Monitoring
Automated Software Testing
Unit Testing.
Developer Testing Tricks
Test Driven Development
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Software Testing.
Unit testing C# classes
History, Characteristics and Frameworks
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
You’ll get better code in less time (If you do it for a while)
Presentation transcript:

A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com

 Here is a diagram that illustrates how we can deliver software iteratively using tests to guide our delivery.

 Document the programmer’s intent.  (right, wrong, or otherwise – this is what the programmer wanted the code to do.)  Ensure the code units function as intended.  A suite of UTs gives us a “safety net” when we change code so that we don’t break it by altering it or adding something new.  UTs are a good indicator of how well we understand the code we are writing.

 Level 1 – we write some unit tests.  We wrote some tests that tell us if our code mostly works.  Level 2 – we write (most of) our unit tests first.  We write a unit test then some code, then some more code.  Level 3 – we do Test-Driven Development  We write a unit test, then we write some code to make it pass. Then we refactor, and do it all over again.  Level 4 – we do Acceptance Test-Driven development  We automate our acceptance criteria into acceptance tests first, and then use failing AT’s as a guide to work using UTDD building code and tests until our AT’s pass.

 A small, FAST test that examines a little piece of code (like a single method)  It is NOT a “scenario,” an end-to-end, or use case verification. These are different kinds of tests and should not be in with true unit tests.  The Unit test and the code it tests should all fit on one screen.

 Reveals the intent of the developer who wrote it  Independent  No side effects, and is isolated from other tests  Has no dependencies on external systems or code  UTs Can be run in any order  Automated (this goes without saying…)  Executes very fast (milliseconds)  Unique – tests something not tested elsewhere  Short and Clear – 5 to 10 lines of code  Coded to same standards as main line code

 Arrange  Setup. Set up all the data and conditions that the test needs to execute. Create anything that is needed for the test to operate independently of its environment.  Mock out any dependent systems  Control all variables not under test  Act  Execution – execute the mainline code  Assert  Validate that the code operated as intended

 SMALL code units.  A unit test ideally should cause execution of no more than 20 lines of main-line code.  (this is only a guideline)  Unit tests are focused on a specific narrow piece of functionality rather than on a broader set.  Method-level, or usually even smaller focus.  We should have many tests for each method.

 We want to ensure that our code works, so we write unit tests to illustrate how the code behaves in specific circumstances.  Test for things that may be null  Test for empty strings…  Test all but the simplest properties. Don’t usually need unit tests for this:  public String Name  {  get  {  return this.name;  }  set  {  this.name = value;  }

 How do we know if we have enough UT’s?  100% code coverage?  Zero bugs?  All Acceptance Tests Passing?  Nope…  We have enough tests when we feel CONFIDENT in our code quality.  If we don’t feel confident that our code works and it’s safe to refactor, keep writing tests…

 “Changing existing code to refine its structure without changing its behavior.”  MAKE ONLY SMALL CHANGES AT ONE TIME.  Then test them. With ALL the tests.  Then review them with someone. Then check them in…  This should happen a LOT.  Consider the whole design – not just the classes and methods you happen to be touching.  Think globally, act locally…  Look at the overall design and make refactoring changes accordingly.  Refactor MERCILESSLY.

 There are always bugs.  If there is a bug, it means someplace we forgot a test! [do we have good acceptance criteria?]  Find a bug, write a test!  DO NOT FIX A BUG WITHOUT A TEST!  Write the test FIRST. This is important.  First the test fails – illustrating the bug. When we fix the bug, the test passes.  We now have a test that ensures the bug will never return. No more regressions!

 Figure out what the code needs to do – exactly [note: this is the HARD part.]  Write a test that illustrates the code does the right thing.  The test fails because there is no code yet.  Write just enough code to make the test pass (this can mean hard-coding the right answer)  Refactor the code keeping all the tests passing, until the overall code and design are good enough to stand up to peer review.  Lather, rinse, repeat.

 NUnit – the standard.  TestDriven.Net – VS- integrated test runner  Selenium RC – the best in UI testing  Selenium IDE:   Rhino Mocks – mock object framework  JSUnit – JavaScript Unit Testing  reSharper - if you use C# and Visual Studio

 Agile Testing  Lisa Crispin  xUnit Test Patterns – Refactoring Test Code  Gerard Mezaros  Refactoring to Patterns  Joshua Kerievsky  Refactoring – Improving the Design of Existing Code  Martin Fowler  TestDrivenDeveloper.com

 Test it well  Test it ONCE  Refactor a lot