Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests for HTML Output?

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 with placeholders, and functions for certain recurring elements (tabular data / any sort of looped output) - but I'm not sure how to go about verifying this.

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?

Thanks.

Edit: I guess a corollary to this would be: are these sorts of unit tests even worth having? If you're keeping your content and structure properly separated, then you would really only be testing a handful of includes in very limited scenarios (presumably, anyway). Is it really worth it to semi-hand-craft full pages just to have a file to compare to?

like image 837
Kristina Avatar asked Aug 07 '10 05:08

Kristina


People also ask

What is a unit test in HTML?

Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers.

How do I test HTML?

If you want to work with Chrome Developer Tools, simply run the HTML document in Google Chrome and right-click the HTML element you want to inspect. Click on "Inspect" and you will have the tools to run, analyze, and even debug the code.

What should I unit test in frontend?

Unit testing It analyzes individual components and functions to ensure they're working as expected. This is crucial for any frontend application, testing your components and features against how you expect them to behave in production, leading to a stable codebase and a reliable app for your customers.

Is it possible to unit test Web API?

You can either create a unit test project when creating your application or add a unit test project to an existing application. This tutorial shows both methods for creating a unit test project. To follow this tutorial, you can use either approach.


2 Answers

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; 
like image 192
Per Kristian Avatar answered Sep 21 '22 08:09

Per Kristian


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.

like image 43
Gutzofter Avatar answered Sep 21 '22 08:09

Gutzofter