douyinghuo8874 2013-01-18 07:35
浏览 20
已采纳

如何使用像zend这样的框架测试php [关闭]

I am use to working with frameworks like zend or rails and using various testing methodologies. How ever, I cannot fathom how to test a regular, non framework based class in php that has the following methods:

class Example{
    public function add(){}
    public function edit(){}
    public function delete(){}
    public function list(){}
}

I know you need a testing or tests or test folder, that parts down. would the class inside be something like:

class ExampleTest extends PHPUnit_Framework_TestCase{}

if so what are the function? the same as the above? will it know to use the example class or do I have to require it?

when it comes to testing with out a framework like zend or something else I am lost....Your help is appreciated.

Note: I am not using any type of framework (aside from phpunit) so pointing me to zend or cakephp is not going to help. this is simply written from scratch.

  • 写回答

2条回答 默认 最新

  • dongzheng8463 2013-01-18 08:47
    关注

    I'm not sure i understand your question but i think you are asking about how to test while you are not using a framework like zf etc. Imho that's the bad part of learning a framework before learning basics. I think this is a mistake(sadly, i've done this mistake too), a lot of people trying to use a framework but they don't have enough knowledge about php yet so they made a lot of bad practices(e.g: me, now i'm trying to forget what i've learned from frameworks when i was new to php) so my suggestion is use frameworks when you are done with php basics(this part was not for your question, just a suggestion when i see "i'm good with framework but i fail without framework").

    If i come back to your question, In here yes you need(require) example class otherwise how can you create a new example object? And no, your example class and it's methods shouldn't be written in ExampleTest file, you need to write functions will test Example object's methods(are they doing what we expect?). It will fail and then write Example class and methods to pass this test (see: http://www.phpunit.de/manual/current/en/test-driven-development.html). An example for your case :

    class ExampleTest extends PHPUnit_Framework_TestCase{
       require_once('../classes/exampe.php');
    
       protected $object;
    
       protected function setUp() {
        $this->object = new Example;
        //we will use object to test it.
       }
    
       function testAddingMustReturnIntegerValue(){ //codes to check is it really integer? }
       function testAddingNegativeValuesForbidden(){ //codes to check when we try to add negatives it shouldn't perform this action }
    
       //some other thing that we are expecting from add and other methods
    
    }
    

    This one is extremly dummy example but i hope you get what i mean.

    p.s: I'm not a native speaker so sorry about my English. If i didn't understand your question and this is not helping please let me know so i can delete.

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

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持