Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for TDD and reporting

Tags:

tdd

pdf

reporting

I am trying to become more familiar with test driven approaches. One drawback for me is that a major part of my code is generated context for reporting (PDF documents, chart images). There is always a complex designer involved and there is no easy test of correctness. No chance to test just fragments!

Do you know TDD practices for this situation?

like image 426
Dirk Brockhaus Avatar asked Jan 18 '10 06:01

Dirk Brockhaus


4 Answers

Some applications or frameworks are just inheritently unit test-unfriendly, and there's really not a lot you can do about it.

I prefer to avoid such frameworks altogether, but if absolutely forced to deal with such issues, it can be helpful to extract all logic into a testable library, leaving only declarative code behind in the framework.

like image 180
Mark Seemann Avatar answered Oct 29 '22 19:10

Mark Seemann


The question I ask myself in these situations is "how do I know I got it right"?

I've written a lot of code in my career, and almost all of it didn't work the first time. Almost every time I've gone back and changed code for a refactoring, feature change, performance, or bug fix, I've broken it again. TDD protects me from myself (thank goodness!).

In the case of generated code, I don't feel compelled to test the code. That is, I trust the code generator. However, I do want to test my inputs to the code generators. Exactly how to do that depends on the situation, but the general approach is to ask myself how I might be getting it wrong, and then to figure out how to verify that I got it right.

Maybe I write an automated test. Maybe I inspect something manually, but that's pretty risky. Maybe something else. It depends on the situation.

like image 42
Jay Bazuzi Avatar answered Oct 29 '22 18:10

Jay Bazuzi


To put a slightly different spin on answers from Mark Seemann and Jay Bazuzi:

Your problem is that the reporting front-end produces a data format whose output you cannot easily inspect in the "verify" part of your tests.

The way to deal with this kind of problem is to:

  1. Have some very high-level integration tests that superficially verify that your back-end code hooks correctly into your front-end code. I usually call those tests "smoke tests", as in "if I turn on the power and it smokes, it's bad".

  2. Find a different way to test your back-end reporting code. Either test an intermediate output data structure, or implement an alternate output front-end that is more test-friendly, HTML, plaintext, whatever.

This similar to the common problem of testing web apps: it is not possible to automatically test that "the page looks right". But it is sufficient to test that the words and numbers in the page data are correct (using a programmatic browser surch as mechanize and a page scraper), and have a few superficial functional tests (with Selenium or Windmill) if the page is critically dependent on Javascript.

like image 36
ddaa Avatar answered Oct 29 '22 19:10

ddaa


You could try using a web service for your reporting data source and test that, but you are not going to have unit tests for the rendering. This is the exact same problem you have when testing views. Sure, you can use a web testing framework like Selenium, but you probably won't be practicing true TDD. You'll be creating tests after your code is done.

In short, use common sense. It probably does not make sense to attempt to test the rendering of a report. You can have manual test cases that a tester will have to go through by hand or simply check the reports yourself.

You might also want to check out "How Much Unit Test Coverage Do You Need? - The Testivus Answer"

like image 25
Jim Mitchener Avatar answered Oct 29 '22 20:10

Jim Mitchener