Selenium/PHPUnit tests always run twice, fail on second pass
The problem I am having is that I have setup runners for my Selenium/PHPUnit tests and they are always being run twice, per test, one after the other. And then it will move onto the next test. I found http://www.phpunit.de/ticket/688 that bug seems to be exactly what is happening to me -- so I removed all references to PHPUnit_MAIN_METHOD but I am still having the same problem, and it seems that "bug" was resolved two years ago anyways, and I am using the newest version of PHPUnit.
Here is my runner code:
<?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);
}
开发者_Python百科 return $suite;
}
}
?>
And my reporting class code:
<?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);
}
}
?>
And here are the constants that are being used:
<?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'
)
);
?>
Can anyone see why these tests would run twice, and ALWAYS fail on the second time through?
Example of CLI output while running tests:
CCO_TestName
www.domain.com
.www.domain.com
E
And the results file only gets updated with the results of the first test (which is normal behavior we get no logging if we get an E on the test, but I am wondering why it is running twice to begin with?
EDIT: Forgot to include my setup file:
<?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");
?>
The function
public function testContentPresent
in my reporting class was causing the issue -- Selenium automatically runs any method that starts with the word "test" hence it being run twice per actual test.
Renamed to:
public function ContentPresent
and it now works.
精彩评论