Reputation: 41
I'm writing a raytracer, and I would like to code it using TDD, top-down approach. I don't wan't to bore you with the details so in a nutshell the program will work in a way that you give it a specified scene (for example coordinates of a sphere and its radius), and it will output an image with 3d sphere rendered. How in the world can I test first such behaviour?
I know I can test first some inner algorithms that are inside that .render() function, but I would like to go top-down, and I just can't anticipate the generated image in advance. I know I can test it for beeing all black or specified size, but if I would like to be strict in using TDD those tests wouldn't get me anywhere: "you shall not implement more than its required to pass the test".
So, any thoughts?
Upvotes: 4
Views: 272
Reputation: 30032
Do not try to automate testing the image. Your eyeballs are infinitely better at verifying images than any code you could possibly write. As a regression test you can capture the image output and do a pixel compare, but that will be a fragile regression test, not a TDD unit test.
Extract the rendering logic into separate testable classes. I assume there will be lots of calculations and algorithms you can TDD unless you can find an existing library (which is probably worth looking for). TDD is about design and right now it is telling you to separate out the rendering algorithm from the display.
Upvotes: 3
Reputation: 14366
Obviously you can't anticipate the exact image for a given use case, or you wouldn't need the project. Not even the most rabid TDD advocate would demand that you write the complete test first. However, you may well write the load-image-and-compare-to-program-output part first and insert the actual image later, and I would actually recommend doing that. Starting out with a use of the proposed API is always a good idea, as it can expose issues that you would otherwise never think about until it's too late.
It is also possible that you already know, not the exact image, but certain properties of the image that you want to test. For instance, if you render the obligatory reflecting metal sphere, you may well know how bright (approximately) certain areas of the image have to be, or that certain parts have to be symmetrical, etc. It is a good idea to write those tests first. (I maintain a test suite for automatically generated diagrams, and they contain tests such as "all ten colours must be visible in this image", i.e. concentric circles should be drawn largest-first. Raytracing output is, of course, much more complicated, but the principle applies.)
Upvotes: 1