Selenium / PHPUnit测试总是运行两次,第二次传递失败

时间:2010-10-29 12:47:49

标签: unit-testing selenium phpunit

我遇到的问题是我已经为我的Selenium / PHPUnit测试设置了跑步者,并且每次测试都会一次又一次地运行两次。然后它将进入下一个测试。我发现http://www.phpunit.de/ticket/688这个bug似乎正是我发生的事情 - 所以我删除了所有对PHPUnit_MAIN_METHOD的引用,但我仍然遇到同样的问题,似乎两年前“bug”已经解决了,我正在使用最新版本的PHPUnit。

这是我的转轮代码:

<?php
require_once '/libs/prefix.php';

class WWW_TestSuite
{
public static function main()
{
    PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite(){
    global $TEST_CASES, $TEST_FILES, $DOMAINS, $SUB_DOMAINS, $LOG_FILES;
    $suite = new PHPUnit_Framework_TestSuite('PHPUnit Framework');
    $GLOBALS['testDomain'] = 'cco';
    $GLOBALS['startURL'] = 'www.domain.com';
    $GLOBALS['resultsFile'] = $LOG_FILES['www.domain.com'];
    $numberOfTests = count($TEST_CASES['cco']);
    $resultsFile = $GLOBALS['resultsFile'];
    $fh = fopen($resultsFile, 'w');
    fwrite($fh, CRLF.DELIM_PLUS.CRLF);
    fwrite($fh, $numberOfTests.NUMBER_OF_TESTS_STRING.CRLF);
    fwrite($fh, DELIM_PLUS.CRLF);
    fclose($fh);
    foreach ($TEST_CASES['cco'] as $testCase){
        include $TEST_FILES['cco'][$testCase];
        $suite->addTestSuite($testCase);
    }
    return $suite;
}
}
?>

我的报告类代码:

<?php
class reporting extends PHPUnit_Extensions_SeleniumTestCase
{
private $linkNotFound;
private $imageNotFound;
private $textNotFound;
private $testStatus;
private $successLines = array();
private $errorLines = array();

public function testContentPresent($testContent, $className){
        foreach ($testContent as $contentType=>$contentArr){
            foreach ($contentArr as $currentContent){
                try {
                    $this->assertTrue($this->$contentType($currentContent));
                    $this->successLines[] = $contentType.': '.$currentContent;
                } catch (PHPUnit_Framework_AssertionFailedError $e) {
                    $this->textNotFound = 1;
                    $this->errorLines[] = $contentType.': '.$currentContent;
                }
            }
        }
    $this->print_report($this->testURL, $className, $this->errorLines, $this->successLines);
}


public function print_report($testURL, $testName, $errorLines='', $successLines=''){
    $timeStamp = CRLF.date("m/d/y h:i:s a").CRLF;
    $resultsFile = $GLOBALS['resultsFile'];
    $fh = fopen($resultsFile, 'a');
    fwrite($fh, CRLF.DELIM_NUM);
    fwrite($fh, $timeStamp);
    fwrite($fh, "TEST NAME: ".$testName.CRLF);
    fwrite($fh, "TEST URL: ".$testURL.CRLF);
    fwrite($fh, DELIM_NUM.CRLF);
    if (!empty($successLines)) {
        fwrite($fh, DELIM_STAR.CRLF);
        fwrite($fh, TEST_PASS.CRLF);
        fwrite($fh, DELIM_STAR.CRLF);
        foreach ($successLines as $successLine){
            fwrite($fh, $successLine.CRLF);
        }
    }else {
        fwrite($fh, DELIM_STAR.CRLF);
        fwrite($fh, TEST_PASS.": NONE".CRLF);
        fwrite($fh, DELIM_STAR.CRLF);           
    }
    if (!empty($errorLines)) {
        fwrite($fh, DELIM_STAR.CRLF);
        fwrite($fh, TEST_FAIL.CRLF);
        fwrite($fh, DELIM_STAR.CRLF);
        foreach ($errorLines as $errorLine){
            fwrite($fh, $errorLine.CRLF);
        }
    }else {
        fwrite($fh, DELIM_STAR.CRLF);
        fwrite($fh, TEST_FAIL.": NONE".CRLF);
        fwrite($fh, DELIM_STAR.CRLF);
    }
    fclose($fh);
}
}
?>

以下是正在使用的常量:

<?php
// REPORT VARIABLES
define('DELIM_NUM', '#################################################################################################');
define('DELIM_STAR', '***************************************************************');
define('DELIM_PLUS', '++++++++++++++++++++++++++++++++++++++++++++');
define('NUMBER_OF_TESTS_STRING', ' TESTS HAVE RUN IN THE FOLLOWING BATCH.');
define('CRLF' ,"\n");
define('TEST_PASS', "SUCCESSES");
define('TEST_FAIL', "ERRORS");

// DIRECTORY VARS
define('CCO_TESTS_DIR', '/tests/CCO/');

// $DOMAINS - DOMAINS ARRAY
$DOMAINS = array (
'cco'   => '.domain.com'
);

// $SUB_DOMAINS - SUB DOMAINS ARRAY
$SUB_DOMAINS = array (
'www',
'dev2'
);

// $LOG_FILES - LOG FILE PATHS ARRAY
$LOG_FILES = array (
'www.domain.com'                => CCO_LOG_FILES_DIR.date("m.d.y-H.i.s").'-CCO-WWW.txt'
);

// $TEST_CASES - TEST CASE NAMES ARRAY
$TEST_CASES = array (
'cco' => array( 
    'CCO_TestName'
)
);


// $TEST_FILES - ARRAY ASSOCIATING A TEST NAME WITH THE TEST FILE.
$TEST_FILES = array (   
'cco' => array (
    'CCO_TestName'              => CCO_TESTS_DIR.'CCO_TestName.php'                     
    )
);

// $TEST_NAME_URL_RELATION - ARRAY ASSOCIATING TEST CLASSES WITH THEIR ASSOCIATED URL ROOT.
$TEST_NAME_URL_RELATION = array(
'cco'=>array(                                   
    'CCO_TestName'                              => '/testdoc.asp?querystring=123'           
)
);

?>

有谁可以看到为什么这些测试会运行两次,并且第二次总是失败?

运行测试时CLI输出的示例:

CCO_TestName
www.domain.com
.www.domain.com
E

结果文件只会根据第一次测试的结果进行更新(这是正常的行为,如果我们在测试中得到一个E,我们就没有记录,但我想知道为什么它开始运行两次?

编辑: 忘了包含我的设置文件:

<?php
global  $TEST_NAME_URL_RELATION;
$testName = get_class($this);       
$this->reporting = new reporting();
$this->errorLines = array();
$this->startURL = $GLOBALS['startURL'];
$this->setBrowser("*firefox");
$this->setBrowserUrl("http://".$this->startURL);
$this->testUrlRoot = $TEST_NAME_URL_RELATION[$GLOBALS['testDomain']][$testName];
$this->testURL = $this->startURL.$this->testUrlRoot;
print ($this->testURL."\n");
?>

1 个答案:

答案 0 :(得分:3)

功能

public function testContentPresent

在我的报告类中导致了问题 - Selenium会自动运行任何以“test”开头的方法,因此每次实际测试都会运行两次。

重命名为:

public function ContentPresent

它现在有效。