Writing Clean and Dry Infrastructure Test Code
-
Upload
vagif-abilov -
Category
Software
-
view
151 -
download
1
Transcript of Writing Clean and Dry Infrastructure Test Code
![Page 1: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/1.jpg)
Writing clean and DRY
infrastructure test code
Vagif Abilov, Miles AS
![Page 2: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/2.jpg)
About myself
Mail: [email protected]
Twitter: @ooobject
GitHub: object
BitBucket: object
Blog:
http://bloggingabout.net/blogs/vagif/default.aspx
Articles: http://www.codeproject.com
![Page 3: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/3.jpg)
Agenda
How good is test code? Or how bad?
Testers as first class citizens
10 steps that lead to cleaner test code
Case studies and examples
![Page 4: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/4.jpg)
![Page 5: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/5.jpg)
How good is test code? Or how bad?
What quality do you expect from test code?
Test code tends to have inferior quality comparing to
production code
Test code tends to have superior quality comparing to
production code
Test and production code tend to have same quality
How long is a test’s lifetime?
Test code lasts as long as production code
Test code can be discarded while production code is still
in use
Test code can outlive production code
![Page 6: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/6.jpg)
Testers as first class citizens
Anything that is deployed to production or shipped to customers is subject to more rigid requirements than internal matters
Product internals are external matters, test internals are internal matters
Software testers’ choices of tools and languages usually meet more liberal reception by management and principal architects
There is no excuse for a tester of not turning liberal management attitude to his/her own advantage –software testers have better chances to adopt most recent methods and technology
![Page 7: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/7.jpg)
10 steps that lead to cleaner test code
1. Bridge communication gap
2. Use executable specifications
3. Let actors act in scenario definitions
4. «Push HOW down the stack»
5. Reduce translation layers
6. Practice explorative scripting
7. Expose test automation frameworks
8. DRY – explore community
9. Embrace dynamic languages
10. Care only about persistent state that matters
![Page 8: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/8.jpg)
1. Bridge communication gap
“BDD is a 2nd generation, outside-in, pull-based, multiple-stakeholder, multiple-scale, high-automation, Agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters.” – Dan North
“BDD is not primarily a testing thing. You happen to get tests out of it afterwards, but there's usually still more testing that needs to be done. It's much more about developing an understanding of what you're about to produce.” – Liz Keogh
“Conversations are much more important than the BDD tools.” – Liz Keogh
![Page 9: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/9.jpg)
2. Use executable specifications
Scenario: The answer with the highest vote gets to the
top
Given there is a question «What is your favourite
color» with the answers
| Answer | Vote |
| Red | 1 |
| Green | 1 |
When I upvote the answer «Green»
Then the answer «Green» should be on the top
![Page 10: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/10.jpg)
Advantages of executable specifications
Written in a natural language (and local language)
Written in a formalised way
Integrated into daily builds
Living documentation
Set development milestones
Functional coverage
![Page 11: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/11.jpg)
Functional coverage
![Page 12: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/12.jpg)
Examples of executable specifications
Relish
Publishing executable specifications on the web
Examples: GDS/whitehall
brighterplanet/flight
![Page 13: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/13.jpg)
3. Let actors act in scenario definitionsFeature: Pay bill
Background: Prices
Given the following operations are available:
| operation | price |
| routine check up | 10 |
| shots | 5 |
Scenario: Dave pays for Fluffy
Given Dave owns a pet Fluffy
And Dave brings Fluffy into the clinic for the following operations:
| routine check up |
| shots |
When the veterinarian charges Dave for the visit
And Dave pays cash
Then Dave is given a receipt which looks like this:
Operations:
$10 (routine check up)
$5 (shots)
Total to pay: $15 from Mann Wynne’s blog
![Page 14: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/14.jpg)
Real world example
Adding an actor to a scenario
![Page 15: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/15.jpg)
Distributing election reports
Scenario: Received report is saved and notification message is enqueued
Given table RP07 doesn’t contain reports for 46010000
And notification queue is empty
When receival service receives report RP07 with data
| Element | Value |
| dateCreated | 2013-05-01T10:01:02 |
| identifier | 46010000 |
| mandates | Ap=6;H=5;V=4 |
Then receival service should return OK
And table RP07 should contain report for 46010000
And notification queue should contain message
| Code | Identifier | Mandates |
| RP07 | 46010000 | Ap=6;H=5;V=4 |(Code example 1)
![Page 16: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/16.jpg)
Revised scenario
Scenario: Parliament overview is updated shortly upon the report receival
Given I see following Parliament party mandates overview| Party | Mandates || Ap | 3 || H | 2 || V | 1 |
When receival service receives report RP07 with data| Party | Mandates || Ap | 6 || H | 5 || V | 4 |
And I wait 10 seconds
Then I should see following Parliament party mandates overview| Party | Mandates || Ap | 6 || H | 5 || V | 4 |
![Page 17: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/17.jpg)
4. «Push HOW down the stack»
As said by Seb Rose and Matt Wynne
Gojko Adzic defines three levels of UI test automation- business/rule- user interface/workflow- technical
![Page 18: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/18.jpg)
Specifications reflect a problem domain
From Matt Wynne’s blog
![Page 19: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/19.jpg)
5. Reduce translation layers
BDD fixtures translate between system concepts
and user concepts. The more work the fixtures do,
the more wrong your system is
@richardadalton
![Page 20: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/20.jpg)
Finding most efficient feature
implementation language
Scenario: Refunded items should be returned to stock
Given a customer buys a black jumper
And I have 3 black jumpers left in stock
When he returns the jumper for a refund
Then I should have 4 black jumpers in stock
![Page 21: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/21.jpg)
Step definition examples
1. C#/SpecFlow
2. Ruby
3. F#/TickSpec
(Code examples 2-4)
![Page 22: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/22.jpg)
6. Practice explorative scripting
Don’t rush building test automation framework before
you explore the system
Try finding REPL tool for the language of your tests
Explore the system from script sessions
Use exploration results to build automated tests
Beware that manual test scripts and automated tests
carry different contexts and usually can’t be easily
substituted
![Page 23: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/23.jpg)
DEMO
Web UI scripting using
scriptcs and FluentAutomation
![Page 24: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/24.jpg)
7. Expose test automation frameworks
If you have WebDriver APIs in your test methods,
you're doing it wrong (Simon Stewart)
UI
Tests Tests
UI
Automatio
n
API
![Page 25: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/25.jpg)
«Page» in PageObject can be misleading
Despite the term "page" object, these objects
shouldn't usually be built for each page, but rather
for the significant elements on a page (Martin
Fowler)
Fowler also gives an alternative name: Panel
In general PageObject classes in UI automation
library should not bear names of HTML pages
Rather than exposing logical page control API you
can model a test automation framework after user’s
behavior
![Page 26: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/26.jpg)
Example: Google search automation
Feature: Search for professional events
In order to keep updated about professional events
As a service subscriber
I want to be able to search for conferences
Scenario: Search for professional conference Web sites
Given I am a service subscriber
When I search for "Agile Testing Days"
Then I should receive results starting with "www.agiletestingdays.com"
![Page 27: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/27.jpg)
Page pattern vs. behavioral pattern
Page pattern fits well regression tests and
automation of external Web sites
Behavioral pattern focuses on high level user
activities and can be easier to apply when Web site
structure is subject to major changes
(Code examples 5-8)
![Page 28: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/28.jpg)
8. DRY – explore community
In fact it’s not just about DRY – it’s DRO (Don’t
Repeat Others)
There’s plenty of libraries (mostly open-source) that
have already solved your plumbing needs
Generating of pseudo-random test data
Converting Gherkin tables to DTOs
Comparing Gherkin tables with contents of data sources
Replacing DTOs with dynamic objects
![Page 29: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/29.jpg)
Example: user data validation
Scenario: Validate users
Given the following users exists in the database:
| Name | Birth date | Length in meters|
| John | 1940-10-09 | 1.80 |
| Paul | 1942-06-18 | 1.80|
| George | 1943-02-25 | 1.77 |
| Ringo | 1940-07-07 | 1.68 |
Then the following users must exist in the database:
| John | 1940-10-09 | 1.80 |
| Paul | 1942-06-18 | 1.80|
| George | 1943-02-25 | 1.77 |
| Ringo | 1940-07-07 | 1.68 |
(Code examples 9-12)
![Page 30: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/30.jpg)
9. Embrace dynamic languages
Less ceremony – more action
Access databases without creating proxy classes
and DTOs
Access Web services without importing WSDL files
Eliminate data transfer objects
Code examples 13-15)
![Page 31: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/31.jpg)
10. Care only about persistent state that matters
Don’t set expectations about persistent state for the
test
Don’t restore persistent state after the test
Don’t take an effort of complex data deletion routines
Just push aside data that stand on the way of your
test
Code examples (16-17)
![Page 32: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/32.jpg)
Once again...
1. Bridge communication gap
2. Use executable specifications
3. Let actors act in scenario definitions
4. Push HOW down
5. Reduce translation layers
6. Practice explorative scripting
7. Expose test automation frameworks
8. DRY – explore community
9. Embrace dynamic languages
10. Care only about persistent state that matters
![Page 33: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/33.jpg)
Karaoke time!
«Beautiful Code»
Christina Agile-Era
![Page 34: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/34.jpg)
![Page 35: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/35.jpg)
![Page 36: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/36.jpg)
![Page 37: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/37.jpg)
![Page 38: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/38.jpg)
![Page 39: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/39.jpg)
![Page 40: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/40.jpg)
![Page 41: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/41.jpg)
![Page 42: Writing Clean and Dry Infrastructure Test Code](https://reader034.fdocuments.net/reader034/viewer/2022042607/55a2bd851a28ab63268b45c5/html5/thumbnails/42.jpg)
Writing clean and DRY infrastructure test code
Thank you!
Vagif Abilov
@ooobject
GitHub: object
BitBucket: object