PHP 中 TDD 和/或 BDD 的状态如何?

发布于 2024-07-13 00:24:13 字数 293 浏览 6 评论 0原文

PHP 世界中测试的广泛程度、支持程度和发展程度如何? 与Java同等吗? 与 Ruby/Rails 一样吗? 我用谷歌搜索发现测试框架是存在的,但我想知道它们是否被广泛使用。

主要的 PHP IDE 是否像 Eclipse 的 Java 工具或 NetBeans 的 Ruby/Rails 工具那样具有内置测试运行程序? 测试是否像 Rails 一样内置到 PHP 的 MVC 框架中?

我问这个问题是因为我工作的一个团队想要雇人为他们开发 PHP 应用程序。 我担心质量和维护,因为我可能会被要求支持这件事。

How widespread, supported, developed is testing in the PHP world? On par with Java? Up there with Ruby/Rails? I Googled and found that testing frameworks exist but I'm wondering if they're widely used.

Do the major PHP IDE's have built-in test runners the way Eclipse's Java tools do or NetBeans's Ruby/Rails tools do? Is testing built into PHP's MVC frameworks as with Rails?

I ask because a group where I work wants to hire someone to develop a PHP app for them. I'm concerned about quality and maintenance as I might be called on to support this thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

别低头,皇冠会掉 2024-07-20 00:24:13

至少有两个成熟的、独立的、JUnit 风格的测试套件可用,名为 PHPUnitSimpleTest

就 MVC 框架而言,Symfony 有自己的测试框架,名为 lime,Code Igniter 有一个 unit_test 库和 CakePHP 依赖于前面提到的 SimpleTest。

我知道 Zend Studio 内置了对 PHPUnit 测试的支持,并且 PHPUnit 和 SimpleTest 都有命令行运行程序,因此可以集成到任何工作流程中。

如果开发人员想要利用这些工具,那么 PHP 世界中就存在这些工具,而聪明的商店也确实会利用它们。

这些警告是您对 PHP 投诉课程的标准。 有两个 PHP 社区; PHP 作为构建软件的平台,PHP 作为与 Web 服务器、Web 浏览器和数据库交互以在 Web 上生成类似应用程序的东西的方式。 它不再是非黑即白的事物,而是一个连续体。 其中,在软件开发人员方面,单元测试和 TDD 与任何其他平台一样受到支持和使用。 在“拼凑一堆我不明白的东西但仍然得到结果的人”中,这是闻所未闻的。

有很多非框架/自定义框架遗留 PHP 代码,很难获得有用的测试工具。 PHP 还很容易适合依赖浏览器环境来运行的模式。 除了我自己的观察之外,我没有任何证据支持这一点,但是许多关心测试的 PHP 商店最终依赖验收测试(即 Selenium)作为实际单元测试、测试优先等的替代品。 发展。

根据您的具体情况,采访您的团队将要雇用的开发人员。

  1. 询问他们使用什么单元测试框架

  2. 要求他们概括地描述他们开发新功能及其支持的真实世界示例测试

  3. 要求他们笼统地描述他们开发新功能时的真实世界示例测试失败以及他们如何解决该情况

您对具体情况不太感兴趣他们将描述并且更感兴趣的是他们如何轻松地讨论他们的一般代码测试知识。

There are at least two mature, stand-alone, JUnit style test suites available, named PHPUnit and SimpleTest, respectively.

As far the MVC Frameworks go, Symfony has its own testing framework named lime, Code Igniter has a unit_test library and CakePHP relies on the aforementioned SimpleTest.

I know that Zend Studio has built in support for PHPUnit tests, and both PHPUnit and SimpleTest have command-line runners so integration into any workflow is possible.

The tools are there in the PHP world if a developer wants to take advantage of them, and smart shops do take advantage of them.

The caveats are your par for the course PHP complaints. There are two PHP communities; PHP as a platform for building software, and PHP as a way to interact with a web server, web browser, and database to produce application-like things on the web. It's less a black and white thing and more a continuum; Among those more on the software developer side unit testing and TDD is supported and used as much as it is on any other platform. Among the "cobble together a bunch of stuff I don't understand but still get results people", it's unheard of.

There's a lot of non-framework/custom-framework legacy PHP code around that's difficult to get a useful test harness around. PHP also lends itself easily to patterns that rely on the existence of a browser environment to run. I don't have any evidence to back this up other than my own observations, but a lot of PHP shops that care about testing end up relying on acceptance testing (i.e. Selenium) as a substitute for actual Unit Testing, test-first, etc. development.

In your specific situation, interview the hell out of the developer your group is going to hire.

  1. Ask them what unit testing framework they use

  2. Ask them to describe, in general terms, a real world example of a time they developed a new feature and its supporting tests

  3. Ask them to describe, in general terms, a real world example of a time their tests failed and what they did to resolve the situation

You're less interested in the specific situation they're going to describe and more interested in how comfortable they are discussing their knowledge of code testing in general.

ゃ人海孤独症 2024-07-20 00:24:13

每当我使用 XUnit 风格的工具进行 TDD 项目时,我都很难把注意力集中在正确的位置上。 我发现使用为行为驱动开发设计的工具或“示例规范”使我更容易< a href="http://gojko.net/2010/12/02/the-principle-of-symmetry-change/" rel="noreferrer">正确执行 TDD——即专注于设计、暴露意图和描述特定上下文中的行为测试。

也就是说,我想在对话中介绍 pecs。 来自项目网站上的自述文件。

pecs 是一个针对 PHP 5.3 的小型行为驱动开发库,类似于 RSpec 或 JSpec。

如果您使用过 JSpec 或更好的,Jasmine-BDD(对于 JavaScript)描述行为的 pecs 风格应该真的很熟悉。 我发现这种风格非常适合组件级规格。 如果您正在寻找用于功能级别规范(故事或用户验收测试)的 PHP 工具,请考虑 Behat

回到 pecs,这里有一个从 pecs 项目站点挑选的示例:

describe("Bowling", function() {
  it("should score 0 for a gutter game", function() {
    $bowling = new Bowling();
    for ($i=0; $i < 20; $i++) {
      $bowling->hit(0);
    }
    expect($bowling->score)->to_equal(0);
  });
});

是的,这是一个 PHP 规范。 浏览 pecs 源代码,作者似乎能够利用 PHP 5.3+、Lambda 和闭包中的新热点来实现这一目标。 所以我猜这意味着你不能在任何基于 PHP 的项目中使用 pecs < 5.3(仅供参考)。

而且,pecs 并不像 PHPUnit 或 SimpleTest 那样成熟。 然而,我认为 PHP 社区中 BDD 的支持者应该支持 pecs 等工具的发展,这些工具鼓励“示例规范”或 BDD,而不会因为必须使用旧版 XUnit 测试工具而带来混乱。

这些天我更多地使用 Python 而不是 PHP。 然而,下次我选择 PHP 项目时,如果我有一个成熟的、社区支持的工具(如 pecs)来制定软件规范,我会非常高兴。

Whenever I TDD a project with XUnit style tools, I have difficulty getting my head in the right spot. I find that using tools designed for Behaviour Driven Development or "Specification by example" makes it easier for me to do TDD right -- i.e. focus on design, exposing intent and describing behaviour in specific contexts. Not testing.

That said, I would like to introduce pecs into the conversation. From the readme on the project site.

pecs is a tiny behavior-driven development library for PHP 5.3, a la RSpec or JSpec.

If you've used JSpec or better yet, Jasmine-BDD (for JavaScript) the pecs style of describing behaviour should be really familiar. I find this style great for component level specs. If you are looking for a PHP tool for feature level specifications (stories or user acceptance tests) consider Behat.

Going back to pecs, here's an example culled from the pecs project site:

describe("Bowling", function() {
  it("should score 0 for a gutter game", function() {
    $bowling = new Bowling();
    for ($i=0; $i < 20; $i++) {
      $bowling->hit(0);
    }
    expect($bowling->score)->to_equal(0);
  });
});

Yes that is a PHP spec. Looking through the pecs source, it looks like the author is able to pull this off by leveraging the new hotness in PHP 5.3+, Lambdas and closures. So I guess this means that you cannot use pecs in any project based on PHP < 5.3 (just FYI).

Also, pecs is not as mature as PHPUnit or SimpleTest. However, I think the proponents of BDD in the PHP community should support the growth of tools like pecs that encourage "Specification by example" or BDD without the confusion brought on by having to use legacy XUnit testing tools.

These days I work more in Python than PHP. However, the next time I pick up a PHP project, I'll be extremely happy if I have a mature, community supported tool like pecs to craft the specifications for the software.

呆橘 2024-07-20 00:24:13

我在 Behat / Mink http://behat.org 上获得了令人惊叹的体验,

我同意其他人将 php 作为单元测试平台不是一种乐趣或体验 如果您使用任何 php 框架,BDD 是最好的方法

将 Composer 作为 repo 构建工具进行思考是最大的绊脚石,但我们能够使用 Behat Mink Selenium Webdriver 独立服务器 jar 作为令人惊叹的设计和回归测试工具。 我们曾经在 Jenkins 服务器上针对 CakePHP 应用程序运行回归套件,但事实证明它不够“快速失败”

现在我们的工作流程如下所示:
用小黄瓜创造故事
完善故事
编写功能并删除任何新的步骤定义
开始编写 php 解决方案进行测试
然后最后我们有了一个可用的功能或错误修复,并通过 bdd 测试覆盖它。

我们使用可用的 Behat 设置设置了一个 Ubuntu VM,并将其复制到每个工作站。 我们将其纳入我们的流程中。 我们只需拉下更改运行测试,然后开始编写新的东西。

我们编写了一个 shell 脚本来自动运行 mysql 转储并在每个功能之前加载它们,这使得重构代码变得轻而易举。

Mink WebAssert 类为您提供验证行为所需的所有断言
常规会话/CommonContext 类非常适合使用 css 或 xpath。

我之前曾在 Java 和 Rails 项目中使用过 Capybara / WebDriver,发现与 Behat 相比,设置开销/学习曲线太高。

I have had an amazing experience with Behat / Mink http://behat.org

I agree with others php as a unit testing platform is not a fun or experience BDD is the best way to go if you are using any php framework

Wrapping my head around composer as a repo build tool was the biggest stumbling block but we were able to use Behat Mink Selenium Webdriver standalone server jar as an amazing design and regression testing tool. We used to run our regression suite against our CakePHP application on a Jenkins server but it proved to be not so very "fail fast" enough

Now our workflow goes like this:
Create story in gherkin
refine story
write feature and stub out any new step defs
begin coding php solution to test
Then at the end we have a working feature or bug fix with a bdd test covering it

We setup an Ubuntu VM with a working Behat setup and copied it to every workstation. We baked it into our process. We just pull down changes run tests then begin coding new stuff.

We wrote a shell script to automatically run mysql dumps and load them before each feature which has made refactoring code a breeze.

The Mink WebAssert class gives you all the assertions you need to validate behavior
The regular session / CommonContext classes are great for using css or xpath.

I have used Capybara / WebDriver with Java and Rails projects before and found the setup overhead / learning curve is too high compared to Behat.

拥抱影子 2024-07-20 00:24:13

除了 的库/框架Alan 已经提到,您可以使用 mod_perl 的 Apache::Test,我将其用作工具。 它使我能够非常简单地将测试集成到我的发布过程中。 该工具使用 TAP 输出(测试任何协议)来确定测试是否通过或失败,使用 Test::Simple 或 Test 等库::更多 (Perl 和 < a href="http://shiflett.org/code/test-more.php" rel="nofollow noreferrer">PHP)。

开箱即用的 Apache::Test 支持用 Perl 和 PHP 编写测试。 在我自己的项目中,需要一些一点技巧和很多阅读以真正使其正常工作,但是 Test::More in PHP 的实现是内置于线束中。 运行用 PHP 和 Perl 编写的所有测试都是通过一个命令完成的,并且在此过程中的任何失败都会被 Apache::Test 捕获,并尽可能地记录出了什么问题。

这一切最棒的部分是,您甚至可以将 PHPUnit 或 Simple-Test 与前两个测试框架一起使用。 通过在每个相应的库中运行测试,您可以使用 Test::More 的 PHP 实现(甚至通过测试 stdout 来使用 Perl),并吐出 TAP 供您的工具进行解释。

请务必阅读 Apache::Test 文档和mod_perl 运行 Apache::Test 指南。 此外,我在这里找到了这篇文章< /a> 很有帮助。

举个简单的例子,您可以用很少的几行代码在 Perl 中设置一个测试,该测试将运行您网站上的所有页面(有链接)并验证所有结果是否为“200 OK”响应并且没有任何解析错误:

#!perl

use strict;
use warnings;

use Apache::Test qw(:withtestmore);
use Apache::TestRequest;
use Test::More;
use Test::WWW::Mechanize;
use WWW::CheckSite::Validator;
use WWW::CheckSite::Spider;

plan 'no_plan';

my $config = Apache::Test::config();
my $host = "http://". Apache::TestRequest::hostport($config) || '';

my $s = WWW::CheckSite::Spider->new(
    uri => $host,
    ua_class => 'Test::WWW::Mechanize',
);
my $m = $s->current_agent;

while (my $page = $s->get_page) {
    is($m->status(), "200", $m->uri() ." retrieved successfully.");
    $m->content_lacks("Parse Error", $m->uri() ." does not contain syntax errors.");
}

In addition to the libraries/frameworks that Alan has already mentioned, you can make use of mod_perl's Apache::Test, which I what I use as a harness. It allows me to very simply integrate tests into my release process. The harness uses TAP output (Test Anything Protocol) to determine whether or not the tests pass or fail, using libraries like Test::Simple or Test::More (Perl and PHP).

Out of the box Apache::Test supports writing tests in both Perl and PHP. In my own projects, it took a little bit of trickery and a lot of reading to really get it working, but an implementation of Test::More in PHP is built-in to the harness. Running all tests written in both PHP and Perl is done through a single command and any failure along the way is captured Apache::Test, noting as best it can what went wrong.

The awesome part about all this is that you can even utilize PHPUnit, or Simple-Test alongside the previous two testing frameworks. By running tests in each respective library, you can use the PHP implementation of Test::More (or even Perl by testing stdout) and spit back out TAP for your harness to interpret.

Be sure to read the Apache::Test documentation and the mod_perl guide to running Apache::Test. Additionally, I found the article here a great help.

As a quick example, you could setup a test in Perl in very few lines of code that will run through all the pages on your site (that have links) and verify all result in '200 OK' responses and don't have any parsing errors:

#!perl

use strict;
use warnings;

use Apache::Test qw(:withtestmore);
use Apache::TestRequest;
use Test::More;
use Test::WWW::Mechanize;
use WWW::CheckSite::Validator;
use WWW::CheckSite::Spider;

plan 'no_plan';

my $config = Apache::Test::config();
my $host = "http://". Apache::TestRequest::hostport($config) || '';

my $s = WWW::CheckSite::Spider->new(
    uri => $host,
    ua_class => 'Test::WWW::Mechanize',
);
my $m = $s->current_agent;

while (my $page = $s->get_page) {
    is($m->status(), "200", $m->uri() ." retrieved successfully.");
    $m->content_lacks("Parse Error", $m->uri() ." does not contain syntax errors.");
}
各自安好 2024-07-20 00:24:13

在过去的一个项目中,我使用过 PHPUnit,但它让我感到不满意。
PHPUnit + 命令行运行测试,
这样做使得花费太多时间编写测试代码,速度不够快,并且确实似乎以我不喜欢的方式限制了代码的风格(对象更容易测试,所以它似乎有点青睐对象)。

Selenium 是我们讨论过但从未投入使用的解决方案,我认为我们确实会从这种输出级测试中受益。

在这个最新的项目中,随着我们一直在修改软件,首席程序员采用了更实用的编程方法。 当我提到我想通过 TDD 进行编码时,他在一天或更短的时间内就制定了一个自定义解决方案,我认为该解决方案对我来说与 PHPUnit 一样有效。 此外,他真的让我对面向对象与函数式编程的问题大开眼界。

第一个项目,从头开始,在底层,面向对象的编码,大型单元测试框架,它变得单一并很快陷入困境。 第二个项目是成熟的 CMS 软件,具有 5 年的历史和旧代码,但函数式编程范例和简单的测试框架(我们实际上经常使用 php 断言)使其变得更简单,而不是变得复杂。

第二个项目也从未达到实现 Selenium 的程度(我仍然认为这会很有用),但函数式编程方法使得处理代码内测试变得更容易。

On a past project, I've used the PHPUnit, and it has left me wanting.
PHPUnit + Command line running of the tests,
made it so that too much time was spent coding the tests, wasn't fast enough, and really seemed to constrain the style of the code in a way that I didn't like (Objects were easier to test, so it seemed to kinda favor objects).

Selenium was a solution that we talked about but never got around to getting into play, and I think we would really have benefited from that kind of output-level testing.

On this latest project, the lead programmer has taken a more functional programming approach as we've been revising software. When I mentioned that I'd like to code via TDD, he whipped up a custom solution in a day or less that I consider to have been as effective for me to use as PHPUnit. In addition, he really opened my eyes about the question of Object Oriented vs. Functional Programming.

First project, started from scratch, in at the ground floor, Object Oriented coding, large Unit Testing Framework, it became monolithic and bogged down quickly. Second project, well established CMS software with a 5 year history and old code, yet a functional programming paradigm and a simple testing framework (we actually often made use of php assert) made it get simpler instead of grow in complexity.

The second project, too, never got to the point of implementing Selenium (and I still think it would be beneficial), but the functional programming approach made it easier to deal with in-code testing.

你与清晨阳光 2024-07-20 00:24:13

我刚刚发现这个问题,而我仍处于“研究阶段”,想知道发生了什么。 我刚刚发现了 Ruby on Rails 的一个名为“Cucumber”的东西 http://cukes.info/

它本质上是“故事驱动” Ruby 的“开发”,并且很可能是功能测试领域的黄金标准,至少就我在我的旅行中所看到的而言是这样。 (我把它公开地放在那里,所以如果我错了,专家可以纠正我)

作为 Cucumber 语言的一个例子,你得到了一些非常类似于 SQL 的东西。 但是似乎更具人类可读性。 从 cukes 的首页来看,它们的语言如下所示:

 Scenario: Add two numbers
      Given I have entered 50 in the calculator
      And I have entered 70 in the calculator
      When I press add
      Then the result should be 120 on the screen

上面的代码将作为测试进行编译和运行。

现在,这就是回答您有关 PHP - BDD 和 PHP 的问题的全部序言。 时分驱动。

与上述评论相呼应,PHPUnit 将允许单元测试,并根据此博客文章: http://sebastian-bergmann.de/archives/738-Support-for-BDD-and-Stories-in-PHPUnit-3.3.html 还支持“story风格”BDD 测试。

为了扩展上述关于“SIMPLETEST”的答案,ST 系统有一个用于浏览器自动化的内置浏览器对象类,而 PHPUnit 有一个用于 SELENIUM 浏览器自动化的扩展 http://seleniumhq.com (Selenium 与 SimpleTest 的优点是 Selinium 将运行任何页面上的 javascript,而 SimpleTest 则不会)。

我希望这些信息对您有所帮助,因为它是对上述技术进行数月个人研究和实际试验和错误的结果。 如果有专家可以澄清并提高我对上述内容的理解,我欢迎反馈。

  • 亚历克斯.

I just found this question and, while I'm still at the "researching stage" in figuring out what's going on. I just discovered something for Ruby on Rails called "Cucumber" http://cukes.info/

It is essentially 'story driven development' for Ruby and quite possibly a gold standard in the realm of functional testing, at least as far as I've seen in my travels. (I put this up there publicly, so experts can correct me if I'm wrong)

As an example of the language in Cucumber, you've got something that very closely resembles SQL. BUT seems to be even more human readable. From the cukes front page their language looks like this:

 Scenario: Add two numbers
      Given I have entered 50 in the calculator
      And I have entered 70 in the calculator
      When I press add
      Then the result should be 120 on the screen

The above will compile and run as a test.

Now that's all preamble to the point of answering your question about PHP - BDD & TDD.

In echoing the comments above, PHPUnit will allow unit testing and according to this blog post: http://sebastian-bergmann.de/archives/738-Support-for-BDD-and-Stories-in-PHPUnit-3.3.html also supports "story style" BDD testing.

To expand on the above answer with respect to "SIMPLETEST" mentioned above, the ST system has a built in browser object class for browser automation, while PHPUnit has an extension for the SELENIUM browser automation http://seleniumhq.com (the advantage of Selenium vs. SimpleTest is that Selinium will run any on-page javascript while SimpleTest will not).

I hope you find this information helpful as it's the result of a number of months personal research and hands-on trial and error with the above technologies. If there are experts out there who can clarify and improve my understanding of the above, I welcome the feedback.

  • Alex.
无法回应 2024-07-20 00:24:13

Michael Booth 对两种语言的 BDD 测试功能的比较:

http://mechanicalrobotfish.com/posts/117-ruby-vs-php-bdd-beauty-contest-no-contest

得出的结论是,PHP BDD 工具和文化目前尚不发达。

当然,无论是在知识(书籍、视频、文章、博客文章)还是工具(Rspec、Shoulda、Factory Girl、Mocha、Cucumber)方面,Ruby 程序员都无法比拟。

Michael Booth's comparison of BDD testing features in both languages:

http://mechanicalrobotfish.com/posts/117-ruby-vs-php-bdd-beauty-contest-no-contest

concludes that PHP BDD tools and culture is underdeveloped at this point.

Certainly there is nothing comparable with what's available to a Ruby programmer, either in terms of knowledge (books, videos, articles, blog posts) or tools (Rspec, Shoulda, Factory Girl, Mocha, Cucumber).

时光病人 2024-07-20 00:24:13

您可能想查看 PHPStorm。 我喜欢在 IDE 中使用 PHPUnit 的测试运行程序。

You might want to check out PHPStorm. I like the test runners that use PHPUnit from within the IDE.

一瞬间的火花 2024-07-20 00:24:13

现在我正在开发用于 BDD 测试的“Spectrum”框架: https://github.com/m-haritonov/频谱

Now I'm developing "Spectrum" framework for BDD test: https://github.com/m-haritonov/spectrum

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文