duanchandun1860
2013-02-12 08:17
浏览 64
已采纳

PHPUnit&Behat; 补充还是替代?

I have been looking around SO and Google, but I couldn't really get a definitive answer.

PHPUnit is a framework for unittesting, like JUnit. I use it, also in combination with the Selenium-extension for functional testing. When browsing around I see Behat/Mink keeps on getting mentioned. But I do not completely understand how Behat fits in here.

With Behat you write scenarios in human-readable format. Behat can then translate that into skeleton classes for a new project? But does it also provide skeleton classes for unittesting? Do you write unittests using Behat, or you would use PHPUnit / SimpleTest for those?

But then Behat/Mink does replace PHPUnit_Selenium-extension for functional testing?

Do you use Behat only for new projects, or can it also be adapted to existing projects?

图片转代码服务由CSDN问答提供 功能建议

我一直在寻找SO和Google,但我无法得到明确的答案。

PHPUnit是一个单元测试框架,就像JUnit一样。 我使用它,也结合Selenium扩展进行功能测试。 浏览时,我看到Behat / Mink不断被提及。 但是我并不完全理解Behat如何适应这里。

使用Behat,您可以用人类可读的格式编写场景。 然后,Behat可以将其转换为新项目的骨架类吗? 但它是否也为单元测试提供了骨架类? 你是否使用Behat编写单元测试,或者你会使用PHPUnit / SimpleTest?

但是Behat / Mink会替换PHPUnit_Selenium-extension来进行功能测试吗?

您是仅将Behat用于新项目,还是可以适应现有项目?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douwudie8060 2013-02-12 11:20
    已采纳

    Yes, I would consider Behat to be an alternative to phpUnit, both for general code testing and for functional testing using browser automation.

    For general testing, phpUnit is a more well-established product, while Behat is focussed on tests written using "BDD" methodology (Behaviour Driven Design). phpUnit does also have BDD features, but Behat is much more targetted toward that style.

    Since we're talking about functional browser tests rather than unit tests, you probably aren't thinking about some of the more powerful unit testing features that they have; you just want to write the browser automation. For this, you really can pick whichever of the two suits you the best.

    There are subtle edge-cases where each of them has strengths over the other, but for most cases they're both capable of running test scripts across a variety of browsers.

    My preference would go to Behat/Mink, on the grounds that it is more flexible; it can support several browser automation engines, including Selenium and Sahi, which means that if you hit an issue with one, you can switch your tests to the other and carry on. We had this exact situation, trying to get some Mink test scripts to work in Selenium; switching to Sahi made it work, and involved changing just a single line of test code.

    It's also worth adding that Mink can be used on its own without Behat, if you don't want to write BDD style tests.

    已采纳该答案
    打赏 评论
  • drktvjp713333 2013-08-10 18:17

    I would consider Behat to be complementary to PHPunit. I mean complementary by using Behat for testing behavior, PHPUnit for testing code. Using BDD/TDD, one would first write a test based on a user-story. To make the test pass, you would typically write several PHPUnit tests using TDD to create the application which will be able to pass the BDD test.

    BDD should test behavior and be unaware of the code of the application, like entities/controllers/interfaces. You should be able to swap out the complete application with another one and still pass all tests when the behavior of the application is the same.

    TDD, besides other things like your mind, should guide your structure of classes and interaction between them. Besides that, they prove your methods do what they should do, makes the intent clear and makes it possible to refactor without fear.

    打赏 评论
  • dousong1992 2013-08-13 19:55

    seems to be all wrong with this answer.

    phpunit is unit testing, and by that it adheres to TDD.

    phpspec and behat is a functional testing, so it's BDD.

    they complement each other, a perfect scenario would be a project where you have both type of testings, but this could restricted only to big projects that can afford to do that.

    TDD test single or just a couple of classes. BDD test a complete feature across multiple modules in your application.

    打赏 评论

相关推荐 更多相似问题