doulan8846 2009-12-25 01:45
浏览 47


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


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

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() {
            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?



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,

    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,


    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 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息