25

I'm working in image processing (mainly OCR) and I wonder how I should integrate unit tests in my development.

I'm already using unit tests for more "common" type of code but when dealing with image processing code I'm not sure how to deal with it. This kind of code always need some image data input/output and mocking this is not obvious. For now I'm mostly doing integration tests but they take a while to run and I would like some ideas on how to break down this kind of code into unit tests so that I can run them more quickly.

Edit: Analyzing a character can go through many steps involving multiple rotation, scaling and morphological operations. These steps change often as the algorithm is being developed. Thus the input and expected output can evolve a lot while testing. Each character can be 100x100 pixels so hardcoding them in the code or working with generated data is out of question.

Doc Brown
  • 218,378
rold2007
  • 375

1 Answers1

19

I work with video recording/analytics/streaming software and we faced a very similar problem. Below was our solution, not sure how it'll work out long-term but for now it seems to work.

Save input/output images as resources in your unit test project. Then have unit test verify that when a specific input is given, that specific output is produced.

9/10 times when you refactor the code and add other functionality, you would expect the behavior of your image handling routines not to change, so if all of a sudden unit tests start failing, it's likely due to an error.

On the other hand, if you make changes in the actual algorithm, that will also result in unit test failure. In this case, you would have to manually/visually verify that the results are correct and if they look good, then update the image resources to make the unit test pass again.

In our project, we ended up developing "fake" (or mock if you will) video sources, that can feed us data both for input and output. But the data itself is not fake, it was actually captured using helper data recording classes from a running system when we ran manual tests and verified that everything was working.

DXM
  • 20,022