仅在PHPUnit中需要时才执行dataProvider? [英] Execute dataProviders only when needed in PHPUnit?

查看:194
本文介绍了仅在PHPUnit中需要时才执行dataProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试套件只需要几分钟就可以开始.那是因为数据提供程序是在phpunit启动时立即执行的,并且所有数据(数千个对象)都保存在内存中.

My test suite takes several minutes even just to begin. That because data providers are executed all at once at phpunit's start up and all their data (several thousands of objects) is kept in memory.

是否有一种方法可以在测试运行之前调用dataProvider?我不在乎在屏幕上看到如此精确的进度.

Is there a way to call dataProviders just before the test runs, instead? I don't care of seeing such a precise progress on screen.

谢谢

推荐答案

需要一些重构,但我建议您将数据提供者与您的测试方法隔离开,而应在实际测试中对数据提供者方法进行调用

It would take some refactoring but I'd suggest decking off the data providers from your test methods and instead make method calls within your actual tests to the data provider methods.

您已经知道,从PHPUnit的手册中可以找到

As you will already be aware, from PHPUnit's manual:

所有数据提供程序都在调用setUpBeforeClass静态方法和第一次调用setUp方法之前执行.因此,您无法访问在数据提供程序中在那里创建的任何变量.为了使PHPUnit能够计算测试总数,这是必需的.

All data providers are executed before both the call to the setUpBeforeClass static method and the first call to the setUp method. Because of that you can't access any variables you create there from within a data provider. This is required in order for PHPUnit to be able to compute the total number of tests.

您应该只是将那些数据提供者推入愚蠢的类中,您的测试方法可以将它们称为即席.

You should instead just shove those data providers into dumb classes that your test methods can call ad-hoc.

所以不是

/**
 * @dataProvider additionProvider
 */
public function testAdd($a, $b, $expected)
{
    $this->assertEquals($expected, $a + $b);
}

/**
 * As you know this will be executed before any tests, as will all of them
 */
public function additionProvider()
{
    return [
        [0, 0, 0],
        [0, 1, 1],
        [1, 0, 1],
        [1, 1, 3]
    ];
}

在扩展PHPUnit\Framework\TestCase的测试基类中定义数据提供程序,将它们组织成逻辑组,并从这些逻辑类中扩展测试类.

define your data providers in test base classes that extend PHPUnit\Framework\TestCase, organise them into logical groups, and extend your test classes off those.

class SomeDataProviderClass extends TestCase
{
    public function provideSomeData()
    {
        return [
            [0, 0, 0],
            [0, 1, 1],
            [1, 0, 1],
            [1, 1, 3]
       ];
    }
}


class AnActualTest extends SomeDataProviderClass
{
    public function testThatDependsOnSomeDataBeingProvided()
    {
        $data = $this->provideSomeData()
        // Use the data and do your test
    }
}

显然,鉴于您拥有多少测试,这并不理想,但采用这种方式可以更好地进行组织,而不是到处都是@dataProvider批注(大概;因为您没有提供任何我无法告诉的代码).所有的数据提供程序方法都将按照逻辑进行组织,并且易于维护,这当然可以使测试运行更快.

Obviously it's not ideal given how many tests you have but it's better organised this way other than having a lot of @dataProvider annotations all over the place (presumably; as you've not given any code I can't tell). All data provider methods will be organised logically and easier to maintain, and of course this should make the tests run a lot faster.

唯一可能的缺点是,如果您的数据提供者从数据库等中获取东西(再次,因为您还没有发布代码,我也不知道),因为多次调用同一提供者函数将是每次数据库往返.但是,您可以轻松地缓存此类方法调用的返回值.

The only possible downside to this is if your data providers get stuff from databases, etc (again since you've not posted code I have no idea) as calling the same provider function multiple times will be a database round trip each time. You could easily cache the return value of such method calls, however.

这篇关于仅在PHPUnit中需要时才执行dataProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆