Testing – How to Use Fixtures in Unit Testing

testing

I'm trying to understand the use of fixtures in the context of unit testing models in a web MVC system.

The problem I am trying to understand is that fixture data does no give a true representation of the data that the system accepts.

A simple example is say I have a user model that has email validation. Fixtures will bypass the validation so the tests maybe testing invalid data.

Edit: What I'm trying to get at is, wouldn't it make more sense to create the test data through the application business logic?

So for the email example, if a developer accidentally created a fixture with an email that would not pass validation, this could cause false negatives in testing (eg testing sending emails would fail because the fixture has invalid data).

The email example probably isn't the best, but I hope that makes more sense what I'm trying to get at?

Best Answer

in the context of unit testing

wouldn't it make more sense to create the test data through the application business logic?

In some cases, that might be a more useful test to write. But be aware you have moved out of the realm of unit testing and into integration testing. Both are necessary.

if a developer accidentally created a fixture with an email that would not pass validation, this could cause false negatives

I would argue you should intentionally create invalid test input, and test that your unit handles it appropriately. If you do this for every unit, your code becomes more robust.

In other words, testing against invalid data forces you to code defensively, which is a good thing.