Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Driven Unit Tests

What is the best practice for testing an API that depends on data from the database? What are the issues I need to watch out for in a "Continuous Integration" environment that runs Unit Tests as part of the build process? I mean would you deploy your database as part of the build scripts (may be run your installer) or should I go for hardcoded data [use MSTest Data Driven Unit Tests with XML]?

I understand I can mock the data layer for Business Logic layer but what if I had issues in my SQL statements in DAL? I do need to hit the database, right?

Well... that's a torrent of questions :)... Thoughts?

like image 806
Vyas Bharghava Avatar asked Oct 28 '08 17:10

Vyas Bharghava


People also ask

What do you mean by data driven tests?

What is Data-Driven Testing? Data-driven testing (DDT) is data that is external to your functional tests, and is loaded and used to extend your automated test cases. You can take the same test case and run it with as many different inputs as you like, thus getting better coverage from a single test.

Is TDD a unit test?

TDD is a software development approach focused on understanding the problem domain and fulfilling the requirements. Bare unit tests are about validating the written source code and avoiding bugs and regression. In fact, unit tests are part of the TDD cycle.

How do you test data driven?

Data driven testing is implemented by following the below steps : Data driven testing fetches the input data from the data sources like xls, csv, xml files. It enters the input data into the web application by means of automated test scripts. It compares the actual results with expected output.


3 Answers

As far as possible you should mock out code to avoid hitting the database altogether, but it seems to me you're right about the need to test your SQL somewhere along the line. If you do write tests that hit the database, one key tip for avoiding headaches is to make sure that your setup gets the data into a known state, rather than relying on there already being suitable data available.

And of course, never test against your live database! But that goes without saying :)

like image 195
andygeers Avatar answered Sep 29 '22 10:09

andygeers


As mentioned, use mocking to simulate DB calls in unit tests unless you want to fiddle with your tests and data endlessly. Testing sql statements implies more of an integration test. Run that separate from unit tests, they are 2 different beasts.

like image 41
Andrew Cowenhoven Avatar answered Sep 29 '22 11:09

Andrew Cowenhoven


It's a good idea to automatically wipe the test database and then populate it with test harness data that will be assumed to be there for all of the tests that need to connect to the database. The database needs to be reset before each test for proper isolation - a failing test that puts in bad data could cause false failures on tests that follow and it gets messy if you have to run tests in a certain order for consistent results.

You can clear and populate the database with tools (DBUnit, DBUnit.NET, others) or just make your own utility classes to do the same thing.

As you said, other layers should be sufficiently decoupled from classes that actually hit the database, so the need for any kind of database being involved in testing is limited to tests run a small subset of your codebase. Your database accessing components can be mocked/stubbed for everything that depends on them.

like image 27
aaronroyer Avatar answered Sep 29 '22 10:09

aaronroyer