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条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了