【问题标题】:Test Dependencies between PHP unit tests in Laravel 5.7在 Laravel 5.7 中测试 PHP 单元测试之间的依赖关系
【发布时间】:2019-05-14 20:38:46
【问题描述】:

我正在处理Laravel 5.7,我想问你一个关于 PHPUnit 测试的问题。

我有一个测试类,比如说ProductControllerTest.php,有两个方法testProductSoftDelete()testProductPermanentlyDelete()。我想在 testProductPermanentlyDelete() 中使用注释 @depends 以便首先软删除产品,然后获取产品 ID 并继续进行永久删除测试。这里的问题是DatabaseTransaction trait 在每个测试(方法)执行中运行事务。我需要在我的 ProductControllerTest 类的所有测试之前启动事务,然后在所有测试结束时回滚事务。你有什么想法?从我从网上搜索的内容来看,没有任何工作正常。

public function testProductSoftDelete()
{
   some code

   return $product_id;
}

/**
* @depends testProductSoftDelete
*/
public function testProductPermanentlyDelete($product_id)
{
   code to test permanently deletion of the product with id $product_id.
   There is a business logic behind that needs to soft delete first a 
   product before you permanently delete it.

}

以下内容有意义吗?

    namespace Tests\App\Controllers\Product;

    use Tests\DatabaseTestCase;
    use Tests\TestRequestsTrait;

    /**
     * @group Coverage
     * @group App.Controllers
     * @group App.Controllers.Product
     *
     * Class ProductControllerTest
     *
     * @package Tests\App\Controllers\Product
     */

    class ProductControllerTest extends DatabaseTestCase
    {
        use TestRequestsTrait;

        public function testSoftDelete()
        {
            $response = $this->doProductSoftDelete('9171448');
            $response
                ->assertStatus(200)
                ->assertSeeText('Product sof-deleted successfully');
        }


        public function testUnlink()
        {
            $this->doProductSoftDelete('9171448');
            $response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/unlink/9171448');

            $response
                ->assertStatus(200)
                ->assertSeeText('Product unlinked successfully');
        }
    }


    namespace Tests;

    trait TestRequestsTrait
    {
        /**
         * Returns the response
         *
         * @param $product_id
         * @return \Illuminate\Foundation\Testing\TestResponse
         */
        protected function doProductSoftDelete($product_id)
        {
            $response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/soft-delete/'.$product_id);
            return $response;
        }

    }

    namespace Tests;

    use Illuminate\Foundation\Testing\DatabaseTransactions;

    abstract class DatabaseTestCase extends TestCase
    {

        use CreatesApplication;
        use DatabaseTransactions;
    }

【问题讨论】:

    标签: php laravel phpunit


    【解决方案1】:

    创建一个单独的函数来两次执行相同的行为:

    public function testProductSoftDelete()
    {
        doSoftDelete();
    }
    
    public function testProductPermanentlyDelete()
    {
        doSoftDelete();
        doPermanentDelete();
    }
    

    您的案例不是测试依赖的案例,但您真正想要做的是检查是否可以永久删除软删除(或类似的东西)。 在这种情况下,创建依赖项会增加测试的复杂性。 通常最好从头开始挂载测试场景(数据/对象),然后执行逻辑,然后测试实际场景是否符合预期。

    【讨论】:

    • 使用这种方法,我将执行两次 doSoftDelete() 测试代码,这是我需要避免的。我不知道我是否可以执行依赖功能。也许,这个解决方案是这里唯一的选择。感谢您的友好回复。
    • 另一种解决方案是不在此文件中使用 DatabaseTransaction 特征,而是在需要时设法清理。如果你在像 TestCase 这样的 BaseClass 中使用,那么你需要从那里删除并添加你正在使用的每个类。
    • 或者你可以从一些使用DatabaseTransaction特征的类继承,而在这个类中你没有。但是,您的代码会变得更加复杂。
    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2014-11-17
    相关资源
    最近更新 更多