Laravel Testing with Multiple Databases: Preventing Migrations During Tests / Migrations failing

I’m working on a Laravel project that uses multiple MySQL databases. The main database (core_db) contains system tables like users, migrations, etc., while express_db and regio_db contain most of the other application tables. For testing, I’m using phpunit and pest, and I want to ensure that the tests run efficiently without unnecessary migrations. Even tho all migrations are already ran, Laravel is rerunning the migrations, and then its failing, because the main dbs are already up2date but Laravel is not recognizing it.

Here are the relevant configurations and files:

<?php

namespace Tests;

use IlluminateFoundationTesting{RefreshDatabase, TestCase as BaseTestCase};
use LaravelTelescopeTelescope;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        config()->set('audit.enabled', false);
        Telescope::stopRecording();
    }
}

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="PULSE_ENABLED" value="false"/>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_DATABASE" value="core_db_testing"/>
        <env name="DB_USERNAME" value="dbuser"/>
        <env name="DB_PASSWORD" value="test"/>
        <env name="DB_CONNECTION_EXPRESS" value="mysql"/>
        <env name="DB_DATABASE_EXPRESS" value="express_db_testing"/>
        <env name="DB_USERNAME_EXPRESS" value="dbuser"/>
        <env name="DB_PASSWORD_EXPRESS" value="test"/>
        <env name="DB_CONNECTION_REGIOANALYSIS" value="mysql"/>
        <env name="DB_DATABASE_REGIOANALYSIS" value="regio_db_testing"/>
        <env name="DB_USERNAME_REGIOANALYSIS" value="dbuser"/>
        <env name="DB_PASSWORD_REGIOANALYSIS" value="test"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
    <source>
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </source>
</phpunit>

pest.php:

<?php

use AppModelsUser;
use LaravelTelescopeTelescope;


uses(TestsTestCase::class)
    ->beforeEach(function () {
        Telescope::stopRecording();
    })
    ->in('Feature');


expect()->extend('toBeOne', function () {
    return $this->toBe(1);
});

.env.testing:

APP_ENV=testing

DB_CONNECTION=mysql
DB_HOST=coreapi-mysql-1
DB_PORT=3306
DB_DATABASE=core_db_testing
DB_USERNAME=dbuser
DB_PASSWORD=test

DB_CONNECTION_EXPRESS=mysql
DB_HOST_EXPRESS=database-mysql-1
DB_PORT_EXPRESS=3306
DB_DATABASE_EXPRESS=express_db_testing
DB_USERNAME_EXPRESS=dbuser
DB_PASSWORD_EXPRESS=test

DB_CONNECTION_REGIOANALYSIS=mysql
DB_HOST_REGIOANALYSIS=database-mysql-1
DB_PORT_REGIOANALYSIS=3306
DB_DATABASE_REGIOANALYSIS=regio_db_testing
DB_USERNAME_REGIOANALYSIS=dbuser
DB_PASSWORD_REGIOANALYSIS=test

I’m running the tests using the following command:

sail artisan test --profile --stop-on-failure --exclude-group=brands_api --env=testing

Even though I use the RefreshDatabase trait, migrations are still being run during tests, causing them to fail. According to the Laravel documentation, RefreshDatabase should only migrate if the schema is out of date and otherwise just wrap each test in a transaction.
Same behaviour for DatabaseTransactions or DatabaseMigrations of course.

How can I ensure that migrations are only run if necessary and that the database state is correctly maintained for tests? Additionally, how can I prevent migrations from running every time if the schema is already up to date? What am I missing? Why is it re-running the migrations every time and cause the whole test to fail, since the migrations are already done?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật