views:

118

answers:

5

Suppose that I am intending to draw some user-supplied text on a bitmap in C#, what sort of tests would I write up front?

Is this sort of thing even possible? BDD seems very straight-forward when dealing with mathematical problems but I find it near impossible and more trouble than it's worth when dealing with custom UI controls, graphics etc.

All of the TDD examples that I can find use simple calculation examples like currency conversion or ten-pin bowling scoring which even a newbie can easily do using TDD. Am I missing something?

A: 

Possible, yes (see OCR and computer vision). Realistic, no.

(It might become feasible in the near future: Picture driven programming, Project SIKULI)

SealedSun
+2  A: 

Hmm.. I'd like some more specific examples of the behavior you want to test.

AFAIK, BDD is problem-complexity agnostic. If you can explain it to someone in English, you can write a BDD story / scenario.

GIVEN some text entered by the user 'DRAW THIS'
WHEN I have a bitmap 'c:\temp\SampleImage.bmp'
THEN the text should be drawn on the bitmap

Next you map each of the above steps to glue code - that keys into your app code

  • some text entered by the user 'DRAW THIS' => setText('DRAW THIS')
  • I have a bitmap 'c:\temp\SampleImage.bmp' => loadBitmap('c:\temp\SampleImage.bmp')
  • the text should be drawn on the bitmap => verifyTextOnBitmapInMemory()

The glue code is the developers to define and can be as complex as the situation mandates. Ditto in TDD.

Gishu
Yes, I figured that the best I can do is verify that the correct 'DrawString' command is sent to some sort of IGraphics object.
Damien
@Damien - Is your problem 'how to test X' or 'how to do BDD for scneario involving X'. The first one implies issues with testability of visual interfaces. There are different approaches you can take.. e.g. golden files/outputs to verify against or mocks like you have come up with.
Gishu
Yes, I guess it just feels weird doing all of my sandbox work making sure that I get what I want and then writing tests.Maybe if I was a better programmer, I'd know exactly what to do up front and could write all of the specifications/tests first.
Damien
A: 

If you want automated tests around this, I think it is much more trouble than it is worth. To some extent that depends on how much the elements in question are subject to change in the future.

You can easily, however, add a semi-automated test.

  • run test
  • form displays with bitmap and (hopefully) text drawn on
  • yes/no dialog pops up (launched by test method) - "text drawn properly?"
  • click yes, test passes; click no, fails

In some testing frameworks you can use attributes like [Slow] for tests like this or integration tests that hit a database, so that they don't run in common, everyday test runs, but do in more comprehensive test runs.

Jay
A: 

As others have mentioned, it's really hard. What you really need is GUI unit testing so you can write a BDD test like:

public void can_draw_user_supplied_text_on_bitmap()
{
    draw_on_bitmap();
    check_bitmap()
}

The only tool that does this easily is IcuTest.
Of course, I'm very biased; I wrote it. If you're interested in a beta, let me know.

Ray
The *only* tool?
Jay
+1  A: 

It's not that hard, with the right library. Check out Approval Tests, for Java, .net, and ruby. Full disclosure: it's written by friends of mine.

With approval tests, you draw the image you want, then require the code to produce something that matches. Or - just write the code right, manually accept the image it generates, and then that's your test. Highly recommended, for exactly this kind of problem.

Carl Manaster
Yes, this is typically what I'd expect someone to do. Have an image, have another image with the text drawn on, draw text on image using code, compare sample image with text drawn on to image drawn on with code.
Sekhat