jeudi 27 octobre 2016

Data-driven unit tests with google test

I am currently writing unit tests for an embedded application using googles unit test framework. Now my boss got upset that the data I test with (i.e. the values with which I call methods of the class under test) is hard wired in the tests. He requests to have this data read-in from a file. His argument is that it would thus be easier to add another test for a corner case that was previously forgotten. I am not that experienced with unit tests but so far that was not how I did it. So I tried to figure out what would be the best way to do it - even if it is a good idea to do it at all. I quickly came across the DDT (data-driven testing) approach.

The google unit test framework has a feature they call "Value-Parameterized Tests". With that my test fixture becomes a template class and I can pass in parameters. However, I see some problems with this:

  • I currently have a fixture per class under test. But I would need a fixture per method under test since each method requires a different set of parameters. This will be a lot of extra work.
  • As I see it I can pass in only a single parameter. Since I need several ones for my tests (all parameters for my method plus the expected results) This would require me to pass in something like a vector or map. Again this construction and retrieval sounds like a lot of work.

I would have imagined that something as mature as the google test framework to make it easier. However, they write

value-parameterized tests come handy [when] you want to test your code over various inputs (a.k.a. data-driven testing). This feature is easy to abuse, so please exercise your good sense when doing it!

Additionally there exists this blogpost TotT: Data Driven Traps, also warning me of (abusing) data-driven unit tests.

So my question comes down to:

  • Is it a good idea to do data driven unit tests?
  • How to do data-driven unit tests with google test framework

I am not really bound to googletest and basically free to choose any framework I'd like, though.

Aucun commentaire:

Enregistrer un commentaire