1

How do you make unit tests for the HTML output of your PHP functions/scripts, specifically to check that the output is HTML5 valid?

Currently a can test functionality in PHPUnit and presentation with online copy/paste validators. But it would be much nicer if this could be integrated into the PHPUnit testing.

Is there a standard way to go about such things, or is it mainly a matter of using regular unit tests on functions which create the inserted content, and then making sure it looks correct in the browser/W3C Validator?

Similar question for older version of PHPUnit that no longer applies: Unit tests for HTML Output?

William Entriken
  • 37,208
  • 23
  • 149
  • 195

2 Answers2

0

What you looking for is behavior testing. Take a look at Behat

common sense
  • 3,775
  • 6
  • 22
  • 31
0

The Twine project (http://twineproject.sourceforge.net/doc/phphtml.html) replaces the copy/paste manual process. It might be useful; it still sends the HTML to the w3C site each time, which is not ideal for unit tests. (The W3C says all their stuff is open source and so you might be able to download it and run it locally... I couldn't find the download link though!)

An alternative approach is to use DomDocument::validate() However it requires the DTD to be referenced inside, and as this answer https://stackoverflow.com/a/15245834/841830 explains, HTML5 has no DTD.

(I'm assuming you meant that you have functions that return HTML5 strings, and you want to unit test those functions: if you want to test the whole output of a web app, e.g. as run through Apache and seen in a browser, I would use CasperJS or Selenium. But this is high-level functional testing, notably slower to run than unit tests, so I recommend to unit test what can be unit tested: and I still cannot find an offline HTML5 validator for Casper/Phantom/Slimer nor for Selenium!)

Community
  • 1
  • 1
Darren Cook
  • 27,837
  • 13
  • 117
  • 217