Unit tests for HTML Output?

后端 未结 9 1183
-上瘾入骨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: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.

提交回复
热议问题