Test-Driven Development (TDD) for Eclipse RCP

Download Report

Transcript Test-Driven Development (TDD) for Eclipse RCP

Test-Driven Development
(TDD) for Eclipse RCP
Red-Green-Refactor!
EclipseCon 2008
Kevin P. Taylor, Nicolaus Malnick
Kevin P. Taylor
• President, Obtiva
• Former editor: java.about.com
• Author: Test-Driven Development in
Java course
• Presenter: EclipseCon, EclipseWorld,
Agile Conference and other events
• Former technical lead on large Eclipse
RCP project
©2008 Obtiva Corporation
Nicolaus Malnick
• Senior Consultant, Obtiva
• Currently participating in a large Eclipse
RCP project
• Test-driven development and pair
programming advocate
©2008 Obtiva Corporation
How are We Doing?
• Who uses JUnit consistently?
- For Eclipse RCP applications?
• Who writes their tests first?
• Who writes their tests after?
- Who finds time?
©2008 Obtiva Corporation
Overview
• The Benefits of Test-Driven
Development
• The Challenges of Test Driving Eclipse
RCP
• The Heartbeat of Test-Driven
Development
• Exercise: A Simple Example
• Tools of the Test-Driven Trade
©2008 Obtiva Corporation
Overview Continued
• Exercise: Eclipse RCP TDD Example
• Functional Testing
• GUI Recorder Testing
• Conclusion
• References
©2008 Obtiva Corporation
The Benefits of Doing TDD
• Cleaner code
– Only code what you need
– Tests interact with an malleable API
• Coding Confidence
– Increase code reliability
– Ensure rapid feedback
• DRY Code (Don’t Repeat Yourself)
– Constantly remove duplication
– Ruthless refactoring
©2008 Obtiva Corporation
More Benefits of TDD
• Document our knowledge
• Technique for learning new libraries,
frameworks and API’s
• Ensure we are writing tests at all!
©2008 Obtiva Corporation
TDD Hurdles with Eclipse RCP
• Separation of concerns
• OSGi wires bundles at runtime
• SWT Widgets are difficult to mock
©2008 Obtiva Corporation
The Heartbeat of TDD
•
•
•
•
Red - Write a test that fails
Green - Make it pass
Refactor - Remove duplication
Repeat
©2008 Obtiva Corporation
Exercise: A Simple Example
Fire up Eclipse and follow along!
©2008 Obtiva Corporation
Tools of the Trade
•
•
•
•
JUnit and JMock
PDE JUnit
Model-View-Presenter (MVP) pattern
Presenter First Methodology
©2008 Obtiva Corporation
JUnit and JMock
• Use for classes that are not dependent
on Eclipse Platform API
• Particularly useful for testing business
logic in model classes
©2008 Obtiva Corporation
PDE JUnit (Plug-in JUnit)
• PDE JUnit tests are executed by a
special test runner that launches
another Eclipse instance in a separate
VM.
• This means your tests can call the
Eclipse Platform API, along with
methods from your own plug-in.
©2008 Obtiva Corporation
Enable Testing by Creating
Layers
• MVC
• MVP
• Presenter First
©2008 Obtiva Corporation
Model-View-Controller (MVC)
©2008 Obtiva Corporation
Model-View-Presenter (MVP)
©2008 Obtiva Corporation
MVP Helps Us
• Separate concerns
• Minimize untested GUI code
©2008 Obtiva Corporation
Databinding API as MVP
Courtesy of http://wiki.eclipse.org/JFace_Data_Binding_Introduction
©2008 Obtiva Corporation
Easier Eclipse RCP with
Presenter First
• Drive unit tests against Presenter
• View and Model are interfaces
– Mock Implementation
– Actual Implementation
• Implement only the behavior required
by each story
• Keep view dumb and minimal
©2008 Obtiva Corporation
Exercise: TDD Mail Client
Flip to Eclipse IDE!
©2008 Obtiva Corporation
Functional Testing
• Unit Tests are not a silver bullet
• Functional tests exercise:
presenter<->model<->DB
• How we can do it: Fitster/Fit
©2008 Obtiva Corporation
Fitster/Fit
• Pre-alpha
• Run Fit tests from within Eclipse RCP
applications
©2008 Obtiva Corporation
GUI Recorder Testing
• Eclipse Test & Performance Tools
Platform (TPTP)
• IBM Rational Tester
• SWTBot
©2008 Obtiva Corporation
Test & Performance Tools
Platform (TPTP)
• Automated GUI recorder
• API recorder
• Manual testing
©2008 Obtiva Corporation
IBM Rational Tester
• Automated, functional testing and
regression tool
©2008 Obtiva Corporation
SWTBot
• Functional testing tool for SWT
applications
• Should also work for Eclipse RCP
• Written by Ketan Padegaonkar, who is
presenting on it at EclipseCon 2008
©2008 Obtiva Corporation
Conclusion
• The Benefits of TDD
• The challenges we face test driving
Eclipse RCP applications
• Tools of the trade
• Functional testing
• GUI testing
©2008 Obtiva Corporation
References
• TPTP - http://www.eclipse.org/tptp/
• Fitster http://sourceforge.net/project/showfiles
.php?group_id=134927
• Rational Tester - http://www306.ibm.com/software/awdtools/tester/f
unctional/
©2008 Obtiva Corporation
References Continued
• SWTBot http://swtbot.sourceforge.net/wiki/Main_Page
• Gamma, E. & Beck, K., (2003), Contributing
to Eclipse: Principles, Patterns, and Plug-Ins,
Addison-Wesley.
• Alles, M., et al., (2006), “Presenter First:
Organizing Complex GUI Applications for TestDriven Development,” Agile 2006.
©2008 Obtiva Corporation