doulan8846 2009-12-25 01:45
浏览 47
已采纳

用netbeans和PHPUnit测试php函数(不是类)

I'd like to run unit test for a functions library file...

that is, I don't have a class, it's just a file with helper functions in it...

for example, I've created a php project at ~/www/test

and a file ~/www/test/lib/format.php

<?php

function toUpper( $text ) {
  return strtoupper( $text );
}

function toLower( $text ) {
  return strtolower( $text );
}

function toProper( $text ) {
  return toUpper( substr( $text, 0, 1 ) ) .  toLower( substr( $text, 1) );
}
?>

tools -> create PHPUnit tests gives me the following error:

PHPUnit 3.4.5 by Sebastian Bergmann.

Could not find class "format" in "/home/sas/www/test/lib/format.php".

now, if I code (by hand!) the file ~/www/test/tests/lib/FormatTest.php

<?php
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../../lib/format.php';

class FormatTest extends PHPUnit_Framework_TestCase {

  protected function setUp() {}

  protected function tearDown() {}

  public function testToProper() {
    $this->assertEquals(
            'Sebastian',
            toProper( 'sebastian' )
    );
  }
}
?>

it works fine, I can run it...

but if I select test file from format.php i get

Test file for the selected source file was not found

any idea?

saludos

sas

ps: another question, is there a way to update generated tests without having to manually delete them???

ps2: using netbeans 2.8 dev

  • 写回答

1条回答 默认 最新

  • donglu3243 2010-01-22 10:48
    关注

    How you've written your Unit test case is 100% correct. The problem lies with common convention and how PHPUnit and Netbeans relies on them.

    Best practice these days is to write all your code in an object orientated fashion. So instead of having a PHP file full of utility functions like you have, you wrap these functions into a class and have them as static functions. Here's an example using your code above,

    <?php
    
    class Format
    {
        public static function toUpper($text)
        {
            return strtoupper($text);
        }
    
        public static function toLower($text)
        {
            return strtolower($text);
        }
    
        public static function toProper($text)
        {
            return self::toUpper(substr($text, 0, 1 )) .  self::toLower(substr($text, 1));
        }
    }
    

    You'd now use your functions like so,

    Format::toProper('something');
    

    PHPUnit, and Netbeans, depend on this object orientated philosophy. When you try to automatically generate a PHPUnit test case, PHPUnit looks in your file for a class. It then creates a test case based on this class and it's public API, and calls it ClassNameTest, where ClassName is the name of the class being tested.

    Neatbeans also follows this convention and knows that ClassNameTest is a PHPUnit test case for ClassName, so creates a link between the two in the IDE.

    So, my advice is to always use classes where you can. If you have utility functions that don't depend on anything and are used globally, make them static ones.

    Side note: I'd get rid of your two functions toUpper() and toLower(). There's no need to wrap built in PHP functions when there's no need to. There's also no need to test them as they are thoroughly tested.

    Site note 2: There's kind of a built in PHP equivalent to your function toProper() called ucfirst().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值