2011-03-15 60 views
3

我想使用setUpBeforeClass()来建立一个数据库连接,并做一些日志记录,但它没有被调用之前我的测试执行(或根本就没有)。 我有以下几点:PHPUnit和Selenium:setUpBeforeClass()不叫

class TestSetup extends PHPUnit_Extensions_SeleniumTestCase { 
    public static function setUpBeforeClass() { 
     //do some setup stuff here for all my tests 
    } 
    protected function setUp() { 
     $this->setBrowserUrl('http://' . $this->deviceIp); 
    } 
    protected function testOne() { 
     //do a test here 
    } 
    protected function testTwo() { 
     //do a test here 
    } 
} 

我做了一些挖成的PHPUnit /框架/ TestSuite.php,并已证实,线660 $这个 - >测试用例是布尔(假)。但我无法弄清楚它应该是真的还是应该发生的地方(除了在__construct()中)。

我略微超过了我的头,所以任何帮助将不胜感激。

让我知道我是否可以提供任何其他有用的信息。

Josh

回答

3

我在文档中找不到任何东西,但代码似乎与您一致。

PHPUnit/Extensions/SeleniumTestCase.php运行方法(第289+行)没有调用setUpBeforeClass(或任何其他方法可能会这样做)的迹象。

如果你认为这个问题我建议在phpunits issue tracker上打开一张票。

对于您可以在setUp使用一个静态属性是这样一个解决方法:

protected function setUp() { 
    static $db = null; 
    if($db === null) { 
     $db = whatevery_you_do_there(); 
    } 
    $this->db = $db; 
} 

应该工作,排序的,因为如果你想运行在setUpBeforeClass()

+0

谢谢你,双重的。首先,为了确认我没有错过什么:)。其次,一个非常聪明的解决方案,完全符合我的需求。 – 2011-03-16 21:29:11

+0

这是错误:https://github.com/sebastianbergmann/phpunit-selenium/issues/69 – 2012-05-11 18:14:44