您可以在设置中排除一种测试方法吗? (Laravel测试Phpunit)

时间:2018-08-12 10:55:41

标签: laravel phpunit

我正在寻找一种优雅的方式来排除phpunit设置的一种测试方法。 要进一步解释,请参见以下代码:

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

    $this->signUp; //creates and logs in the user
}

/** @test */
public function guest_cannot_see_request_page() 
{
    $this->get(route('requests.list'))
         ->assertRedirect(route('login'));
}

但是我想排除guest_cannot_see_request_page()方法的登录。既然应该是客人。对于我所有其他方法,用户均已登录。

2 个答案:

答案 0 :(得分:0)

您的测试方法将根据以下条件执行:

  • 它具有一个@test docblock参数
  • 方法名称以test开头

因此将执行以下任何操作:

/** @test */
public function guest_cannot_see_request_page() 
{
    $this->get(route('requests.list'))
         ->assertRedirect(route('login'));
}

/** Just human readable here */
public function test_guest_cannot_see_request_page() 
{
    $this->get(route('requests.list'))
         ->assertRedirect(route('login'));
}

如果要禁用被调用进行测试的方法,只需确保两种方法均未应用:

/** Just human readable */
public function guest_cannot_see_request_page() 
{
    // This test will not be executed
    $this->get(route('requests.list'))
         ->assertRedirect(route('login'));
}

注意。请注意,phpunit会将其视为跳过测试的风险。

答案 1 :(得分:0)

如果将 setUp() 方法重命名为 manualSetUp() 并在运行代码的测试方法的开头调用它,则可以实现所需的结果。

相关问题