【问题标题】:Skip expensive methods from PHP unit testing从 PHP 单元测试中跳过昂贵的方法
【发布时间】:2021-10-14 13:43:07
【问题描述】:

我想跳过生产 Lavavel 项目中昂贵的方法。我就是这样跳过的

class HomePageTest extends TestCase
{
   use WithFaker, RefreshDatabase;
    /** @test */
    public function something_testing()
    {
      if (  env('APP_ENV') == 'local' ) {
        $response = $this->get('/expensive-service');
        $response->assertStatus(200);
      } else {
         $this->assertTrue(true);
      }
    }
}


以上代码有效。但是,我觉得对于某些州来说,跳过测试方法会有一种优雅的方式。

【问题讨论】:

    标签: laravel phpunit


    【解决方案1】:

    在 PHP 单元中,您可以使用 groups 标记测试,例如

    class HomePageTest extends TestCase
    {
       use WithFaker, RefreshDatabase;
        /** 
         * @test 
         * 
         * @group notProduction
         */
        public function something_testing()
        {
            $response = $this->get('/expensive-service');
            $response->assertStatus(200);
        }
    }
    

    Laravel 附带一个 phpunit.xml.dist,您可以按如下方式更新该文件:

    <groups>
      <exclude>
        <group>notProduction</group>
      </exclude>
    </groups>
    

    关于配置选项的文档是here

    对于本地测试,您可以创建不带排除选项的本地 phpunit.xml

    Laravel 测试将使用 phpunit.xml(如果存在),否则它们会退回使用 phpunit.xml.dist,这样您就可以控制在每个环境中运行的测试配置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      相关资源
      最近更新 更多