Unit tests for HTML Output?

后端 未结 9 1179
-上瘾入骨i
-上瘾入骨i 2021-02-19 00:39

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:28

    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('<p>Hello World</p>'); 
    
    // Get explanation of what's wrong (if validation failed)
    print $validator->message; 
    
    0 讨论(0)
  • 2021-02-19 01:32

    Testing for HTML output would be considered a coverage test. Initially, when I started using PHP I was creating these tests, but over time I found that these tests weren't really all that helpful.

    If there is one thing that I know, it is that the presentation is going to change a lot from initial development to deployment.

    If you think about it, a for loop really is not logic but is a isometric transformation function, and if you follow Separation of Concerns, Then you are passing the data into the for loop via a method of some sort. I would recommend testing that the for loop gets the correct data, but not the output of the for loop.

    If you find yourself repeating yourself in generating tables then by all means start unit testing those table templates. But once again, you'll find that those templates will be seeing a lot of change.

    At this point you should be looking at separating the iteration from the HTML output to help isolate yourself from these concerns in your tests.

    One way to do this is to use a mapping function, it will take a list and transformation function and perform the function on each item in the list, then return the transformed list.

    Usually, when creating tables, I end up with two for loops in creating a row.

    1. Iterate over all rows.
    2. While in (1) iterate over items in row.

    Pretty ugly to unit test that, but with closures you can create function generators that would really be easy [this is said with a grain of salt] to implement.

    0 讨论(0)
  • 2021-02-19 01:33

    Running into this question myself. I think an approach might be to use something like phpQuery to make your tests less fragile. Instead of testing for exact output, test that there should be an h3 tag ~somewhere~ in the output. If it gets wrapped in a div later because a designer needed to tack on an extra background, or because of some ie6 float bug workaround, then your test still works.

    It's not very pure, but still potentially a very useful tool.

    0 讨论(0)
提交回复
热议问题