copyright © 2005 charlie poole. all rights reserved test first user interfaces xp2005 sheffield...

63
Copyright © 2005 Charlie Poole. All rights reserved Test First User Test First User Interfaces Interfaces XP2005 Sheffield University June 18, 2005

Upload: theodore-weaver

Post on 20-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test First User InterfacesTest First User Interfaces

XP2005

Sheffield University

June 18, 2005

Page 2: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

ScheduleSchedule

2:00-3:00 IntroductionReview of Testing PracticesIs UI Testing Hard?Message of this Course

3:00 Break

3:15-4:15 PrinciplesPatternsPractices

4:15 Break

4:30-5:30 ToolsTechniquesConclusion

Page 3: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

IntroductionIntroduction

Page 4: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Some Some BackgroundBackgroundAbout MeAbout Me

Page 5: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

ContactContact Email

[email protected] [email protected]

Web Sites www.charliepoole.org www.pooleconsulting.com

Seattle XP Users Group www.seattlexp.org

NUnit Testing Framework www.nunit.org

Page 6: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Why Are We Here?Why Are We Here?

What sort of UI work do you do?What platforms do you use?What do you want from this course?

Page 7: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Testing PracticesTesting Practices

Page 8: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Agile DevelopmentAgile Development

Modern software development methodologies increasingly emphasize agility.– Increased pace of development– Prevalence of unstable requirements

Teams need to work as a cohesive unit, with strong communications and continuous feedback.

Traditional communication channels often fail in this setting.

Page 9: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Tests in XPTests in XP

Acceptance/Customer Tests– Tied to Requirements– Express customer intention

Unit/Programmer Tests– Small units of functionality tested in isolation– Express programmer intention

Page 10: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Tests as CommunicationTests as Communication

For each test there is a message– Criterion for success– Result

For each test there are two communicators– Whoever defines the test criteria– Whoever tries to satisfy the test criteria

Page 11: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Tests Communicate IntentTests Communicate Intent

From the customer– What the application should do

From the developer– How the application will do it

Page 12: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Test-Driven DevelopmentTest-Driven Development A fundamental practice of XP

But “extremely” valuable on it’s own Note that the term is overloaded

– The practice of writing tests and then making them pass– The “inner cycle” which includes TDD as one element

A three-step micro-cycle Make it Fail

– No code without a failing test Make it Work

– As simply as possible Make it Better

– Refactor

Page 13: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The Circle of CodeThe Circle of Code

Simple Design Test-Driven Development

Refactoring

Continuous Integration

Page 14: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Effect of Continuous TestingEffect of Continuous Testing

The state of the application is known at all times – based on which tests pass.

Requirements changes are accepted as a basic condition of development rather than being resisted.

Resulting code is more robust and – with continuous integration – can be ready for demos or even delivery at any time.

Page 15: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Automating TestsAutomating Tests

Tests must be automated if they are to be run as frequently as we need them to run.

Tools like SUnit, JUnit, CPPUnit and NUnit provide a framework for automating unit tests.

Tools like FIT and Exactor provide a framework for automating acceptance tests.

Page 16: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Is UI Testing Hard?Is UI Testing Hard?

Page 17: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

ExerciseExercise

What makes UI testing hard for you?Break into groups of three or four…

– List things that make UI testing difficult– Identify general versus platform-specific issues– What would be needed to resolve each issue?

Select one person to present your answers.

Page 18: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

DiscussionDiscussion

Page 19: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

What Makes Testing Hard?What Makes Testing Hard?

Programmer tests aim at test independence

But…Testing connected sets of objects is hard

– Tests become dependent on one another– Objects have to be put in a known state– It’s hard to figure out what failed

Page 20: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Problems of GUI TestingProblems of GUI Testing

Tight coupling to other parts of the application – may include business logic

UI is implemented as a single large program handling all user interaction

Design is driven by IDE code generationTechnical issues of the platform

Page 21: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Possible SolutionsPossible Solutions

Tight coupling to other parts of the application – may include business logic– Separate business logic from the UI– Use interfaces to permit substitution– Use factories to create business objects

Page 22: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Possible SolutionsPossible Solutions

UI is implemented as a single large program handling all user interaction– Use separate classes for UI logic– Use separate classes for validation– Use smart controls where feasible

Page 23: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Possible SolutionsPossible Solutions

Design is driven by IDE code generation– Resist the temptation to insert code into an the

UI just because the IDE makes it easy.– Explore the limits of your tools– Where possible, split the code into two parts

Page 24: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Possible SolutionsPossible Solutions

Technical issues of the platform– Learn your platform and language thoroughly– Explore key aspects needed for testing UI

Event generation and capture Instantiation of UI elements as part of a test Access to contained UI elements

– Be clear on what testing is needed

Page 25: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The “Hardness” ParadoxThe “Hardness” Paradox

Perhaps “hard to test” is a good thing…– Most of the problems that make GUI testing

“hard” are problems of design– “Hardness” can be a signal to look more deeply

at how our application is designed– Question: Are ALL such problems signals of

design issues? How can we tell?

Page 26: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

Page 27: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

GUI testing may be hard, but it’s no harder than many other aspects of

delivering great software.

Page 28: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

What initially presents as a testing problem usually turns out to be a

design problem.

Page 29: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

The design techniques we use to solve GUI testing problems are the same ones we use for business objects.

Page 30: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

GUI testing presents unique technical – as opposed to conceptual – issues.

Page 31: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

The general principles used for GUI testing are platform-independent.

Page 32: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The MessageThe Message

Some of the lower-level techniques require platform-specific knowledge, even though similar things must be accomplished on every platform.

Page 33: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The “Hardness” ParadoxThe “Hardness” Paradox

Perhaps “hard to test” is a good thing…– Most of the problems that make GUI testing

“hard” are problems of design– “Hardness” can be a signal to look more deeply

at how our application is designed– Question: Are ALL such problems signals of

design issues? How can we tell?

Page 34: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

An Objection?An Objection?

Should we be modifying the design merely to make the

software easier to test?

Page 35: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Yes, because…Yes, because…

Testability is just as valid an objective as security, safety, ease of deployment or any other desirable characteristic

Poor testability is almost always due to other problems in the design, particularly poor separation of responsibilities, and excessive coupling between components.

Page 36: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Questions?Questions?

Page 37: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Page 38: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Know exactly what you’re testing– Each test should have one purpose– Do you really need to test it?

Too simple to fail Part of the platform

Page 39: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Keep domain logic out of the UI– Use separate domain objects– Test them separately, without the UI– Make the UI as thin as possible– Consider NOT testing it!

Page 40: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Avoid tight coupling with the domain– Use creational patterns that allow substitution– UI should not create domain objects directly– Use interfaces in languages that require them– Mock the domain when testing the UI

Page 41: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Apply separation of concerns to the UI– Separate behavior from display– Use separate classes for validation– Use smart controls judiciously

Page 42: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Avoid code generation pitfalls– Resist the temptation to insert code into an the

UI just because the IDE makes it easy.– Explore the limits of your tools– Where possible, split the code into two parts

Page 43: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Learn your platform and language thoroughly– Explore key aspects needed for testing UI

Event generation and capture Instantiation of UI elements as part of a test Access to contained UI elements

– Be clear on what testing is needed

Page 44: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PrinciplesPrinciples

Understand your own common mistakes– Don’t test getters and setters…

unless you habitually mess them up

– Don’t test whether event’s are hooked up…unless you often forget to hook them up

Page 45: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Simple GUI ExampleSimple GUI Example

Page 46: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

BackgroundBackground

The NUnit test framework allows users to assign tests to categories and run all the tests in one or more of these categories.

A domain object maintains a list of all the available categories and another list of the currently selected categories.

The UI must display the available categories and allow users to select those to be used in a run.

Page 47: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

The ModelThe Model

Categories are represented by strings. The available and selected category lists are

represented as arrays of strings. Initially, the available category list is hard-coded. The following methods are provided:

GetAvailableCategories() GetSelectedCategories() SetSelectedCategories()

Page 48: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Simple GUISimple GUI

Page 49: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

DemonstrationDemonstration

Page 50: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

PatternsPatterns

Page 51: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Model-ViewModel-View

ViewModel

Page 52: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

MVCMVC

View

Controller

Model

Page 53: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

MVPMVP

Presenter

View

Model

With no dynamic features, View can remain ignorant of both

Presenter and Model.

Page 54: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

MVPMVP

Presenter

View

Model

View delegates event handling to Presenter. This is the classic implementation. Also called

HumbleDialog.

Page 55: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Extended GUI ExampleExtended GUI Example

Page 56: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Further TasksFurther Tasks

Modify SimpleGui to use MVPAdd Event Handling to ModelSupport changing available list

asynchronouslyMake gui richer: add second list box and

Add and Remove buttons.

Page 57: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Bits of AdviceBits of Advice

Page 58: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Three Rules of UI TestingThree Rules of UI Testing

Get business functions out of the UI– Use domain or controller objects– Test them separately

Get UI logic out of the Form– Separate controller objects– Derived or User Controls– Test them separately

Know what you’re testing for– Do you really need to test it?

Page 59: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Non-EssentialsNon-Essentials

Things that are usually non-essential– Precise positioning of controls– Shades of color– Exact sizes

They become essential– When they are hard requirements– When code depends on them

Page 60: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Specialized Test FrameworksSpecialized Test Frameworks

JFCUnitJWebUnitNUnitFormsNUnitAsp

Page 61: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

Testing Event HandlingTesting Event Handling

Test Generation of Events– Use a mock or fake to capture the events

Test Handling of Events– Use a fake to send the events

Page 62: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

UI Testing ApproachesUI Testing Approaches

Testing through the GUITesting immediately “under the glass”Isolated GUI testingExploratory testingVisual TDD

Page 63: Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005

Copyright © 2005 Charlie Poole. All rights reserved

Test Driven User Interfaces

ConclusionConclusion