Micro Object Testing

30
Micro Object Testing Micro Object Testing Presentation to Presentation to ESUG 2003 Conference ESUG 2003 Conference Andy Berry Andy Berry [email protected] [email protected]

description

Micro Object Testing. Presentation to ESUG 2003 Conference Andy Berry [email protected]. What We’ll Talk About. What is MOT? How did it come about? Models and Views The MOT Principles The Benefits and Costs of MOT. But, First, An Amusing Fact…. - PowerPoint PPT Presentation

Transcript of Micro Object Testing

Page 1: Micro Object Testing

Micro Object TestingMicro Object TestingPresentation toPresentation toESUG 2003 ConferenceESUG 2003 Conference

Andy BerryAndy [email protected]@tof.co.uk

Page 2: Micro Object Testing

What We’ll Talk AboutWhat We’ll Talk About

• What is MOT?What is MOT?• How did it come about?How did it come about?• Models and ViewsModels and Views• The MOT PrinciplesThe MOT Principles• The Benefits and Costs of MOTThe Benefits and Costs of MOT

Page 3: Micro Object Testing

But, First, An Amusing But, First, An Amusing Fact…Fact…

• In previous presentations to In previous presentations to ESUG, I’ve never included any ESUG, I’ve never included any Smalltalk code!Smalltalk code!

• But, MOT is practical.But, MOT is practical.• So, there’s some code!So, there’s some code!• It may not be perfect, it may not It may not be perfect, it may not

be pretty, but…be pretty, but…• It seems to work!It seems to work!

Page 4: Micro Object Testing

What is MOT?What is MOT?

• MOT is a practical approach to MOT is a practical approach to testing OO code.testing OO code.

• It’s part of a trend towards more It’s part of a trend towards more ‘agile’ development methodologies ‘agile’ development methodologies like XP.like XP.

Page 5: Micro Object Testing

How did MOT come about?How did MOT come about?

• I’ve been using MOT informally for I’ve been using MOT informally for many years and over several many years and over several projects.projects.

• But, I was recently asked to explain But, I was recently asked to explain my approach to testing…my approach to testing…

• I wrote a paper and it needed a I wrote a paper and it needed a snappy title, so…snappy title, so…

• I called it ‘Micro Object Testing’I called it ‘Micro Object Testing’

Page 6: Micro Object Testing

Why ‘Micro’?Why ‘Micro’?

• Because it’s very simple!Because it’s very simple!• Because it doesn’t take much Because it doesn’t take much

effort.effort.• Because you only need a small Because you only need a small

amount of test code.amount of test code.• And, anyway, why not?And, anyway, why not?

Page 7: Micro Object Testing

Where does MOT fit?Where does MOT fit?

Page 8: Micro Object Testing

Models and ViewsModels and Views

Page 9: Micro Object Testing

Why separate them?Why separate them?

• Your model objects make up the Your model objects make up the ‘domain model’ that models the things ‘domain model’ that models the things you’re trying to handle.you’re trying to handle.

• Your views are messy and handle all the Your views are messy and handle all the nasties involved in dealing with those nasties involved in dealing with those horrible people – the users.horrible people – the users.

• I honestly feel I’ve failed if I can’t switch I honestly feel I’ve failed if I can’t switch views without altering my models.views without altering my models.

Page 10: Micro Object Testing

In Visualworks…In Visualworks…

Smalltalk.MicroObjectTesting defineClass: #Employeesuperclass: #{Core.Object}indexedType: #noneprivate: falseinstanceVariableNames:

'payrollNo name address telephone 'classInstanceVariableNames: ''imports: ''category: 'MicroObjectTestingExamples'

Models are defined like this:Models are defined like this:

Page 11: Micro Object Testing

Views are drawn in the UI Views are drawn in the UI Painter…Painter…

Page 12: Micro Object Testing

You open Views by…You open Views by…openView

"open a view on this object" 

| view | 

view := EmployeeView new.view

model: self;open

For this to work, the view must For this to work, the view must have an instance variable called have an instance variable called modelmodel

Page 13: Micro Object Testing

And Views are linked to And Views are linked to Models with ‘Adaptors‘Models with ‘Adaptors‘

address"answers aspect adaptor for the address field"  ^address isNil ifTrue: [address := AspectAdaptor subject: self model. address forAspect: #address; subjectSendsUpdates: true. address] ifFalse: [address]

Page 14: Micro Object Testing

You’d build an Example You’d build an Example Object like…Object like…exampleObject "answer an example of an Employee object"  " Employee exampleObject "  ^self new payrollNo: '374'; name: 'Fred Smith'; address: '12 Some Street, Somewhere'; telephone: '0111 222 333'

As a class method in the As a class method in the Employee classEmployee class

Page 15: Micro Object Testing

Did you notice?Did you notice?

• I slipped in a somewhat unusual I slipped in a somewhat unusual comment:comment:

“ “ Employee exampleObject “Employee exampleObject “• If you select this and inspect the result, If you select this and inspect the result,

what happens?what happens?• Well, if I’ve coded it correctly, you see Well, if I’ve coded it correctly, you see

an example of an Employee object…an example of an Employee object…• Very simple, but very, very useful!Very simple, but very, very useful!

Page 16: Micro Object Testing

It shows two of the twelve It shows two of the twelve MOT Principles…MOT Principles…

Ensure that running the tests is straightforward. Where possible, include the actual line of code needed to run the test.

Construct example objects for objects that hold data.

And:And:

Page 17: Micro Object Testing

You’d run a Test with…You’d run a Test with…basicTest

"perform a basic functional test" 

" Employee basicTest " 

| employee | 

employee := self exampleObject.employee openView

As another class method in As another class method in the Employee classthe Employee class

Page 18: Micro Object Testing

This shows another two This shows another two MOT Principles…MOT Principles…

Test methods must run stand-alone and display results for testers.

Build tests from the bottom-up.

And:And:

Page 19: Micro Object Testing

So, what are the MOT So, what are the MOT Principles?Principles?• Test frequently – every few minutes Test frequently – every few minutes

or as soon as you’ve completed a or as soon as you’ve completed a testable chunk of code.testable chunk of code.

• Test the domain objects, not the Test the domain objects, not the views.views.

• Construct example objects for objects Construct example objects for objects that hold data.that hold data.

• Test methods must run stand-alone Test methods must run stand-alone and display results for testers.and display results for testers.

Page 20: Micro Object Testing

And…And…

• Build tests from the bottom-up.Build tests from the bottom-up.• Test infrastructure code first, then Test infrastructure code first, then

application specific code.application specific code.• Re-use as much test code as you Re-use as much test code as you

can.can.• Keep the tests as simple as Keep the tests as simple as

possible.possible.

Page 21: Micro Object Testing

And, we’re over half way…And, we’re over half way…

• Ensure that running the tests is Ensure that running the tests is straightforward. Where possible, include straightforward. Where possible, include the actual line of code needed to run the the actual line of code needed to run the test.test.

• Whenever you change code, always re-Whenever you change code, always re-run the test for the code you’ve run the test for the code you’ve changed. If the changes affects the changed. If the changes affects the behaviour of the object, you may need behaviour of the object, you may need to re-run higher level tests as well.to re-run higher level tests as well.

Page 22: Micro Object Testing

And, the last two…And, the last two…

• Always bear in mind that the tests Always bear in mind that the tests form an essential knowledge base form an essential knowledge base about your application.about your application.

• Make corrections as soon as you Make corrections as soon as you can. If you find an error you can’t can. If you find an error you can’t correct immediately, write it down correct immediately, write it down so you won’t forget it.so you won’t forget it.

Page 23: Micro Object Testing

What’s this ‘Knowledge What’s this ‘Knowledge Base’ idea?Base’ idea?

• Let me illustrate with a example…Let me illustrate with a example…

• Other developer: ‘Andy, what Other developer: ‘Andy, what behaviour does a Employee have?’behaviour does a Employee have?’

• Me: ‘Well, if you look at the Me: ‘Well, if you look at the basicTestbasicTest method, you’ll see’ method, you’ll see’

Page 24: Micro Object Testing

He learns…He learns…• The EmployeeView (opened by the The EmployeeView (opened by the

test method) shows him what data test method) shows him what data an Employee has.an Employee has.

• He then looks at the Employee He then looks at the Employee class again and finds a class again and finds a databaseTestdatabaseTest method that shows method that shows how to load and save an Employee how to load and save an Employee object.object.

Page 25: Micro Object Testing

The Benefits of MOTThe Benefits of MOT

• Now, I’m not claiming that I’m Now, I’m not claiming that I’m anything special…anything special…

• BUT…BUT…• It amazes me how often parts of It amazes me how often parts of

applications I develop using MOT applications I develop using MOT work first time I run them.work first time I run them.

• That’s simply because they are That’s simply because they are based on firm foundations.based on firm foundations.

Page 26: Micro Object Testing

The Cost of MOTThe Cost of MOT

• I probably spend about 20% of my I probably spend about 20% of my time developing test methods.time developing test methods.

• It seems a lot, but I’m sure the It seems a lot, but I’m sure the benefits in terms of client (and, job) benefits in terms of client (and, job) satisfaction makes it worthwhile.satisfaction makes it worthwhile.

Page 27: Micro Object Testing

Introducing MOT on your Introducing MOT on your project…project…

• Why not just start developing low-Why not just start developing low-level test methods?level test methods?

• If you add comments to them so If you add comments to them so it’s obvious how to run them and it’s obvious how to run them and encourage other developers to encourage other developers to regard them as a source of regard them as a source of example code, you’ve done it!example code, you’ve done it!

Page 28: Micro Object Testing

Further InformationFurther Information

• This presentation will be available This presentation will be available onon– www.tof.co.uk/motwww.tof.co.uk/mot

• There’ll also be a copy of the ‘Micro There’ll also be a copy of the ‘Micro Object Testing’ paper on the same Object Testing’ paper on the same site.site.

Page 29: Micro Object Testing

What we’ve covered…What we’ve covered…

• What is MOT?What is MOT?• The model/view approach to OO The model/view approach to OO

developmentdevelopment• The twelve MOT PrinciplesThe twelve MOT Principles• Benefits and Costs of MOTBenefits and Costs of MOT• Where to find out moreWhere to find out more

Page 30: Micro Object Testing

Thanks for listening!Thanks for listening!

• I hope you’ve enjoyed this I hope you’ve enjoyed this presentation.presentation.

• Please, try MOT!Please, try MOT!• If you need any further information, If you need any further information,

just contact me on:just contact me on:– [email protected]@tof.co.uk