Rails Girls: Programming, Web Applications and Ruby on Rails
Contentful Testing at Scotland on Rails 2008
-
Upload
anthonybailey -
Category
Technology
-
view
1.071 -
download
2
description
Transcript of Contentful Testing at Scotland on Rails 2008
![Page 1: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/1.jpg)
Regression Therapy – Contentful Testing
Anthony Baileyhttp://anthonybailey.net
3 April 2008, Scotland on Rails
![Page 2: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/2.jpg)
Teaser
The conventional wisdom: # Assert explicitly and exactly what we care about.
assert_select(“divs#of_interest”, /my_desires/)
My dumb straw man alternative:# Everything should just be exactly like yesterday.
assert_equals(File.new(“expected.html”).read, @response.body)
2Regression Therapy – Contentful Testing
![Page 3: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/3.jpg)
Setting the scene
• I won’t try to behave.• When I say view content, I mean view
content.• When I don’t, I mean everything…• …even other languages and frameworks.
3Regression Therapy – Contentful Testing
![Page 4: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/4.jpg)
Setting the scene: terminology
• I won’t try to behave
Regression Therapy – Contentful Testing 4
![Page 5: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/5.jpg)
Setting the scene: scope
• When I say view content, I mean view content.– not the models the views display– not the presentation logic they use to display it– not the controller logic invoked by interacting
with the view– not the workflow that the views comprise– just the tags– (at least for now)
Regression Therapy – Contentful Testing 5
![Page 6: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/6.jpg)
Setting the scene: scope
• When I don’t say views, I mean everything.– Contentful testing can work in other domains
Regression Therapy – Contentful Testing 6
![Page 7: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/7.jpg)
Setting the scene: scope
• Other languages and frameworks– Contentful testing doesn’t need Ruby or Rails,• used it in a C++ desktop app, a Java/Spring web app
– But this talk will focus on a Rails plug-in:• http://contentful.rubyforge.org/
Regression Therapy – Contentful Testing 7
![Page 8: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/8.jpg)
Setting the scene: personal
• The plug-in was born of genuine need– but in a small, simple, CRUDdy app.
Regression Therapy – Contentful Testing 8
![Page 9: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/9.jpg)
Why do we test?
• To maintain known good behavior and catch accidental bad changes...........................!..!.....................!.........
• To drive intended changes >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>• To explore and understand emergent changes .....!!??????............!!??????........!!??????.......
Regression Therapy – Contentful Testing 9
![Page 10: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/10.jpg)
How do Rails devs test the view?
• Functional test, plus assert_select
Regression Therapy – Contentful Testing 10
![Page 11: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/11.jpg)
How do Rails devs test the view?
• Decoupled unit test, plus assert_select
Regression Therapy – Contentful Testing 11
![Page 12: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/12.jpg)
But views are not very assertive
• Assertion tests– don’t help exploration– are great for test-driving,• but view content doesn’t get much value from this.
– have selective coverage– are verbose when coverage needs to be high;• translation takes time and space
– are expensive to update when things change
Regression Therapy – Contentful Testing 12
![Page 13: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/13.jpg)
How much do Rails devs test the view?
• Not much!
• "I find that the View tests that RSpec and Zentest provide are fragile and provide dubious value. Rarely do they find bugs and often they break for unimportant reasons. I just don't find them to have a good return on investment.“ – Jay Fields, Thoughtworks.
• err.the_blog’s groupthink thought similar
Regression Therapy – Contentful Testing 13
![Page 14: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/14.jpg)
assert_select
Regression Therapy – Contentful Testing 14
![Page 15: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/15.jpg)
Past life regression
• Regression tests make some things a bit better– Any one test is simple to express• “the expected output is exactly as in this file”
– Expected output needs no translation• - it’s just a straight copy
• But they make other things much worse– Very expensive to maintain, because• each test is brittle, breaking on minor changes• a single production code change can break many tests
Regression Therapy – Contentful Testing 15
![Page 16: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/16.jpg)
Regression therapy• One step back, two steps forward• Dampen the noise
• diff the DOM, not the text - normalize• DRY up tested content
• Smooth the workflow• easily create tests• conveniently inspect changes• quickly accept changes• …and these should all work well in batch
Regression Therapy – Contentful Testing 16
![Page 17: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/17.jpg)
Contentful plug-in
% ruby script/plugin install svn://rubyforge.org/var/svn/contentful
Regression Therapy – Contentful Testing 17
![Page 18: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/18.jpg)
Regression Therapy – Contentful Testing 18
![Page 19: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/19.jpg)
Regression Therapy – Contentful Testing 19
![Page 20: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/20.jpg)
Regression Therapy – Contentful Testing 20
![Page 21: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/21.jpg)
Regression Therapy – Contentful Testing 21
![Page 22: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/22.jpg)
Regression Therapy – Contentful Testing 22
![Page 23: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/23.jpg)
Regression Therapy – Contentful Testing 23
![Page 24: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/24.jpg)
Regression Therapy – Contentful Testing 24
![Page 25: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/25.jpg)
Regression Therapy – Contentful Testing 25
![Page 26: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/26.jpg)
Regression Therapy – Contentful Testing 26
![Page 27: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/27.jpg)
Regression Therapy – Contentful Testing 27
![Page 28: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/28.jpg)
Regression Therapy – Contentful Testing 28
![Page 29: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/29.jpg)
Regression Therapy – Contentful Testing 29
![Page 30: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/30.jpg)
Regression Therapy – Contentful Testing 30
![Page 31: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/31.jpg)
Regression Therapy – Contentful Testing 31
![Page 32: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/32.jpg)
Other paths to contentfulness
• That was just one implementation– Really, I want to sell the contentful testing pattern
more than my particular Rails plug-in
• Capture a normalized from of entire output• and, automate new/test/review/accept
somehow• others Rails devs might prefer different Rake tasks• other devs might want something other than Rake• maybe use some IDE other than the command-line
Regression Therapy – Contentful Testing 32
![Page 33: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/33.jpg)
Pushing contentful testing• Leveraging the coverage/upkeep trade-off– I pay the tax on dense coverage to get value from
serendipitous discovery of changes
• Testing more than the view content– cover for exploration, spiking, and follow-on
• Bonus value in regular assertion tests– If you’re assertion testing controllers anyway, an
extra assert_contentful is almost free– I (guiltily) trade decoupling for convenience
Regression Therapy – Contentful Testing 33
![Page 34: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/34.jpg)
An obvious contentful usecase• Use the machinery as a temporary testing vise
(term due to Michael Feathers)
– Pin down all view content during a big refactor• extracting partials and helpers• refactoring form builders• changing template system
• CONTENTFUL_AUTO = true– assert_contentful in every functional test
Regression Therapy – Contentful Testing 34
![Page 35: Contentful Testing at Scotland on Rails 2008](https://reader033.fdocuments.us/reader033/viewer/2022051109/5485fbb1b4af9fb37a8b4885/html5/thumbnails/35.jpg)
http://contentful.rubyforge.org
Regression Therapy – Contentful Testing 35