This question is related to PHPUnit, although it should be a global xUnit design question.
I'm writing a Unit test case for a class Image
.
One of the methods of this class is setBackgroundColor()
.
There are 4 different behaviors I need to test for this method,
array(255,255,255)
array('red' => 255, 'green' => 255, 'blue' => 255)
(this is the output format of the GD function imagecolorsforindex()
)IMG_COLOR_TRANSPARENT
At the moment, I have all this contained within 1 test in my test case called testSetBackgroundColor()
, however I'm getting the feeling these should be 4 separate tests as the test is getting quite long and doing a lot.
My question is, what should I do here? Do I encapsulate all this into 1 test of the Image test case, or do I split the above into separate tests like,
testSetBackgroundColorErrors
testSetBackgroundColorShorthandRGB
testSetBackgroundColorRGB
testSetBackgroundColorTransparent
I've put the test in question here http://pastebin.com/f561fc1ab.
Thank
Split it. Absolutely.
When a unit test fails it must be immediately clear what exactly is broken. If you combine the tests, you'll be debugging a unit test failure.
By the way, are you writing tests first? With TDD it's unlikely to end up with bloated tests.
My preference is to split the tests as you describe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With