我试图进行一个单元测试来进行数据库查询,但是每当我运行php artisan test
时,PHP都会开始消耗内存,直到我的虚拟服务器上没有空闲的RAM为止。
为了进行测试,我将虚拟服务器内存增加到16 Gb,它消耗了所有16 GB的内存,然后崩溃!
所以我得到了一个Laravel教程项目的简单测试,并尝试在它上创建一个单元测试。
以下是模型:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Traits\Timestamp;
use Laravel\Sanctum\HasApiTokens;
class Student extends Model
{
use HasFactory, HasApiTokens;
protected $table = "students";
protected $fillable = [
"name",
"email",
"password",
"phone_no",
];
public $timestamps = false;
}
和单元测试:
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Student;
class ExampleTest extends TestCase
{
public function setup() : void
{
$this->setup();
}
/**
* A basic test example.
*
* @return void
*/
public function test_example()
{
$st = new Student();
$st->select("*")->get();
$this->assertTrue(true);
}
}
测试结果如下:
Symfony\Component\Process\Exception\ProcessSignaledException
The process has been signaled with signal "9".
at vendor/symfony/process/Process.php:441
437▕ usleep(1000);
438▕ }
439▕
440▕ if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
➜ 441▕ throw new ProcessSignaledException($this);
442▕ }
443▕
444▕ return $this->exitcode;
445▕ }
+15 vendor frames
16 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
我尝试了很多事情,改变设置等等,但没有什么帮助!
更新1
仅供参考:
php -v
PHP 8.1.10 (cli) (built: Sep 18 2022 12:52:19) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
with Zend OPcache v8.1.10, Copyright (c), by Zend Technologies
更新2 I创建简单的最小项目:
https://github.com/vasiliyaltunin/LaravelPhpUnitDatabase
所以你可以自己测试它,也许有PHP版本的问题,或者其他我已经试过的东西!
更新3
所有问题都解决了!
我希望测试项目帮助ppls了解数据库测试是如何工作的。
我通过为表添加播种机和使用RefreshDatabase来更新测试项目,所以在每次测试运行时,它都将擦除和重新播种表!
您可以删除使用RefreshDatabase;如果您不想每次都丢失您的数据!
发布于 2022-09-26 23:24:35
您的安装方法正在创建一个无限循环:
public function setup() : void
{
$this->setup(); // calling itself infinite times
}
您可能想要调用parent::setup()
:
public function setup() : void
{
parent::setup();
}
https://stackoverflow.com/questions/73850581
复制相似问题