1

I'm trying to learn how to write unit tests the 'proper' way and only test a specific component. (Using http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/ for reference)

In one case, I've written a primative wrapper to query an old MS SQL 2000 database in PHP/PHPUnit. Without getting into specifics, it looks like this:

$mssql = MSSQL::connect($credentials);
$mssql->insert($table, $assoc_array_of_data);
$mssql->select($table, $columns, $where_clauses);
$mssql->update($table, $assoc_array_of_data, $where_clauses);
$mssql->delete($table, $where_clauses);

My question is, how would I structure a unit test to test each method in isolation, since they depend on each other? My current unit test basically does the following:

// Connect to the DB
// Insert a test row
// Select it to verify it made it in
// Update the test row
// Select it to verify the update worked
// Delete the test row
// Select it and verify it isn't there

Is this a proper approach to a unit test, or is there a better way?

3
  • 4
    Mock or stub the methods that the method your are testing depends on.
    – user246
    Jun 18, 2014 at 16:34
  • From the article you link to "You’ve definitely taken a wrong turn if you have to run your tests in a specific order, or if they only work when your database or network connection is active". What exactly are you trying to test? What is the purpose of each test? Jun 18, 2014 at 20:13
  • I'm trying to test whether each method of the class works. But in order to test that mssql->select() works, for example, I first have to have data to test. So I'm using mssql->insert() first. Testing it against a mock database wouldn't really guarentee my code actually works, since I'm trying to test the database model itself. So I guess my question is, would this approach be correct for this particular problem? I can't think of how to decouple them, or if they should be decoupled.
    – Anthony
    Jun 19, 2014 at 16:26

1 Answer 1

1

This doesn't sound like code you'd want to unit test, since the purpose of it seems only to be interacting with a database. What you want here is integration testing which will verify that parts of your system work together correctly. CRUD tests like this are pretty common integration tests in my experience.

It is possible that there is something worth unit testing here though. Does your code do anything besides forward requests to the database? I've seen some code like this that does things like formatting SQL commands from objects before sending them to the database engine. That's the kind of scenario where you could stub the database and verify that your query generation code is creating valid SQL queries.

When figuring out how and what to unit test a lot of it comes down to whether or not you wrote the code. Ideally you'll be able to unit test all of the code you write yourself, but you'll usually get to some point where your code is just sending data or commands to 3rd party code, like a database. You'll want to minimize the amount of code that's strongly coupled to 3rd party code to make it more testable. How much of your example code depends on how much it does. If it's just a wrapper around existing 3rd party code there's not much for you to do. If it's doing anything else you can pull your code into external components and test those components in isolation.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.