【问题标题】:Selenium/PHPUnit tests always run twice, fail on second passSelenium/PHPUnit 测试总是运行两次,第二次通过失败
【发布时间】:2011-05-02 09:45:52
【问题描述】:

我遇到的问题是我为我的 Selenium/PHPUnit 测试设置了运行程序,并且每次测试它们总是运行两次,一个接一个。然后它将进入下一个测试。我发现http://www.phpunit.de/ticket/688 这个错误似乎正是发生在我身上的事情——所以我删除了对 PHPUnit_MAIN_METHOD 的所有引用,但我仍然遇到同样的问题,而且似乎“错误”无论如何在两年前就已经解决了,我我正在使用最新版本的 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");
?>

【问题讨论】:

    标签: unit-testing selenium phpunit


    【解决方案1】:

    功能

    public function testContentPresent
    

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

    重命名为:

    public function ContentPresent
    

    它现在可以工作了。

    【讨论】:

    • 很好的发现。您是否有机会将链接添加到有此参考的 Selenium 文档?绝对是一个很好的书签链接
    猜你喜欢
    • 2021-01-20
    • 2020-08-22
    • 2017-05-20
    • 2014-01-22
    • 1970-01-01
    • 2021-09-10
    • 2011-10-30
    • 2020-03-28
    • 2015-10-31
    相关资源
    最近更新 更多