Quote:
Originally Posted by clowntable
Maybe I'm not a testing guru but I tried to TDD prototype a little game because I wanted to test Ruby+Gosu and I have a draw method that kind of looks like this:
Code:
def draw
# x (positive=right),y (positive=down),z layer (0=lowest)..starting point is 0,0 top/left
# Draw the background, at Z layer 0
@background_image.draw(0,0,0)
# Draw the inventory, at Z layer 1
@inventory_image.draw(0,background_image.height-@inventory_image.height,1)
# XXX: TODO Draw the player
end
I want to have a test along the line of
Code:
it "should draw background and inventory" do
# XXX TODO: How do we check if stuff is actually drawn?
game.draw
end
Kind of hard to say what I want but how do I actually test that the stuff I want is drawn? I guess I should compare the surface before and after the draw somehow and see they are not equal? Other ideas? Basically I want to say "make sure that inside the draw method X,Y,Z is drawn (i.e. some other methods that do this are called)" and then also "check that said methods draw stuff correctly"
I guess I could also check that a certain region has a certain color after the draw takes place or something.
clown,
i'm far from a guru but i'll share some stuff that occurs to me:
- in general, only test code that you're writing. if you're using some 3rdparty code that actually puts pixels on the screen then you're not in a good position to test that code and it's not your job anyway. when you write code that tests the list.append() method, do you write tests for that? no, you assume it works as advertised.
- what i do want to test is that my code does the things i expect it to. your supplied example is a little weird since, in a sense, it only has side effects. more frequently, your methods will be changing some state in an object or updating data or something -- an action with a concrete, measurable outcome. that's the stuff i'm verifying with my tests.
- if you really do want to test that the pixels are getting drawn correctly, here are a few things you might try:
-- mock the canvas/window/whatver is displaying the pixels. a mock framework will give you stuff like "make sure this method was called with parameters foo and bar".
-- design your draw method such that the test can provide its own thing to draw. by default you draw the background and the inventory and everything else, but the test says "just draw this square". then you can check that the corners of the square are drawn correctly (by directly inspecting the screen/canvas/window/etc.).
-- as a last resort, dump the screen to a file and compare it with a known master or look for specific features or whatever. we do something like this at my soon-to-be-former job as a big end-to-end test. it's very powerful but also a huge pain in the ass to maintain.
- btw all this advice applies to the general problem of automated gui testing, which is why i have so much to say on this topic (i quickly tire of qa dudes who use "it's too hard" as a justification for doing ****ty manual testing for the rest of their sad little lives).
hth!