HTML输出的单元测试? [英] Unit tests for HTML Output?

查看:335
本文介绍了HTML输出的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是您是否对PHP函数/脚本的HTML输出进行单元测试?



我试图保留我的HTML和我的PHP是独立的 - 即HTML包含占位符,以及某些循环元素的功能(表格数据/任何类型的循环输出) - 但我不知道如何去验证这个。



有没有一种标准的方法去处理这些事情,或者主要是对创建插入的内容的函数使用常规的单元测试,然后确保它在浏览器/ W3C验证器?



谢谢。

编辑:我想这是一个必然结果:是这些各种单元测试甚至值得拥有?如果你保持你的内容和结构正确分离,那么你真的只会在非常有限的情况下(大概无论如何)测试少数内容。是否真的值得半手工制作整个页面,以便有一个文件进行比较? 基于我在测试HTML中的经验,我现在遵循以下三条基本规则:

1。不要使用正确的模板来测试HTML输出。
您将经常修改输出的HTML,最终浪费时间维护您的测试。



2。检查生成的HTML中是否存在重要数据。
如果您生成HTML(而不是您曾编写过的静态HTML),请为生成的HTML测试重要数据。例如:如果您基于二维数组生成表,请检查数组中的值是否在生成的HTML中找到。不要打扰验证完整的输出,因为这会违反#1。


3。验证输出是否为正确的HTML。
验证正确HTML的所有输出以避免错误,比如缺少结束标记。我为此写了一个库,它可以完全免费使用。



这个PHP库可以让你验证一个字符串是否是有效的HTML5。该库使用Validator.nu。兼容PHPUnit或任何其他测试框架。



下载以及文档。



易于使用,例如:

  $ validator = new HTML5Validate(); 
$ b $ // Validate(返回TRUE或FALSE)
$ result = $ validator-> Assert('< p> Hello World< / p>');

//获取有关错误的解释(如果验证失败)
打印$ validator->消息;


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?

解决方案

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; 

这篇关于HTML输出的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆