如何有效拆分大型 phpunit 测试套件?

How to effectively split a large phpunit testsuite?

我使用 phpunit 的测试套件功能来组织我的测试。我这样做是为了以后能够 运行 并行测试。

对于不同的目录,这是相对直接的。例如,我可以按捆绑拆分测试套件。

<testsuite name="bundle-one">
    <directory>tests/BundleOne</directory>
</testsuite>

<testsuite name="bundle-two">
    <directory>tests/BundleTwo</directory>
</testsuite>

<testsuite name="bundle-three">
    <directory>tests/BundleThree</directory>
</testsuite>

但现在我只有一个目录(服务),其中包含数十个子文件夹。我可以手动为这个文件夹创建几个测试套件。但在我看来,这将是一个薄弱的解决方案。因为如果我提到其中的每个子文件夹并且文件夹被重命名或删除,测试套件很容易损坏。

我的想法是使用某种正则表达式 select 一系列子文件夹包含在一个测试套件中,另一范围的文件夹包含在另一个测试套件中。

<testsuite name="services-AM">
    <directory>tests/services/{A-M}</directory>
</testsuite>

<testsuite name="services-NZ">
    <directory>tests/services/{A-M}</directory>
</testsuite>

我找不到关于我的想法的任何文档。有人对此有想法吗? :-)

我不确定是否有办法做到这一点 "on-the-fly" 但您可以自动执行从 reg-ex 创建这些包的过程:

在composer.json中:

 "scripts": {
    "make-phpunit-bundles": "php path/to/myscript.php"
},

在 运行 测试之前:

php composer.phar make-phpunit-bundles

myscript.php:

//Iterate/scan your directories, build and write the phpunit.xml file 
//based on the regular expressions you want

一个更简单的解决方案可能是过滤您的测试:

# Only include test classes beginning with the letter A-M
phpunit --filter '/^[A-M]/'

# Only include test classes beginning with the letter N-Z
phpunit --filter '/^[N-Z]/'

假设您的所有测试 类 都以大写字母开头。

您需要进行试验才能获得均分。也就是说,如果您所有的测试都以 A-M 之间的字符开头,那么这将不起作用。

使用 PHPUnit 5.4.6 测试