Tuesday, August 29, 2006

Ruby Unit Test Parameters

My project team recently started following a new convention of adding parameters to the test methods in our code base. However, the parameters are not arbitrary nor do they represent each variable used in the test. Instead they represent the arguments that are passed to the method under test.

We found this pattern by noticing that our tests generally focused on testing one method at a time. In the test methods we decided to clone the parameters that the method under test required.

For example, the method and test for addition could be:
class MathTest < Test::Unit::TestCase
def test_add(x=1, y=2)
assert_equal 3, Math.add(x,y)

class Math
def add(x, y)
x + y
A benefit to this style of test development is the ability to easily see which method is under test and what the values are of the arguments passed to the method under test. However, this does introduce a maintainability issue. Clearly, changing the parameters to the method under test will not automatically change the parameters to the test method. Additionally, when methods have multiple execution paths, thus requiring multiple test methods, the naming convention begins to break down.

When the effort is made to keep the definitions between the test and method under test synchronized this test definition convention has been shown to increase maintainability.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.