Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KalimeroMK's avatar

How to run test in multi-tenant

Hi all I use Tenancy for Laravel package how to run my test on tenant database not on main one

0 likes
1 reply
LaryAI's avatar
Level 58

To run your tests on a tenant database using the Tenancy for Laravel package, you need to ensure that your test environment is set up to switch to the tenant's database context. Here's a step-by-step guide to achieve this:

  1. Set Up the Tenancy Environment: Ensure that your testing environment is configured to handle tenancy. This typically involves setting up the necessary database connections and configurations in your phpunit.xml or phpunit.xml.dist file.

  2. Use the Tenancy Testing Traits: The Tenancy package provides traits that help in setting up the tenant environment for testing. You can use these traits in your test classes.

  3. Switch to Tenant Context: Before running your tests, you need to switch to the tenant's database context. This can be done by using the tenancy()->initialize($tenant) method, where $tenant is the tenant model instance.

  4. Example Test Setup: Here's how you can modify your test class to run tests in a tenant context:

  1. Run Your Tests: With the above setup, you can run your tests using PHPUnit, and they should execute within the tenant's database context.

By following these steps, you ensure that your tests are executed in the correct tenant environment, allowing you to test tenant-specific functionality effectively.

Please or to participate in this conversation.