Using RDFa for testing templates
By Yves on Tuesday 28 June 2011, 11:52 - Permalink
I promised a bunch of people from the BBC that I would write about this, so here it is! This is my first Software Engineering-related post, bear with me :)
We recently released a new iteration of /programmes, built on top of a completely new technology stack. As part of that move, we decided we wanted to review our template testing strategy. In our old application, the process for a new feature would basically be:
- Software Engineer writes models, controllers and data views;
- Web Developer writes XHTML templates;
- Software Engineer writes controller tests, which would actually test the routes, the controllers and the templates (such controller 'unit tests' are actually fairly standard across MVC frameworks, for some reason - e.g. in Zend)
Those controller tests were based on CSS selectors or XPaths. Therefore, any time a small front-end tweak needed to be done, the controller tests would break, which is very annoying for everyone.
We had two problems:
- Our controller 'unit tests' were not really unit tests - front-end developers shouldn't have to understand the whole routing, controllers, models for making a front end change.
- Using CSS selectors or XPaths for template tests is brittle. We don't want our tests to break every time we change the name of a CSS class.
In order to solve the first problem, we divided our controller tests in route tests (here is a route, assert that the application forwards it to the right controller/action with the right parameters), real unit controller tests (mock the models, call an action with some request parameters, check that the right data is sent to the view), and template tests.
In order to solve our second problem, we made those template tests rely on RDFa markup embedded within the page. In order to test a template, we create some mock data, generate the template using this data, and check the right RDF triples can be extracted from the resulting page. It ensures that tests are actually based on data - front-end changes won't have an impact on them. We just want to make sure we present the right data to the user. As the tests are not relying on other application code, it also means that someone writing the templates can maintain his own test suite.
A simple example of one of these tests is the following one:
public function testLetterSearch()
{
$this->setDefaultComponent('/components/atoz/letters');
$data = (object) array(
"by" => "by",
"search" => "b",
"slice" => "player",
"letters" => array('@', 'a', 'b', 'c'),
);
$this->assertTriples($data, array(
array('/programmes/a-z/by/b', 'rdfs:seeAlso', '/programmes/a-z/by/%40/player'),
array('/programmes/a-z/by/b', 'rdfs:seeAlso', '/programmes/a-z/by/a/player'),
array('/programmes/a-z/by/b', 'rdfs:seeAlso', '/programmes/a-z/by/c/player'),
));
}
which can be read as if you are displaying a list of letters on an A-Z
page and you have selected one, you shouldn't link to that one
Another very nice side-effect is that developers have a motivation to put lots of RDFa inside our pages! Compare the 168 triples extracted from a new /programmes page, including full tracklist information, programme metadata and broadcasts, to the 18 triples extracted from an old one. And as we add new components to this page, more RDFa will become available.
Also, the speed at which our developers picked up RDFa (1.0, not even 1.1, which is apparently simpler) defeats the eternal argument about RDFa being too complicated, but that's just my opinion :) The RDFa cheat sheet has proved immensely helpful.
Comments
Great post!
How does assertTriples() work? Is it an RDFa parser?
Also, referring to this comment:
> which can be read as "if you are displaying a list of letters on an A-Z page
> and you have selected one, you shouldn't link to that one"
Did you have any success with creating test helpers that are reusable/intention-revealing? I found that quite painful in php-unit, but if possible it would be nice to have a test helper like
assertPageHasLinkToLetter('a') assertPageHasNoLinkToLetter('b')etc. which would remove the need for a comment explaining what the test does. RSpec has an option to format the test output into readable documentation when the tests have nice names, not sure if something similar is possible with phpunit.
Hello Chris!
assertTriples takes the data passed as a first parameter, use it to inflate the template '/components/atoz/letters', extracts the triples from the inflated template, and checks the extracted triples match the ones given as a second parameter. It also does some blank-node matching logic (nothing too fancy though).
Wrt. your second comment, I haven't had any luck, no. It should be possible to provide libraries wrapping assertTriples tests with intention-revealing names, but I haven't found any way to format the test output in readable documentation :/ Let me know if you find soemthing!
Cheers,
y
When the door of happiness closes,http://shopcheaplv.com another opens but often times we look so long at the closed door that we don’t see the one which has been opened for us http://coachpurses4u.com
When the door of happiness closes,http://shopcheaplv.com another opens but often times we look so long at the closed door that we don’t see the one which has been opened for us http://coachpurses4u.com
When the door of happiness closes,http://shopcheaplv.com another opens but often times we look so long at the closed door that we don’t see the one which has been opened for us http://coachpurses4u.com
Burberry are more and more popular all over the world. They have unique style and the practical internal. They become people's first choice. Here show no sale tax burberry outlet , including the burberry handbags, burberry scarves and so on. Take action to get them quickly. And you can get more from our replica burberry outlet online store . http://www.burberryoutletonlineuk.c...
Mulberry Bags are more and more popular all over the world. They have unique style and the practical internal. They become people's first choice. Here show no sale tax Mulberry Outlet , including the Mulberry Alexa Bags, it is the hot item in the store. Take action to get it quickly. And you can get more from our Mulberry Factory Shop . http://www.mulberry-outlet-online.c...
Mulberry Bags are more and more popular all over the world. They have unique style and the practical internal. They become people's first choice. Here show no sale tax Mulberry Outlet
jasdnajbsweiyqwenasdasydaba
now it's your time to own the Cheap Mulberry Handbags,style yourself up wherever you go from http://www.mulberryhandbags-factory...
we can say Ferragamo Shoes owe the success to its exquisite workmanship, elegant style, generous and competitive price. Wherever and whenever you go, owning stylish Ferragamo Handbag style you up.
In order to solve the first problem, we divided our controller tests in route tests (here is a route, assert that the application forwards it to the right controller/action with the right parameters), real unit controller tests (mock the models, call an action with some request parameters, check that the right data is sent to the view), and template tests.
Ccx
http://www.montblancpenswebsite.com...
http://www.christianlouboutinsalesc...
http://www.hermesscarfsales.com/her...
http://www.montblancpenswebsite.com...
http://www.christianlouboutinsalesc...
http://www.hermesscarfsales.com/her...
http://www.montblancpenswebsite.com...
http://www.christianlouboutinsalesc...
http://www.hermesscarfsales.com/her...
http://www.montblancpenswebsite.com...
http://www.christianlouboutinsalesc...
http://www.hermesscarfsales.com/her...
Thanks.. much appreciated
WT-The Canadian goose jacket, using a excellentWT-The Canadian goose jacket, using a excellent brand name clothes, which was founded in Canada. diesel jeans within of the outfits latest market in winter.These design Canada goose vest to preserve the look at of person. http://www.diesel-jeans-uk.org/ , This diesel jeans uk can be turned off . canada goose jackets maintain system and blew cozy complete day. diesel jeans sale will boost the charm of your personality. Canada goose jackets sale is ready to keep you warm this winter. This is Canada goose expedition parka sale and apparently, They are http://www.canadagoose-jackets1.net... online. brand name clothes, which was founded in Canada. diesel jeans within of the outfits latest market in winter.These design Canada goose vest to preserve the look at of person. http://www.diesel-jeans-uk.org/ , This diesel jeans uk can be turned off . canada goose jackets maintain system and blew cozy complete day. diesel jeans sale will boost the charm of your personality. Canada goose jackets sale is ready to keep you warm this winter. This is Canada goose expedition parka sale and apparently, They are http://www.canadagoose-jackets1.net... online.
Very nice article. Please keep up the good work!!