php 对网站进行单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/547302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit Testing a Website
提问by Parrots
I'm curious to see how other developers go about testing their web sites. PHP specifically in my case, but this probably spans multiple languages. I've been working on a site for over a year now, and I'd really like to automate a lot of the regression testing I do between versions.
我很想知道其他开发人员如何测试他们的网站。PHP 特别是在我的情况下,但这可能跨越多种语言。我已经在一个网站上工作了一年多了,我真的很想自动化我在版本之间所做的很多回归测试。
This specific site is in CodeIgniter, so I have some tests for my models. I'd like to move beyond just testing those though. However, this is an issue even non-MVC developers have had to tackle I'm sure.
该特定站点位于 CodeIgniter 中,因此我对我的模型进行了一些测试。不过,我想不仅仅是测试那些。但是,我敢肯定,即使是非 MVC 开发人员也必须解决这个问题。
Edit:I think the functionality that would satisfy a lot of my test desires is the ability to assert that paramters have a specific value at the end of the script processing. In my case a lot of logic is in the controller, and that's the main area I'd like to test.
编辑:我认为可以满足我的很多测试需求的功能是能够在脚本处理结束时断言参数具有特定值。就我而言,控制器中有很多逻辑,这是我想测试的主要区域。
采纳答案by ryeguy
For actual unit testing without testing the UI, you should just test the functions in the model. Most of your functionality should be in there anyways.
对于不测试 UI 的实际单元测试,您应该只测试模型中的功能。无论如何,您的大部分功能都应该在那里。
You might want to have a look at Seleniumfor testing the UI of your site. It can record your actions and play them back, or you can edit the scripting directly.

(source: seleniumhq.org)
您可能想看看Selenium来测试您网站的 UI。它可以记录您的操作并回放它们,或者您可以直接编辑脚本。
(来源:seleniumhq.org)
回答by Edison Gustavo Muenz
Have you tried Fitnesse?
你试过Fitnesse吗?
It helps on creating Acceptance tests. They are specially useful for websites, which doing this kind of tests are a pain.
它有助于创建验收测试。它们对网站特别有用,做这种测试很痛苦。
There are a couple of videos from unclebob inside the webpage too. The good thing is that Fitnesse is not restricted for website testing, so your knowledge about using it can be used with other apps too.
网页内也有一些来自 unclebob 的视频。好消息是 Fitnesse 不受网站测试的限制,因此您使用它的知识也可以用于其他应用程序。
The project I'm working on is a Desktop APP written in c++ that uses Fitnesse tests.
我正在做的项目是一个用 C++ 编写的桌面应用程序,它使用 Fitnesse 测试。
But if you meant unit testing the models (which I think you didn't), they can be create using the phpunit lib. I think the ZEND framework has a similar lib for that.
但是,如果您的意思是对模型进行单元测试(我认为您没有这样做),则可以使用 phpunit 库创建它们。我认为 ZEND 框架有一个类似的库。
回答by OverloadUT
You might want to check out PHPUnit http://www.phpunit.de/manual/current/en/
您可能想查看 PHPUnit http://www.phpunit.de/manual/current/en/
I have started using it on my PHP projects and it's very easy to work with and very powerful. In particular, learn and use mocks: http://www.phpunit.de/manual/3.0/en/mock-objects.html
我已经开始在我的 PHP 项目中使用它,它非常易于使用且非常强大。特别是学习和使用模拟:http: //www.phpunit.de/manual/3.0/en/mock-objects.html
Mocking is especially important when unit testing applications that do database operations.
当单元测试执行数据库操作的应用程序时,模拟尤其重要。
回答by Milan Babu?kov
Take a look at TOAST. It's build specially for CodeIgniter. It uses CI infrastructure, so you can run all test tests via a browser and results are displayed back as a web page (HTML). It's very simple to use.
看看TOAST。它是专门为 CodeIgniter 构建的。它使用 CI 基础设施,因此您可以通过浏览器运行所有测试测试,并将结果显示为网页 (HTML)。使用起来非常简单。
I suggest you test your Controllers as well. Testing model is ok, but model is just the DB storage. Controllers contain all the "business logic" and are the place where most things go wrong.
我建议你也测试你的控制器。测试模型没问题,但模型只是数据库存储。控制器包含所有“业务逻辑”,并且是大多数事情出错的地方。
回答by Micha? Tatarynowicz
One of the best ideas I've heard of, as far as testing web apps go, was to create a script that would go over all the pages in the site and check them for differences from the previous scan, letting you accept changes and fix regressions.
就测试 Web 应用程序而言,我听说过的最好的想法之一是创建一个脚本,该脚本将遍历站点中的所有页面并检查它们与之前扫描的差异,让您接受更改并修复回归。
Generally speaking, automatic testing of GUI applications (websites are GUI apps) is difficult and usually unnecessary. Unit tests work best with simple libraries.
一般来说,GUI 应用程序(网站是 GUI 应用程序)的自动测试很困难,而且通常是不必要的。单元测试最适合简单的库。
回答by Otávio Décio
I use Canoo WebTest. It is the best free web site unit test framework out there. It is entirely scriptable with XML and requires no browser so it can run from a build server.
我使用 Canoo WebTest。它是目前最好的免费网站单元测试框架。它完全可以使用 XML 编写脚本,并且不需要浏览器,因此它可以从构建服务器运行。
回答by Kristen
We modified Waiter (Ruby). It plays back "scripts" of URLs and Form Filling to IE and we have added a script "command" to take a Screen Capture; the screen capture image is compared against a Known-Good-Image (i.e. a Master Image) and if that image is different it is logged (basically a Web page of such results is prepared) and "a human" does a review of the Master / Test image. Obviously there are two outcomes at that point - "The difference is intentional" or "There is an incorrect change". In the first instance the Master image is replaced with the New Image; in the second we go fix the bug, and the change will be included in the next test run
我们修改了 Waiter (Ruby)。它将 URL 和表单填充的“脚本”回放到 IE,我们添加了一个脚本“命令”来进行屏幕截图;将屏幕捕获图像与已知良好图像(即主图像)进行比较,如果该图像不同,则将其记录下来(基本上准备了此类结果的网页)并且“人类”对主图像进行/ 测试图像。显然,此时有两种结果——“差异是故意的”或“有一个不正确的变化”。在第一种情况下,主图像被新图像替换;在第二个我们去修复错误,更改将包含在下一次测试运行中

