spacegrass/bareback

在运行测试时选择性使用Laravel框架

0.2 2020-04-12 02:04 UTC

README

Bareback - selectively turn off Laravel in your test suite

Bareback

Bareback是一个Laravel包,它使得在选择哪些测试在框架加载的情况下运行变得容易。

为什么我想在没有框架的情况下运行测试?

在没有加载Laravel框架的情况下运行测试要快得多,通常从50%到70%。通常,尽可能少地依赖实现核心业务逻辑是一个好主意。然而,为没有框架依赖的应用程序创建整个测试套件既不实用也不现实。这个包允许你在“单元”测试中关闭框架以测试核心业务逻辑,并在运行端到端或“集成”测试时打开框架。

安装

composer require "spacegrass/bareback"

用法

首先,您需要扩展基本TestCase,它反过来扩展Laravel TestCase

use Spacegrass\Bareback\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
    //
}

要阻止框架加载,只需在您的测试方法中添加@withoutFramework注解

/**
* @withoutFramework
*/
public function test_something_quickly()
{
    //
}

或者,将注解添加到类本身,那么其中的所有测试方法默认不加载框架。如果您想打开框架,只需将@withFramework注解添加到任何测试方法,那么只有那个测试方法会加载框架。

/**
* @withoutFramework
*/
class FastTests extends TestCase 
{
    public function test_something_without_framework_by_default()
    {
        //
    }

    /**
    * @withFramework
    */
    public function test_something_with_the_framework_for_some_reason()
    {
        //
    }
}]

您可以通过添加noFrameworkSetupframeworkSetup方法到您的测试用例中,为框架加载或不加载时设置特定的设置。

class SometimesRunningWithFrameworkTestCase extends TestCase
{
   public function noFrameworkSetup() 
   {
       // register fake repositories or mock something out, for example
   }
   
   public function frameworkSetup()
   {
       // perhaps add something specific to your database migrations or anything else
       // the is dependent on the framework
    }

如果您希望默认不使用框架运行所有测试,并在运行带框架的测试时强制采用“opt-in”方法,只需添加withFramework属性。

class RunTestsWithoutFrameworkByDefaultTestCase extends TestCase 
{

    protected $withFramework = false;

    public function test_runs_with_out_framework()
    {
        //
    }

    /**
    * @withFramework
    */
    public function test_requires_opt_in_to_use_framework
    {
        //
    }
}