Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:06:14+00:00 2026-05-27T02:06:14+00:00

I am writing tests for our system that does joins across multiple databases. I

  • 0

I am writing tests for our system that does joins across multiple databases. I chose to use MySQL for my test databases, i.e. test_database1 and test_database2 which I create by dumping my production schemas into the respective tables. Note that I don’t need any data at this point, only the schemas.

What is the best way for me to get these databases cleared out before I run a test? I have two requirements:

  1. There are some foreign key constraints which prevents me from just being able to TRUNCATE, so I need each table cleared out “manually”.
  2. I need both databases cleared out.

I looked into overriding:

class PHPUnit_Extensions_Database_Operation_Truncate implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation

in DbUnit but it seems to rely on the DataSet contents which in my case will be empty since I preload my schema via mysqldump –no-data.

Any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:06:15+00:00Added an answer on May 27, 2026 at 2:06 am

    You could create a utility function that rebuilds the database which can be called before tests which require a clean database or in you setUp() function:

    class DbUtil {
    
        public static function rebuild($src, $dest) {
            exec("mysql -u someuser -p password -e 'DROP DATABASE $dest'");
            exec("mysql -u someuser -p password -e 'CREATE DATABASE $dest'");
            exec("mysqldump -d -u someuser -p password $src > mysql -u someuser -p password $dest");
        }
    }
    
    class MyTest extends PHPUnit_Framework_TestCase {
    
        protected function setUp() {
            DbUtil::rebuild('prod_database1', 'test_database1');
            DbUtil::rebuild('prod_database2', 'test_database2');
        }
    
        // ...
    }
    

    This may not be the most performant approach but you will get a clean database for every test.

    If you are willing or can to switch from MySQL for your tests, I have had success building an template sqlite database file and copying that file to a different location for testing. Each test just overwrites the test database file with the clean template file.

    Note: Exec command are written for demonstration purposes only and have not been tested for exact switches/parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a test script for our MLS( Multiple Listing System ) systems.
We are writing unit tests for our ASP.NET application that run against a test
I have a dev, that will get around our code coverage by writing tests
I am writing an interface into our system that will allow end to end
I'm writing some unit tests that serialize and deserialize all of our types that
I'm writing some unit tests which are going to verify our handling of various
I'm writing tests for a business method that invokes some DAO classes to perform
For example, I'm writing tests against a CsvReader. It's a simple class that enumerates
I started writing functional tests for my rails app today. I use the RESTful
I'm writing tests for a Django application that uses an external data source .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.