Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 只运行一次安装方法_Php_Laravel_Phpunit - Fatal编程技术网

Php 只运行一次安装方法

Php 只运行一次安装方法,php,laravel,phpunit,Php,Laravel,Phpunit,我有: 1. IntegrationTestCase extends TestCase 2. UnitTestCase extends TestCase 3. AcceptanceTestCase extends TestCase 在这些测试中,我有很多非静态的方法,它们被用于很多测试中。我所有的测试类都扩展了这3个类中的一个 现在在很多测试类中,我有一个设置方法,它准备所需的数据和服务,并将它们分配给类变量: class SomeTestClass extends Integrati

我有:

1. IntegrationTestCase extends TestCase  
2. UnitTestCase extends TestCase
3. AcceptanceTestCase extends TestCase  
在这些测试中,我有很多非静态的方法,它们被用于很多测试中。我所有的测试类都扩展了这3个类中的一个

现在在很多测试类中,我有一个
设置
方法,它准备所需的数据和服务,并将它们分配给类变量:

class SomeTestClass extends IntegrationTestCase
{
    private $foo;

    public function setUp()
    {
        parent::setUp();

        $bar = $this->createBar(...);
        $this->foo = new Foo($bar);
    }

    public function testA() { $this->foo...; }  
    public function testB() { $this->foo...; }
}
问题是,
setUp
是为每个测试运行的,而我想做的是失败的,如果
setUp
方法所做的事情需要很长时间,那么它将乘以测试方法的数量


使用
公共函数uu构造(…){parent::u构造(…);…}
会产生问题,因为现在Laravel的低级方法和类不可用

除了Mark Baker提到的问题外,我不确定您看到的关于
setUpBeforeClass
的问题是静态的。不过,我想你知道自己在做什么。下面是一个可能的用法示例

class BeforeAllTest extends PHPUnit_Framework_TestCase
{
    private static $staticService;
    private $service; // just to use $this is tests

    public static function setUpBeforeClass() {
        self::createService();
    }

    public static function createService(){
        self::$staticService = 'some service';
    }

    /**
     * just to use $this is tests
     */
    public function setUp(){
        $this->service = self::$staticService;
    }

    public function testService(){
        $this->assertSame('some service', $this->service);
    }
}
更新:您可以在上看到类似的方法(搜索“提示:使用您自己的抽象数据库测试用例”)。我确信您已经在使用它,因为您正在进行密集的db测试。但没有人仅针对db问题限制这种方式


更新2:嗯,我想你必须使用smth,比如
self::createService
,而不是
$this->createService
(我已经更新了上面的代码)。

对于下一个遇到这个问题的人:

我的问题是,我想在运行测试之前迁移数据库,但我不想在每次测试之后迁移数据库,因为执行时间太长

我的解决方案是使用静态属性检查数据库是否已迁移:

class SolutionTest extends TestCase
{
    protected static $wasSetup = false;

    protected function setUp()
    {
        parent::setUp();

        if ( ! static::$wasSetup) {
            $this->artisan('doctrine:schema:drop', [
                '--force' => true
            ]);

            $this->artisan('doctrine:schema:create');

            static::$wasSetup = true;
        }
    }
}

萨曼·侯赛尼(Saman Hosseini)和类似的解决方案对我来说并不合适。使用static属性为下一个测试类标记获取重置

为了克服这个问题,我编写了单独的测试类来测试测试数据库连接&初始化测试数据库一次&确保它在所有其他测试之前运行


在setUp方法中添加一些检查setUp()的整个要点是应该为每个单元测试运行它;因为每一个单元测试都应该与任何其他单元测试完全隔离运行,您看到了吗?但还是想想马克·贝克的评论。@xmike对我没有帮助,因为它是一个静态方法;您应该模拟数据库OK,但我需要调用
self::$staticService=$this->createService(…)
并没有真正帮助我。我在这里添加了一个类似的示例: