Agile.NET Development Test-driven Development using NUnit



Similar documents
Fail early, fail often, succeed sooner!

JUnit. Introduction to Unit Testing in Java

Test Driven Development Part III: Continuous Integration Venkat Subramaniam

Testing Rails. by Josh Steiner. thoughtbot

WORKING IN TEAMS WITH CASECOMPLETE AND MICROSOFT VISUAL SOURCE SAFE. Contents

The Social Accelerator Setup Guide

Test Automation Integration with Test Management QAComplete

How To Test Your Web Site On Wapt On A Pc Or Mac Or Mac (Or Mac) On A Mac Or Ipad Or Ipa (Or Ipa) On Pc Or Ipam (Or Pc Or Pc) On An Ip

Load testing with. WAPT Cloud. Quick Start Guide

Hosting Users Guide 2011

Continuous Integration

Test-Driven Development

Continuous Integration with CruiseControl.Net

Frequently asked questions.

File Manager Pro User Guide. Version 3.0

Leveraging Rational Team Concert's build capabilities for Continuous Integration

Using GitHub for Rally Apps (Mac Version)

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

Test Driven Development

Programming in Access VBA

Unit Testing & JUnit

How To Test In Bluej

Week 2 Practical Objects and Turtles

Version Control with Subversion and Xcode

Introduction to MS WINDOWS XP

SonicWALL CDP 5.0 Microsoft Exchange InfoStore Backup and Restore

Towards Software Configuration Management for Test-Driven Development

SPECIFICATION BY EXAMPLE. Gojko Adzic. How successful teams deliver the right software. MANNING Shelter Island

Web Forms. Step One: Review Simple Contact Form

Continuous Integration with CruiseControl.Net

A Practical Guide to Test Case Types in Java

Java course - IAG0040. Unit testing & Agile Software Development

Applying for Archive Service Accreditation: Using the online application system

How To Install And Run Cesview Iii (For New Users)

Virtual Office Remote Installation Guide

Apple Mac Fundamentals: A Tutorial. Updated 24/4/2013 By Mac Thing enquiries@macthing.co.uk Table of Contents:

Word 2010: Mail Merge to with Attachments

SENDING S & MESSAGES TO GROUPS

BUILD2WIN Lab 2.6. What s New in BUILD2WIN?

TSM for Windows Installation Instructions: Download the latest TSM Client Using the following link:

Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou

How To Create A Digital Signature And Sign A Document With Adobe Reader XI

Application Note Windows 2008 and IBM Tape Diagnostic Tool (ITDT-GE)

Software Engineering I (02161)

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

Online Backup and Recovery Manager Setup for Microsoft Windows.

Capture Pro Software FTP Server System Output

03_Events Trainingv3 1

XP & Scrum. extreme Programming. XP Roles, cont!d. XP Roles. Functional Tests. project stays on course. about the stories

A quick guide to setting up your new website

When you publish data to a SharePoint site, you first

Sky Broadband upgrading your router software

Making the Right Choice

Welcome to GIology. Welcome to GIology v2 November '08 GIology user guide Page 1 of 49

On premise upgrade guide (to 3.3) XperiDo for Microsoft Dynamics CRM

Exploratory Testing in an Agile Context

Create!form Folder Monitor. Technical Note April 1, 2008

You may have been given a download link on your trial software . Use this link to download the software.

DAZZLE INTEGRATED DATA BACKUP FEATURE.

Effective unit testing with JUnit

Paper Designing Web Applications: Lessons from SAS User Interface Analysts Todd Barlow, SAS Institute Inc., Cary, NC

My Secure Backup: How to reduce your backup size


Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

PCRecruiter Resume Inhaler

Working together with Word, Excel and PowerPoint

by Jonathan Kohl and Paul Rogers 40 BETTER SOFTWARE APRIL

Intellect Platform - The Workflow Engine Basic HelpDesk Troubleticket System - A102

Your Online Utility Dashboard

SPHOL207: Database Snapshots with SharePoint 2013

Change Manager 5.0 Installation Guide

Alteryx Predictive Analytics for Oracle R

Creating Online Surveys with Qualtrics Survey Tool

WORKING IN TEAMS WITH CASECOMPLETE AND ACCUREV. Contents

Windows 7 Hard Disk Recovery

USO-FX enables the transfer of files to individuals and groups of users with complete confidence that the data is carried securely at every stage.

CRM Setup Factory Installer V 3.0 Developers Guide

How to output SpoolFlex files directly to your Windows server

Remote Online Support

This section provides a 'Quickstart' guide to using TestDriven.NET any version of Microsoft Visual Studio.NET

So you want to create an a Friend action

Hypercosm. Studio.

Subversion Integration for Visual Studio

Teamstudio USER GUIDE

In the same spirit, our QuickBooks 2008 Software Installation Guide has been completely revised as well.

How to Install the Cisco AnyConnect VPN Client. Installing Cisco AnyConnect VPN Client on Windows with the Chrome Browser (Recommended)

ADVANCED GUIDE TO REFWORKS

When To Work Scheduling Program

Active Interest Media File Transfer Server Initial Client Install Documentation

CaseWare Audit System. Getting Started Guide. For Audit System 15.0

How to Use JCWHosting Reseller Cloud Storage Solution

Synthetic Monitoring Scripting Framework. User Guide

Test-Driven Development and Continuous Integration

Blitz! Finding your inbox unmanageable? Follow the tips in this document to take back control.

Transcription:

Agile.NET Development Test-driven Development using NUnit Jason Gorman

Test-driven Development Drive the design and construction of your code on unit test at a time Write a test that the system currently fails Quickly write the code to pass that test Remove any duplication from the code Move on to the next test

TDD Benefits Focuses your efforts on specific concrete test cases and helps pace development (micro-iterations) Provides continuous feedback about whether your code is working Boosts confidence when working on complex systems Evolves code to do exactly what you need and no more Provides a suite of tests you can run often to be sure your code is always working Code is easier to change without inadvertently breaking dependant code Means better assurance when integrating your code into CVS, VSS etc

Iterations Nominate test scenarios for iteration developer customer Progress to next iteration Estimate time required to satisfy scenarios Choose scenarios that will be tackled in this iteration Mini-iterations Breaks down scenario s implementation into development tasks and estimates time required for each task. If a task cannot be estimated because it is new to the developer, do some exploratory development (sometimes called a spike ) to get a feel for what s involved. Measure project velocity as the amount of development work (estimated days worth) that gets done in an average iteration, which tells the customer how much work can be scheduled for this iteration. They must choose scenarios whose estimates add up to what s achievable at the project velocity.

Mini-iterations Analysis & Design for this scenario developer customer Develop unit test list from high-level design CRC cards, sequence diagrams and other ways of assigning responsibilities for test outcomes can be used to visualize unit tests required Micro-iterations Acceptance test software to confirm requirement is satisfied When the developer believes that a test scenario has been satisfied, he informs the customer, who performs an acceptance test to confirm this. If the customer is satisfied, move on to the next system test scenario. If not, go back and fix the problem(s).

Micro-iterations Write a unit test that the code currently does not pass Quickly write the code to pass that test developer Refactor the code to remove any duplication

Introducing xunit Original framework developed for Smalltalk by Kent Beck Versions for most programming languages (CppUnit for C++, JUnit for Java, Nunit for.net etc) Simple concepts Test fixture : a class that contains one ore more test methods Test method : a method that executes a specific test Test runner : an application that finds and executes test methods on test fixtures Assertion : a Boolean expression that describes what must be true when some action has been executed Expected Exception : the type of an Exception we expect to be thrown during execution of a test method Setup : Code that is run before every test method is executed (eg, logging in as a particular user or initializing a singleton) Teardown : Code that is run after every test method has finished (eg, deleting rows from a table that were inserted during the test)

Getting Started with NUnit 1. Download and install NUnit 2. If you re using Visual Studio.NET, download and install the NUnit add-in

Creating a Test Project 1. Create a library project with a name that makes it obvious which project this will be testing (eg, <project under test name>.tests.dll) 2. In the test project, add a reference to the nunit.framework.dll library found in the \bin directory where Nunit is installed (eg, C:\Program Files\NUnit V2.0\bin\nunit.framework.d ll) 3. Add a reference to the project under test

Creating a Test Fixture 1. Add a new class to the test project with a name that makes it obvious which class it will be testing (eg, <class under test name>tests) 2. Import the NUnit.Framework namespace and add the custom attribute [TestFixture] to the class declaration using System; using System; using NUnit.Framework; using NUnit.Framework; namespace Tutorials.Tdd.Tests namespace Tutorials.Tdd.Tests [TestFixture] [TestFixture] public class AccountTests public class AccountTests

Creating a Test Method 1. Declare a public method with a name that makes it obvious what the test case is (eg, <method under test><test scenario> 2. Add a attribute to the method declaration [TestFixture] [TestFixture] public class AccountTests public class AccountTests public void WithdrawWithSufficientFunds() public void WithdrawWithSufficientFunds()

Write the Test Assertions First 1. Don t write the code needed to execute the test scenario until you ve written the assertions the code needs to satisfy first 2. If there s more than one assertion, consider working one assertion at a time public void WithdrawWithSufficientFunds() public void WithdrawWithSufficientFunds() Assertion.AssertEquals(oldBalance - a mount, account.balance); Assertion.AssertEquals(oldBalance - a mount, account.balance); expected value actual value

Write the Test Body 1. Write the test code needed to execute the scenario public void WithdrawWithSufficientFunds() public void WithdrawWithSufficientFunds() float oldbalance; float oldbalance; float amount = 250; float amount = 250; Account account = new Account(); Account account = new Account(); oldbalance = account.balance; oldbalance = account.balance; account.withdraw(amount); account.withdraw(amount); Assertion.AssertEquals(oldBalance - a mount, account.balance); Assertion.AssertEquals(oldBalance - a mount, account.balance);

Write the Code To Pass The Test and only the code needed to pass the test public class Account public class Account private float balance = 0; private float balance = 0; public void Withdraw(float a mount) public void Withdraw(float a mount) balance = balance - amount; balance = balance - amount; public float Balance public float Balance get get return balance; return balance;

Running the test with the NUnit Add-in 1. Right click on the test method in the code window 2. Select Run Test(s) 3. The test results will be displayed in Output window

Running the test with the NUnit GUI 1. Launch the NUnit GUI fro m your Windows start menu 1. Launch the NUnit GUI fro m your Windows start menu 2. Click File -> Open 2. Click File -> Open 3. Select the DLL created when you built your test project 3. Select the DLL created when you built your test project 4. The NUnit GUI displays all available test fixtures and test 4. The NUnit GUI displays all available test fixtures and test methods methods 5. Select the test method you wish to run and click the 5. Select the test method you wish to run and click the Run button Run button 6. If the test passes then you will get a green bar 6. If the test passes then you will get a green bar 7. If the test fails you will get a red bar 7. If the test fails you will get a red bar

Move On To The Next Test public void DepositAmountGreaterThanZero() public void DepositAmountGreaterThanZero() float oldbalance; float oldbalance; float amount = 250; float amount = 250; Account account = new Account(); Account account = new Account(); oldbalance = account.balance; oldbalance = account.balance; account.deposit(amount); account.deposit(amount); Assertion.AssertEquals(oldBalance + a mount, account.balance); Assertion.AssertEquals(oldBalance + a mount, account.balance);

Get That Test To Pass Quickly public class Account public class Account private float balance = 0; private float balance = 0; public void Withdraw(float a mount) public void Withdraw(float a mount) balance = balance - amount; balance = balance - amount; public void Deposit(float amount) public void Deposit(float amount) balance = balance + amount; balance = balance + amount; public float Balance public float Balance get get return balance; return balance;

Remove Any Duplication fro m the code under test and from the test code (remember that the test code needs to be as easy to change as the model code its testing) code under test [TestFixture] [TestFixture] public class AccountTests public class AccountTests private float oldbalance; private float oldbalance; private float amount = 250; private float amount = 250; private Account account; private Account account; [SetUp] [SetUp] public void SetUp() public void SetUp() account = new Account(); account = new Account(); oldbalance = account.balance; oldbalance = account.balance; public void WithdrawWithSufficientFunds() public void WithdrawWithSufficientFunds() account.withdraw(amount); account.withdraw(amount); Assertion.AssertEquals(oldBalance - a mount, account.balance); Assertion.AssertEquals(oldBalance - a mount, account.balance); public void DepositAmountGreaterThanZero() public void DepositAmountGreaterThanZero() account.deposit(amount); account.deposit(amount); Assertion.AssertEquals(oldBalance + a mount, account.balance); Assertion.AssertEquals(oldBalance + a mount, account.balance);

And So On

TDD The Golden Rule Never write model code unless you have a test that requires it

TDD Best Practices Only work on one test at a time Don t write a whole bunch of tests and then try to pass them all in one go Write test assertions self-explanatory so other developers can readily see what the code is supposed to be doing just by reading the tests balance == oldbalance amount rather than balance == 50 Make the structure of your test projects follow the structure of the projects they are testing so developers can easily navigate from the tests to the code they are testing and vice-versa Keep the tests small More than 15 minutes on the same test is probably too long. You will lose focus. If a test looks complicated, think of ways to break it down. If a test involves several methods or classes that you ll have to write to pass the test then either: Write tests for those first and work your way up to the bigger test (bottom-up TDD) Fake them to pass this test and then work your way down to implement each of them one test at a time (top-down TDD) If you re working in a team, integrate your code changes every few unit tests (say, every hour or two)* to make sure there aren t any nasty surprises waiting for you in someone else s changes If you re working alone, end every day with one broken test to help you quickly refocus the next morning If you re working in a team, never ever leave with broken tests! Automate acceptance tests, too (this is a challenge, I must warn you) so that you can be confident when you re done and ready to release it to the customer, and also so that you know everything that s been accepted will stay like that Take regular breaks and work reasonable hours. TDD is supposed to set a pace that can be sustained indefinitely.

TDD & Continuous Integration Before integrating your code changes tell all team members that you are about to. Until you have a successful build on the integration server, nobody else should consider integrating their changes Get any changed files that have been checked in to your team s repository (eg, VSS, CVS) since you last checked out the code and merge those with your changes Run all of the tests Do not integrate if any of the tests fail If all tests pass then check in your code changes Do a build with the latest code in the repository on the integration server and run all of the tests against that build If all the tests pass then you have a successful build and other developers can start to integrate If any tests fail then you have a broken build, which means nobody else can start integrating your team s priority is now to find out why the test(s) fail and fix the problem ASAP