How do I add classes to form elements in the theme layer?
This is an ideal situation for a preprocess function.

I don't have Drupal 7 working to test at the moment, but something like this in your template.php should do the trick.

function mytheme_preprocess_textfield(&amp;$vars) {
  static $counter = 0;
  $counter++;
  $vars['element']['#attributes']['class'][] = 'test' . $counter;
}