Top Banner
INTRODUCTION TO TEST DRIVEN DEVELOPMENT Presented by Sarah Dutkiewicz [email protected]
44

Introduction to Test Driven Development

May 11, 2015

Download

Technology

An intro to Test Driven Development for Developers and Non-Developers - covering some basics of TDD and ATDD. Presented to UXPA Cleveland at OverDrive on February 27, 2014
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Test Driven Development

INTRODUCTION TO TEST DRIVEN DEVELOPMENTPresented by Sarah Dutkiewicz

[email protected]

Page 2: Introduction to Test Driven Development

AGENDA

• Test Driven Development – What does this mean?

• Gathering Requirements

• Gherkin

• Tools for TDD in the Workplace

Page 3: Introduction to Test Driven Development

WHAT IS TEST DRIVEN DEVELOPMENT?

Page 4: Introduction to Test Driven Development

TEST DRIVEN DEVELOPMENT

• Define {something} first through a test

• Write the code to pass the test

• Verify that the test succeeds

• Improve upon the code and keep the test passing

Page 5: Introduction to Test Driven Development

Write the test

Write the code to pass the

test

Improve the code while keeping the test passing

RED-GREEN-REFACTOR

Page 6: Introduction to Test Driven Development

EXAMPLE – RUBY KOANS

Page 7: Introduction to Test Driven Development

EXAMPLE – RUBY KOANS

Page 8: Introduction to Test Driven Development

TDD CONCEPT - ASSERTIONS

• Verify whether a certain condition has been met

• Asserts come in many forms:• (Not) Equal• Contains / Any• Is a Type• Is an Instance of a Type• Is (Not) Null• Is (True/False)

• Design Guideline – One assert per test

Page 9: Introduction to Test Driven Development

TDD CONCEPT – ARRANGE/ACT/ASSERT

• Pattern for arranging a test

1. Arrange all preconditions and inputs.

2. Act on the object or method.

3. Assert that the results have occurred.

Page 10: Introduction to Test Driven Development

TEST-DRIVEN – TYPES OF TESTSTests, Tests, and More Tests… Oh My!

Page 11: Introduction to Test Driven Development

TYPES OF TESTS

• Unit Tests

• Integration Tests

• End-to-End Tests

• Exploratory Testing

Page 12: Introduction to Test Driven Development

UNIT TESTS

• Focus on a class or a method

• Tests the smallest unit possible

• Typically tests simple objects; does not test things such as:• Database communication• Network communication• File system manipulation

Page 13: Introduction to Test Driven Development

INTEGRATION TESTS

• Tests functions, “does things”

• Tests interactions with the outside world, include:• Database communication• Network communication• File system manipulation

• Focused integration tests isolate the testing to one interaction at a time

• Integration tests should run on their own, with a little help from 2 fundamental units:• Setup – run at the beginning of the test to set up

the test environment• Tear-down – run at the end of the test or upon

error to clean up the test environment

Page 14: Introduction to Test Driven Development

END-TO-END TESTS• The most brittle of tests –

dependent on the big picture

• Verifies that the unit tests and integration tests are working like they should

• Start at the beginning and go through the whole process

• Includes:• Acceptance testing• Functional testing

Page 15: Introduction to Test Driven Development

EXPLORATORY TESTING

• Not an automated process; manual testing

• Sometimes better to go this route rather than end-to-end tests – depending on the design and architecture of your application

• EXPLORE!• Discovery• Investigation• Learning

Page 16: Introduction to Test Driven Development

MORE *-DRIVEN DEVELOPMENTRelated philosophies and methodologies

Page 17: Introduction to Test Driven Development

OTHER RELATED *-DRIVEN DEVELOPMENT

• Behavior Driven Development

• Acceptance Test Driven Development

• Specification by Example

Page 18: Introduction to Test Driven Development

ACCEPTANCE TEST DRIVEN DEVELOPMENT CYCLE

Discuss the requirements

Distill the tests in a

friendly format

Develop the code (and hook the code to the

tests)

Demo the code

Page 19: Introduction to Test Driven Development

ATDD AND THE TDD CYCLE

Discuss the requirements

Distill the tests in a

friendly format

Develop the code (and hook the code to the

tests)

Demo the code

RED

GREEN

REFACTOR

GREEN

Page 20: Introduction to Test Driven Development

GATHERING REQUIREMENTSThe Importance of TDD & ATDD for the UX Realm and an Intro to Gherkin

Page 21: Introduction to Test Driven Development

• Conducted by all who are involved:• Product owners• Business analysts• Developers• QA

• Requirements are explicitly spelled out.• Include use cases.• Include required behaviors or designs.

DISCUSS

Page 22: Introduction to Test Driven Development

GHERKIN

• Common language for gathering requirements

• Written in “plain English” following a particular cadence

• Can then be hooked up to various programming languages and testing tools

• Serves as guidelines for automated tests as well as project documentation

Page 23: Introduction to Test Driven Development

GHERKIN COMPONENTS

• Features

• Scenarios & Scenario Outlines

• Backgrounds

• Steps

• Multiline Arguments

• Tags

Page 24: Introduction to Test Driven Development

FEATURES

• Define a feature of an application

• Starts with the Feature keyword and contains a few lines to define the feature

Example:

Feature: Short, concise, descriptive text of the goal

In order to do something

As someone related to this system

I want to gain something out of this

* Features are stored in a *.feature file

Page 25: Introduction to Test Driven Development

FEATURE EXAMPLE

Feature: Checking out books

In order to read eBooks on my eBook reader,

As a library patron,

I want to check out eBooks.

Page 26: Introduction to Test Driven Development

SCENARIOS

• Possibilities of situations (scenarios) that apply to a feature

• Scenarios are included in *.feature files with their relevant feature

• Created with one or more steps

Page 27: Introduction to Test Driven Development

SCENARIO EXAMPLE

Scenario: Checking out a book

Given the library collection has the book I want to check out

When I check out the book

Then the library collection’s available count is reduced by 1

Page 28: Introduction to Test Driven Development

STEPS

• Given a certain given condition

• When a certain behavior happens

• Then a certain outcome is expected

• Additional keywords include But and And

• Given a certain given condition

• And another given condition

• When a certain behavior happens

• Then a certain outcome is expected

Page 29: Introduction to Test Driven Development

SCENARIO OUTLINES

• Scenario Outlines eliminate the need for copying and pasting like scenarios and collapsing values into variables.

• Rather than starting with Scenario, it starts with Scenario Outline.

• Variables (placeholders) are denoted with names sandwiched in greater-than and less-than symbols.

Page 30: Introduction to Test Driven Development

SCENARIO OUTLINE EXAMPLE

Scenario Outline: Checking book checkout expiration

Given a checkout period of <checkout_period> days

When I open the book at day <open>

Then the book should expire in <left> days

Examples:

| checkout_period | open | left |

| 7 | 2 | 5 |

| 14 | 10 | 2 |

| 21 | 18 | 3 |

Page 31: Introduction to Test Driven Development

MULTILINE ARGUMENTS

• Tables

Example:

Scenario:

Given the following accounts exist:

|name |email |account_type|

| Laura |[email protected] | Admin |

| Sarah |[email protected] | Admin |

| Kevin | [email protected] | User |

Page 32: Introduction to Test Driven Development

MULTILINE ARGUMENTS

• Large paragraph of text

Example:

Scenario:

Given a description search with:

" " " It was the best of times It was the worst of times " " "

Page 33: Introduction to Test Driven Development

BACKGROUNDS

• Backgrounds setup the environment for all scenarios in a feature file.

• Starts with the Background keyword and is typically made up of Given, And, and But clauses

• Runs before individual scenario setup routines

Page 34: Introduction to Test Driven Development

BACKGROUND EXAMPLE

Feature: Checkout eMaterials

Background:

Given a customer named “Sarah Dutkiewicz“

And a library card numbered “12345678901”

And a checkout queue of books:

| title | author |

| Hop on Pop | Dr. Seuss |

| Harold and the Purple Crayon | Crockett Johnson |

| Shark Tales: How I Turned $1,000 into a Billion Dollar Business | Barbara Corcoran|

Page 35: Introduction to Test Driven Development

TAGS

• Used for grouping like tests, scenarios, and/or features together

• Starts with a @, followed by the tag name

Examples:

@UI @accounting @security

• Many test runners support tags and allow collections of tests to be run by tag

Page 36: Introduction to Test Driven Development

TOOLS FOR TDD IN THE WORKPLACE

Page 37: Introduction to Test Driven Development

GHERKIN LANGUAGE RESOURCES AND GENERAL TDD

RESOURCES• Behat – Writing Features – Gherkin Language

• The Art of Agile Development: Test-Driven Development

• Test first != TDD

• Driving Development with Tests: ATDD and TDD

• Let’s Explore – Exploratory Testing

Page 38: Introduction to Test Driven Development

.NET TDD RESOURCES• SpecFlow – Behavior Driven Development,

Acceptance Test Driven Development, Specification by Example; includes support for Silverlight, Windows Phone, and Mono

• TestDriven.Net – Visual Studio integration for unit tests

• WatiN – Web Application Testing in .NET

• TestStack.White – UI automation testing

• Telerik Test Studio

• Nunit

• MbUnit

• MSTest

• NCover

• TypeMock

Page 40: Introduction to Test Driven Development

RUBY TDD RESOURCES

• Cucumber – behavior driven development

• Watir – Web Application Testing in Ruby

• Ruby Koans – learn Ruby via testing

• Rspec – primary Ruby testing tool

Page 41: Introduction to Test Driven Development

PHP TDD RESOURCES

• Behat – behavior driven development

• Mink – web acceptance testing

• PHPUnit

• SimpleTest

• Phpt

• Gouette – headless browser, web scraper

Page 42: Introduction to Test Driven Development

JAVA TDD RESOURCES

• JUnit

• Watij – Web Application Testing in Java

• AppPerfect Java Unit Test

Page 43: Introduction to Test Driven Development

ADDITIONAL *DD RESOURCES

• Sahi – JavaScript browser controller

• Selenium – supports C#, Java, Perl, PHP, Python, Ruby

• Fitnesse – supports multiple languages

• Windmill – supports Python, JavaScript, Ruby

• Canoo WebTest – supports Python, JavaScript

• TSQLUnit – TDD for Transact-SQL

• TST – the T-SQL Test Tool

Page 44: Introduction to Test Driven Development

CONTACT INFORMATION

Sarah Dutkiewicz

Cleveland Tech Consulting, LLC

[email protected]

Twitter: @sadukie

Blog: http://codinggeekette.com