XUnit Test Patterns writing good unit tests Peter Wiles.

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
Advertisements

xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
FitNesse in Fifty Minutes Chris Harbert Resonate 1.
What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation
Unit Tests DEFINITION AND OVERVIEW by Paul M. code of the damned. com.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Tools for Agile Development: A Developer’s Perspective Mike Linnen Blog:
Managing Technical Debt Pierre G. Boutquin. Welcome!
© ThoughtWorks, 2008 Improving Productivity and Quality With Agile Patrick Kua.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
Computer Engineering 203 R Smith Agile Development 1/ Agile Methods What are Agile Methods? – Extreme Programming is the best known example – SCRUM.
Using TDD Making code maintainable, reusable and readable by writing tests.
Test-Driven Development “Test first, develop later!” –OCUnit.
TDD OVERVIEW OF TEST DRIVEN DEVELOPMENT by Paul M. code of the damned. com.
By Bob Bunson  Simulation of software development project  Fictitious system from Concept to Code  Oriented around the.
By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.
© 2012 Autodesk Automated Testing with the AutoCAD ®.NET API Scott McFarlane Senior Software Engineer, Woolpert, Inc.
From 3 weeks to 30 minutes – a journey through the ups and downs of test automation.
TDD,BDD and Unit Testing in Ruby
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
@benday #vslive Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
© Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.
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.
Sofia Bulgaria Summer School IST eXPERT: Best Practice on e-Project Development 30 June - 2 July 2003 eXtreme programming.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Test driving to clean CODE Kenrick Chien CTO, Critical Phase.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
A tool for test-driven development
Chapter 8 Testing the Programs. Chapter 8 Learning Objectives Be able to …  Define different types of faults and how to classify them  Define the purpose.
Extreme Programming Based on and
 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 Engineering. Acknowledgement Charles Moen Sharon White Bun Yue.
1 Presentation Title Test-driven development (TDD) Overview David Wu.
Object Oriented Analysis and Design 1 Chapter 9 From Design to Implementation  Implementation Model  Forward, Reverse, and Round-Trip Engineering  Mapping.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Unit Testing.
MSF 4.0 for Agile Software Development Ron Tolido Capgemini.
Intelligence and Information Systems 1 3/17/2004 © 2004 Raytheon Company USC/CSE Executive Workshop on Agile Experiences March 17, 2004 A Raytheon Agile.
Automated Testing in Sakai Testing applications and services in isolation and in context Josh Holtzman, UC Berkeley David Haines, University of Michigan.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
1 Punishment Through Continuous Delivery If it hurts, do it more often…
Unit testing with NUnit Anne Lam & Chris James CMPS 4113 – Software Engineering April 15, 2015.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Model View Presenter Design Pattern Jay Smith PMO Architect and Evangelist Tyson Foods, Inc.
Introduction to Unit Testing and JUnit David Rabinowitz.
TDD Unit tests from a slightly different point of view Katie Dwyer.
1 © Agitar Software, 2007 Automated Unit Testing with AgitarOne Presented by Eamon McCormick Senior Solutions Consultant, Agitar Software Inc. Presented.
Unit Testing - solid fundamentals
Test-driven development
TESTING TEST DRIVEN DEVELOPMENT
Where Agile Business Meets Agile Development
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Application Development Theory
Unit Testing in a Team Sparkhound Presents by Steve Schaneville
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
History, Characteristics and Frameworks
More JUnit CS 4501 / 6501 Software Testing
Unit Testing & Test-Driven Development for Mere Mortals
Refactoring.
Designing For Testability
You’ll get better code in less time (If you do it for a while)
Agile Development.
Presentation transcript:

xUnit Test Patterns writing good unit tests Peter Wiles

introduction what makes a good unit test? writing good unit tests signs of bad unit tests designing testable software further reading

Daily standup – 78% Iteration planning – 74% Release planning – 65% Burndown – 64% Retrospectives – 64% Velocity – 52% Unit testing – 70% Continuous Integration – 54% Automated builds – 53% Coding standards – 51% Refactoring – 48% Test driven development – 38% Management practicesTechnical practices the state of agile practices

“continuous attention to technical excellence and good design enhances agility”

theory: good unit tests are important.

“legacy code is simply code without tests” - Michael Feathers

no tests = ? agility bad tests = worse agility good tests = good agility you can’t be truly agile without automated tests

tests do not replace good, rigorous software engineering discipline, they enhance it.

what makes a good unit test?

runs fast helps us localise problems a good unit test - Michael Feathers, again

when is a unit test not really a unit test?

- Robert C Martin “What makes a clean test? Three things. Readability, readability and readability.”

good unit tests are FRIENDS

fast lightning fast, as in 50 to 100 per second no IO all in process, in memory

robust withstanding changes in unrelated areas of code isolated

independent not dependant on external factors, including time

examples communicating how to use the class under test readability is key

necessary where removing a test would reduce coverage

deterministic the result is the same every time

specific each test testing one thing

deterministic robust fast independent examples necessary specific

writing good unit tests some advice

use an xUnit framework [TestFixture] public class TestCalculator { [Test] public void Sum() { var calculator = new Calculator(); var sum = calculator.Sum(5, 4); Assert.AreEqual(9, sum); }

write your tests first

standardise test structure [Test] public void Append_GivenEmptyString_ShouldNotAddToPrintItems() { // Arrange var document = CreatePrintableDocument(); // Act document.Append(""); // Assert Assert.AreEqual(0, document.PrintItems.Count); }

be strict Less than 5 lines for Arrange One line for Act One logical Assert (less than 5 lines)

no teardown bare minimum setup

use project conventions testcase class per class test project per project helpers and bootstrappers

use a test naming convention Method_ShouldXX() Method_GivenXX_ShouldYY() Method_WhenXX_ShouldYY()

use a minimal fresh transient fixture per test

the smallest possible fixture you can get away with using

use a minimal fresh transient fixture per test brand new objects every time, where you can

use a minimal fresh transient fixture per test objects that are chucked after each test, left to the garbage collector

use a minimal fresh transient fixture per test the test should create its own fixture

signs of bad unit tests

conditionals if (result == 5)...

long test methods [Test] public void TestDeleteFlagsSetContactPerson() { ContactPerson myContact = new ContactPerson(); Assert.IsTrue(myContact.Status.IsNew); // this object is new myContact.DateOfBirth = new DateTime(1980, 01, 20); myContact.FirstName = "Bob"; myContact.Surname = "Smith"; myContact.Save(); //save the object to the DB Assert.IsFalse(myContact.Status.IsNew); // this object is saved and thus no longer // new Assert.IsFalse(myContact.Status.IsDeleted); IPrimaryKey id = myContact.ID; //Save the objectsID so that it can be loaded from the Database Assert.AreEqual(id, myContact.ID); myContact.MarkForDelete(); Assert.IsTrue(myContact.Status.IsDeleted); myContact.Save(); Assert.IsTrue(myContact.Status.IsDeleted); Assert.IsTrue(myContact.Status.IsNew); }

invisible setup [Test] public void TestEncryptedPassword() { Assert.AreEqual(encryptedPassword, encryptedConfig.Password); Assert.AreEqual(encryptedPassword, encryptedConfig.DecryptedPassword); encryptedConfig.SetPrivateKey(rsa.ToXmlString(true)); Assert.AreEqual(password, encryptedConfig.DecryptedPassword); }

huge fixture [TestFixtureSetUp] public void TestFixtureSetup() { SetupDBConnection(); DeleteAllContactPersons(); ClassDef.ClassDefs.Clear(); new Car(); CreateUpdatedContactPersonTestPack(); CreateSaveContactPersonTestPack(); CreateDeletedPersonTestPack(); } [Test] public void TestActivatorCreate() { Activator.CreateInstance(typeof (ContactPerson), true); }

too much information [Test] public void TestDelimitedTableNameWithSpaces() { ClassDef.ClassDefs.Clear(); TestAutoInc.LoadClassDefWithAutoIncrementingID(); TestAutoInc bo = new TestAutoInc(); ClassDef.ClassDefs[typeof (TestAutoInc)].TableName = "test autoinc"; DeleteStatementGenerator gen = new DeleteStatementGenerator(bo, DatabaseConnection.CurrentConnection); var statementCol = gen.Generate(); ISqlStatement statement = statementCol.First(); StringAssert.Contains("`test autoinc`", statement.Statement.ToString()); }

external dependencies these are probably integration tests

too many asserts [Test] public void TestDeleteFlagsSetContactPerson() { ContactPerson myContact = new ContactPerson(); Assert.IsTrue(myContact.Status.IsNew); // this object is new myContact.DateOfBirth = new DateTime(1980, 01, 20); myContact.FirstName = "Bob"; myContact.Surname = "Smith"; myContact.Save(); //save the object to the DB Assert.IsFalse(myContact.Status.IsNew); // this object is saved and thus no longer // new Assert.IsFalse(myContact.Status.IsDeleted); IPrimaryKey id = myContact.ID; //Save the objectsID so that it can be loaded from the Database Assert.AreEqual(id, myContact.ID); myContact.MarkForDelete(); Assert.IsTrue(myContact.Status.IsDeleted); myContact.Save(); Assert.IsTrue(myContact.Status.IsDeleted); Assert.IsTrue(myContact.Status.IsNew); }

duplication between tests repeated calls to constructors is duplication – refactor this.

test chaining

s….l….o….w t….e….s.…t….s …

no automated build process

debugging

test-only code in production #if TEST //... #endif if (testing) { //... }

randomly failing tests

designing testable software

use dependency injection aka dependency inversion aka inversion of control

Upon construction, give an object everything it needs to do everything it needs to do.

use a layered architecture

stub/substitute the underlying layers

use a testable UI pattern: Model-View-Presenter Model-View-Controller Model-View-ViewModel

choose libraries that allow for unit testing. or, build an anti-corruption layer (adapter)

test from the outside in check out the BDD talks at CodeLab!

further reading xUnit Test Patterns: Refactoring Test Code Gerard Meszaros The Art of Unit Testing Roy Osherove

Working effectively with legacy code Michael Feathers Growing object oriented software, guided by tests Steve Freeman and Nat Pryce even further reading