Unit tests for HTML Output?

后端 未结 9 1614
无人共我
无人共我 2021-02-19 00:49

This may be a dumb question, but do you make unit tests for the HTML output of your PHP functions/scripts?

I try to keep my HTML and my PHP separate - i.e. HTML includes

9条回答
  •  梦如初夏
    2021-02-19 01:15

    Based on my experience in testing HTML, I now follow these three basic rules:

    1. Don't test HTML output against a correct template. You will modify the outputted HTML too often, and you'll end up wasting time maintaining your tests.

    2. Check for the existence of important data in generated HTML. If you're generating HTML (as opposed to static HTML that you're written once), test the generated HTML for important data. For instance: If you're generating a table based on a two dimensional array, check that the values in the array are found somewhere in the generated HTML. Don't bother to validate the complete output, as this would violate #1.

    3. Validate if output is proper HTML. Validate all output for correct HTML in order to avoid stupid mistakes, like missing end tags. I've written a library for this, which can be used absolutely free.

    This PHP library will let you validate whether a string is valid HTML5. The library uses Validator.nu. Compatible with PHPUnit or any other testing framework.

    Download and documentation here.

    Easy to use, example:

    $validator=new HTML5Validate();
    
    // Validate (returns TRUE or FALSE)
    $result=$validator->Assert('

    Hello World

    '); // Get explanation of what's wrong (if validation failed) print $validator->message;

提交回复
热议问题