Commands defined in that way are fragile, so you need
\protect\testMe in the moving arguments, for instance the caption text.
Let's see why. The argument to
\caption needs to be massaged a few times, in particular it gets written to the
.aux file for possible inclusion in the list of figures (or tables).
When TeX does a
\write operation, it expands commands “all the way”, unless instructed not to do so (which is the purpose of
\protect). During this expansion in
\@ifstar is defined upon) break because they need to assign a value to a scratch macro to work and this is not possible during expansion.
Thus you have three possibilities:
\protect\testMe when in a moving argument
\DeclareRobustCommand for defining
\testMe (same syntax as
The last possibility needs a comment. If you do
you get basically the same as your
\testMe, but automatically protected when found in moving arguments.