如何使用浏览器运行PHPUnit测试?

时间:2015-09-25 00:16:29

标签: symfony phpunit

我是PHPUnit的新手,我需要使用浏览器运行测试并将结果保存为xml格式。 但每当我运行此代码时,我都会遇到以下错误:

FatalErrorException:错误:界面' PHPUnit_Framework_Test'在/usr/share/php/PHPUnit/Framework/TestSuite.php第83行中找不到

我的代码如下所示:

 require_once '/var/www/Drillsight/app/autoload.php';
 require_once "PHPUnit/Framework/TestSuite.php";
 require_once "PHPUnit/TextUI/TestRunner.php";

 $run_command = new PHPUnit_TextUI_Command;
 $run_command ->run(array('phpunit', '--log-junit', 'results.xml', 'MyPHPUnitTest.php'),true);

有人可以帮忙解决这个问题吗? 任何其他推荐选项将受到高度赞赏!

提前致谢!

1 个答案:

答案 0 :(得分:0)

真的需要在浏览器中运行吗?

我个人使用包含我所有单元测试的.bat文件

REM GEM_MECHANIC_TESTS

ECHO.
ECHO FILE : GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GENERAL_CONTROLER/TEST/GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.


REM GEM_MECHANIC_INTERFACE_BUILDER_TESTS
ECHO.
ECHO FILE : HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GENERAL_CONTROLER/TEST/TEST_INTERFACE_BUILDER/HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.

在我的Xampp控制台中运行这样的命令:

C:\xampp\htdocs\PC_administration_interface\Controler/script/temp_unit_test.bat >C:\xampp\htdocs\PC_administration_interface\Controler/test_result.txt

或者如果您想每晚使用Windows任务自动执行此操作:

Launch_xampp.bat

@echo off
tasklist | find /i "xampp-control.exe" && goto :eof

start /b "xampp-control.exe" "C:\xampp\xampp-control.exe"

launch_automatic_test.bat

@echo off

call %~DP0\launch_xampp.bat

call %~DP0\..\..\..\..\xampp_shell.bat

cls

call %~DP0\run_unit_test.bat > "%~DP0\..\test_result.txt"

这个方法不像我在文本文件中导出结果那么重要,而是使用PHP来解析输出(你可以稍微更改代码来生成XML)。

public static function fetchTestResult(&$errorMessage){

$echoString = '';
$failure = false;
$fileContent = '';
$errorMessage = '';

$dbManager = GeneralDbManager::getInstance();

if (file_exists(realpath(dirname(__FILE__)) . '/test_result.txt')) {

    @$fileContent = file(realpath(dirname(__FILE__)) . '/test_result.txt');

    if ($fileContent === false){

        $errorMessage = $dbManager->getErrorMessage('UNIT_TEST_RESULT_LOG_ERR', "An error happened while reading the unit tests results log."); 
    }
    else{

        unlink(realpath(dirname(__FILE__)) . '/test_result.txt');

        if (file_exists(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat')) {

            unlink(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat');
        }

        $echoString = HtmlTagBuilder::createCustomTextArea("TA_UNIT_TEST_RESULT", TextAlign::ALIGN_LEFT, false, 1100, 500);

        foreach ($fileContent as $line){

            if (StringManager::stringStartWith($line, "FILE :")){

                $failure = false;

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::TITLE_LINE);
            }
            elseif (StringManager::stringStartWith($line, "time:")){

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::WARNING_LINE);
            }
            elseif (StringManager::stringStartWith($line, "OK (")){

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::SUCCESS_LINE);
            }
            elseif ((StringManager::stringStartWith($line, "There ") and strpos($line, "failure") !== false)
                or $failure === true){

                $failure = true;

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
            }
            elseif (strpos(strtolower($line), "failure") !== false){

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
            }
            else{

                $echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::REGULAR_LINE);  
            }
        }

        $echoString .= '</DIV><br><br>';
    }

}
else{
    $errorMessage = $dbManager->getErrorMessage('UNIT_TEST_NO_RESULT_LOG_ERR', "You must run the unit test and generate the test log before displaying it.");
}

return $echoString;
}