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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)