history
Validation, meet Unit Testing. Unit Testing, meet Validation.
by anders pearson
Tue 20 Sep 2005 23:50:12
[cross posted from the "WaSP":http://webstandards.org/buzz/archive/2005_09.html#a000577 to take comments]
Are you "test infected":http://www.extremeprogramming.org/rules/unittests.html? Do you work on dynamic sites and wish there was an automated way to run the output through the "W3C validator":http://validator.w3.org/? Do you wish it was integrated nicely with your unit testing framework?
Scott Raymond has come up with "a nice bit of code":http://scottraymond.net/articles/2005/09/20/rails-xhtml-validation to add automated validation to the unit tests for a "Ruby on Rails":http://www.rubyonrails.org/ application.
If you're not on Rails, the technique should be pretty straightforward to adapt to your prefered language/framework. Just make a <code>POST</code> request to <code>http://validator.w3.org/check</code> sending parameters <code>fragment</code> (your page, encoded) and <code>output=xml</code>. Then check the response for a header called <code>x-w3c-validator-status</code> to see if it says <code>Valid</code>. If so, your test passed.
comments
Justin Makeig - Wed 21 Sep 2005 01:34:05
Isn't this more of a job for an XML parser? What do you do with errors that come back in the HTTP response? Do you have to parse it? Here's a quick JAXP version:
bq{font-family:monospace;}. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setValidating(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setErrorHandler(new DefaultErrorHandler()); builder.parse(input);
where input can be an input stream or a file. DefaultErrorHandler sends parse errors to System.out or an external logger. With a catalog in place to map public IDs locally, this validation can be done quickly and completely offline. I've got something similar in all of my JUnit tests that need to validate XHTML.
anders - Wed 21 Sep 2005 11:05:01
if you've got a validating parser locally, sure, you're probably better off using that. the W3C validator will also validate plain HTML though (if you aren't using XHTML).
Justin - Thu 22 Sep 2005 02:14:12
Your point about HTML (as opposed to XHTML) is a good one. I forgot that others are still stuck with that. You're out of luck with an XML parser if you're trying to validate HTML.
Ashely Bowers - Wed 23 Nov 2005 03:29:07
I agree but do you invision program in the future that might do this? Using the W3C validator is real easy which is why I like it but always like to have options.
Steve - Sat 19 Nov 2005 03:54:52
Love the JAXP version, can't believe I had never thought of doing that before!
Britney - Mon 19 Dec 2005 05:39:01
Very cool, I am going to show this off in class. I am glad that it was noted it does not work the same with html, that would be an embarassing thing to show and not be able to tell!
Greg Reimer - Wed 21 Sep 2005 18:15:41
Adding to what Justin said, you can also walk the DOM once you've parsed it and check for things beyond validation, like checking document structure. For example are the first and second child elements of body div#skip and div#logo, etc.
DeepCerulean - Thu 22 Sep 2005 11:35:34
Do people ever get you confused with fossil man? I thought you were someone famous for a minute :-)
Jessica - Sat 19 Nov 2005 03:59:43
Thanks to scott for writing this code! It worked like a charm the first time we tried it. Now we will actaully do a check now and again, rather than just putting it off...