我有一个单元测试,并且想基于测试类中的一个参数运行一次测试 我有以下代码:
<?php
namespace Tests\Unit;
use Tests\TestCase;
class LoginTest extends TestCase
{
protected $loginCount = 0;
protected $defaultLoginAttempt = 3;
public function testLoginAttempt()
{
$response = $this->json(
'POST',
'/api/user/v1/login',
[
'email' => 'email',
'password' => 'password'
]
);
$response->assertStatus(200);
$stack['userBearer'] = 'Bearer ' . $response->decodeResponseJson()['access_token'];
// echo "\r\n"."=> Start happy path test" . "\r\n";
echo "\r\n" . $stack['userBearer'] . "\r\n";
return $stack;
}
/**
* @depends testLoginAttempt
*/
public function testLoginAttempt2()
{
$response = $this->json(
'POST',
'/api/user/v1/login',
[
'email' => 'email',
'password' => 'password'
]
);
$response->assertStatus(200);
$stack['userBearer'] = 'Bearer ' . $response->decodeResponseJson()['access_token'];
// echo "\r\n"."=> Start happy path test" . "\r\n";
echo "\r\n" . $stack['userBearer'] . "\r\n";
return $stack;
}
/**
* @depends testLoginAttempt2
*/
public function testDataInsert(array $stack)
{
if ($this->loginCount < $this->defaultLoginAttempt) {
$this->assertTrue(true);
$this->loginCount = $this->loginCount + 1;
$this->testLoginAttempt();
}
}
}
?>
在最后一个函数中,我想再次更新全局变量和运行测试,直到满足所有条件为止。 有没有更好的方法可以实现相同目的? 当我运行测试时,它会成功调用第一个函数(testLoginAttempt),但不会运行其他依赖的函数。