Model testing in Magento 2

Posted on

Sometimes you need to quickly test the Model of your module or another vendor. And in order not to create a complete module, we can do it on one file.

Create file test.php in folder /pub:

ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();
$model = $obj->get('Vendor\ModuleName\Model\YourModel');
// or

Run script in terminal from project root folder:

php pub/test.php

Tested in Magento 2.4.3