3

I'm new to unit testing and I'm trying to get started with PHPUnit on an existing project I'm working on.

The problem I'm facing is that I have lots of unit tests that require a database which is fair enough. I've set up an SQLite DB for the sole purpose of unit testing. Sometimes I want to drop and re-create the database for new tests (by this I mean each separate classes), to prevent unnecessary data clashes.

However, sometimes I don't want this to happen if I have unit tests in the same class that depend upon one another; these may need access to data that was saved in a previous test.

I am currently getting a "fresh" database in the setUp() function of each class. What I didn't anticipate is that this function (as with __construct()) would run after every test case within said class.

Is there a way that I can flush the database with each test class? Or am I going about the entire process incorrectly?

Any tips appreciated, thanks.

3
  • 2
    I haven't gotten that far in my self-teaching of PHPUnit, but I have a feeling you might want to look into the @depends annotation. I believe that lets you indicate that a test depends on the outcome of some other test, though I'm not sure if it preserves state from one test to the next. Also, just a note on architecture. The set of objects that talk directly to the DB should be fairly small, with higher level objects using the DB objects to communicate indirectly. Then when it comes to testing, you can just mock the DB objects for the other objects to talk to Commented Jul 30, 2012 at 16:57
  • @PeeHaa I read it as he uses an sqlite db purely for testing. Commented Jul 30, 2012 at 16:58
  • @PeeHaa I'm using an in-memory database for the tests Commented Jul 30, 2012 at 16:58

2 Answers 2

3

I also started using PHPUnit fairly recently (about a year ago). The first thing I did was to set up unit tests for the project I was working on at the time. I decided it was a good idea to test the data access layer too and did a similar thing to you. It took me days to set up and I ended up with unit tests that took 8 minutes to run! 99% of that time was spent setting up and tearing down the test database. What a disaster!

What I did was to refactor the project so that only one class actually needed to talk to the database and had integration tests for this, but no unit tests. That meant that my project now had to use dependency injection to facilitate testing. I ended up with a suite of tests that run in around 2-3 seconds and a project that practically wrote itself. It is a dream to maintain and make changes/additions to, I wish all my code had been written that way.

Basically, what I am trying to say in my long winded way is that you should change your code to be easy to test, not try and force the unit tests to fit code not designed in a test driven way. The time you invest in that now (if you can) will be repaid back later with dividends!

Bite the bullet and refactor now!

Sign up to request clarification or add additional context in comments.

Comments

2

The setUp and tearDown functions do exactly as you mention, which is to set up the test environment for each test execution and then clean up after each test case exeuction.

What you probably want to do is set up your database data provider at the suite level.

Suite level fixtures

This is probably not really the best way to do things to get true isolated unit tests (i.e. you could set up mock data provider for DB's etc). But it is something that you can do that should meet your immediate needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.