2014-04-14 54 views
3

我正在写一个应该能够使用PHP 5.3+的库。我想使用生成器和封闭绑定,但这些功能是5.5 +和5.4 +。大多数lib可以不用这些功能,所以我只想在php有适当的版本时才运行某些单元测试。有没有简单的方法来做到这一点?PHPUnit - 我可以根据PHP版本运行测试吗?

我期待这样的事情:

/** @version 5.4+*/ 
public function testUsingClosureBind(){...} 

/** @version 5.5+*/ 
public function testUsingGenerators(){...} 

,但我打开任何建议...

+0

您不应该依赖您的测试执行版本。例如,在开发和集成机器上,您可以使用5.5并通过测试,但是在生产中您有5.4并且代码失败。它看起来像你有他们的测试,但如果你没有执行所有的测试它真的很糟糕。在这种情况下,不同的测试服务器使用不同的php版本,并且始终运行所有测试用例 – PolishDeveloper

回答

2

我知道这是不是PHPUnit测试组织一个最好的做法,但如果你能在不同的文件中这些方法根据所需的PHP版本,你可以使用XML配置文件中的以下内容:

<testsuites> 
    <testsuite name="My Test Suite"> 
     <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory> 
     <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file> 
    </testsuite> 
    </testsuites> 

(请参阅http://phpunit.de/manual/3.7/en/appendixes.configuration.html#appendixes.configuration.testsuites

+0

谢谢,我会检查它。 – inf3rno

+0

我试过了,但它不工作,我:php 5.3.28,phpunit 3.7.34。 – inf3rno

+1

问题是该文件不应该在同一个目录中,我只是包括在内......它只是一个包含模式,而不是一个排除......也许我应该为我想要测试的文件设置不同的后缀更高的PHP版本... – inf3rno

3

使用version_compare功能(http://us3.php.net/manual/en/function.version-compare.php)。作为一个例子:

public function testSomething() { 
    if (version_compare(PHP_VERSION, '5.0', '>=')) { 
     //do tests for PHP version 5.0 and higher 
    } else { 
     //do different tests for php lower than 5.0 
    } 
} 
+0

为什么你贴在我身上几乎与我相同,但在30分钟后? – hek2mgl

+0

恩,当我仔细看看你是对的,他们非常相似。对不起,我只是没有仔细阅读你的东西,我只是浏览它找到'version_compare()'函数。我想我可以把这个作为你的回答下的评论 – PolishDeveloper

+1

不,不,它没关系。我只是问问。正如你所说,通常评论会好的。但是,使用'version_compare()'看起来好一点。 +1 ..但是下次你知道!;) – hek2mgl

8

一到archieve这可以标注与@group你的测试取决于版本有道功能适用:

/** 
* @group 5.4 
*/ 
public function testUsingClosureBind() 

/** 
* @group 5.5 
*/ 
public function testUsingGenerators() 

现在可以执行属于测试某一组,或忽略的基团:

phpunit --group 5.5 
phpunit --group 5.4 
phpunit --exclude-group 5.5 

Documentation at PHPUnit website

+0

谢谢,这是我正在寻找。 – inf3rno

0

According to Sebastian,我应该使用@requires annotation来做到这一点。它不能用组来完成,因为我不能根据php版本自动排除它们。

Btw。它并没有帮助,因为我一直运行到解析错误5.3版本,因为使用yield::class的...

他建议的版本相关的代码移动到另一个文件,并使用此:

<testsuite name="My Test Suite"> 
    <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory> 
    <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file> 
</testsuite> 

该文件不应该是/path/to/files目录下,除非你希望它被包含...

最后我加了2个新的后缀较高有关PHP版本的测试:

<testsuite name="unit tests"> 
     <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">test/unit</directory> 
     <directory suffix="Test54.php" phpVersion="5.4.0" phpVersionOperator=">=">test/unit</directory> 
     <directory suffix="Test55.php" phpVersion="5.5.0" phpVersionOperator=">=">test/unit</directory> 
    </testsuite> 
相关问题