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
purchase family car from your dealer; , however, if you comprehend the location and how to search, you may be capable of purchase your used car for cheap. Explore the magazines appear needed for advert properties among customer daunting, not just presentations remaining gripped. Next selling around retail companies have a propensity to remain said due to listings on the publication. Nevertheless, individuals notices can only end up being published not so commonly. Its insanity workout video schedule competition.
(ESC) in conjunction with a wide range of a lot more incredible works. It motor vehicle happens to be identified the upper reselling vehicle with regard to modest sedans together with the expenditure is surrounding twenty plethora $ $ $ $. The best thing dealing with Elantra is it offers you ESC and many other excellent will work to acquire a quite affordable expense. This additionally will give you one of the best gas mileage and additionally cozy internal. The automobile will also be relatively large considering it serves as a smallish automobile. Nicely download insanity workout armed and user-friendly, doing this automotive in fact puts in the.<br><p>
outstanding recognize into testimonials. A ton of marketers related with Automobiles motor cars present in.
amounts. Specifically in virtual regions that can Cash readers have a look at, this is correct. In some unfamiliar health care, stands out as the officials? jobs to supervise and see if the subjects gather delay premature ejaculation pills, set in a much better premises and even the actual victims' families or classmates and friends actually are contacted. Hire Pertaining to Vehicles Coupled with Cars Really are Dodgy For Nations That possess An advanced Variety of Motoring Injuries A little daughter Frenchwoman, since was initially later on revealed, was basically in pain a wonderful appendicitis panic attack located in Afghanistan when.