drgwsx8405 2011-01-12 15:13
浏览 53
已采纳

没有框架的PHP单元测试......可能吗? 可取?

When one starts searching for PHP unit tests, one usually stumbles upon:

  1. PHPUnit.
  2. SimpleTest.
  3. A plethora of blogs explaining how to use PHPUnit and SimpleTest.
  4. StackOverflow questions about PHPUnit and SimpleTest...

...and I think you get the idea.

I was wondering: How does one go about unit testing with plain ol' PHP? Is that even a wise endeavor?

I suppose I would have to build my own little framework. I'm interested because I want to have a better understanding of what's going on in my unit tests. I'm also interested because I think a lightweight, tailored solution would run my tests faster.

Bonus question: Who's the underdog? Is there a third framework worth looking into (for acedemic purposes)?

  • 写回答

4条回答 默认 最新

  • douxiajiao8445 2011-01-12 15:23
    关注

    Unit testing is basically a set of assertions.

    Consider the following PHPUnit test case:

    class MyTest extends PHPUnit_Framework_TestCase {
       public function testFoo() {
           $obj = new My;
           $this->assertEquals('bar', $obj->foo());
       }
    }
    

    You can have a similar test case without using PHPUnit:

    class MyTest {
       public function testFoo() {
           $obj = new My;
           assert("$obj->foo() == 'bar'");
       }
    }
    

    However, by doing it without a framework, you will have to manually create an instance of the test case (MyTest) and call each test method manually (MyTest::testFoo, etc.).

    The framework (e.g.: PHPUnit) is nothing more than a set of "helpers" to make it easier and faster: by automatically generating skeleton; with built-in mock objects, command lines scripts, etc.

    You can still unit test without a framework, but in the end, you'll probably save more time using one, because after all, that's generally what frameworks are for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?