热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在PHP中对HTML进行稳健的单元测试-Robustunit-testingofHTMLinPHP

Imaddingunit-teststoanolderPHPcodebaseatwork.Iwillbetestingandthenrewritingaloto

I'm adding unit-tests to an older PHP codebase at work. I will be testing and then rewriting a lot of HTML generation code and currently I'm just testing if the generated strings are identical to the expected string, like so: (using PHPUnit)

我正在将单元测试添加到旧的PHP代码库中。我将测试然后重写大量的HTML生成代码,目前我只测试生成的字符串是否与预期的字符串相同,如下所示:(使用PHPUnit)

public function testConntype_select() {
    $this->assertEquals(
        '',
        conntype_select(1); // A value from the test dataset.
    );
}

This way has the downside that attribute ordering, whitespace and a lot of other irrelevant details are tested as well. I'm wondering if there are any better ways to do this. For example if there are any good and easy ways to compare the generated DOM trees. I found very similar questions for ruby, but couldn't find anything for PHP.

这种方式的缺点是属性排序,空格和许多其他不相关的细节也被测试。我想知道是否有更好的方法来做到这一点。例如,如果有任何好的和简单的方法来比较生成的DOM树。我发现ruby非常相似的问题,但找不到PHP的任何内容。

2 个解决方案

#1


1  

Look at Zend_Test_PHPUnit. Here you may query the DOM using:

看看Zend_Test_PHPUnit。在这里您可以使用以下方式查询DOM:

assertQuery() or assertXpath();

assertQuery()或assertXpath();

#2


0  

I'm dealing with the same issues. LOL! What I think I'm going to do is use the DOMDocument at some point. But for now all I'm doing is writing coverage tests, which is what your doing. Here is one of my tests. The same as yours:

我正在处理同样的问题。大声笑!我认为我要做的是在某些时候使用DOMDocument。但就目前而言,我所做的只是编写覆盖测试,这就是你所做的。这是我的一个测试。和你的一样:

public function testUpdateSkuTable() {
    $formName = "sku_id";
    $key = $formName;
    $sku = array('sku_id' => 'sku id', 'description' => 'generic description');

    $expected = "
SKU Edit Information For:
sku_id
SKU Data Entry
sku_id:
description:
"; $actual = $this->view->editorUpdateSku($formName, $sku, $key); $this->assertEquals($expected, $actual); }

推荐阅读
author-avatar
她丶无可取代
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有