[Bioc-devel] [BioC] NAMESPACES, export, and unit tests

Seth Falcon sfalcon at fhcrc.org
Tue May 2 16:12:22 CEST 2006

hpages at fhcrc.org writes:
> Yes sorry, I could not remember exactly what I've tried once and that
> didn't work for me. Now I found it: you can't use setGeneric() or
> setMethod() in a test unit. For example, you can't have this in your
> runit-toto.R file:
>   setMethod("==", signature(e1="BString", e2="DNAString"),
>     function(e1, e2) { ... }
>   )
> at the top level or nested in one of your test_totoN() functions.
> Even if you don't have a NAMESPACE.
> Or you'll get something like:
>   Executing test function test_totoN  ...
>   Error in setMethod("==", signature(e1 = "BString", e2 = "DNAString"),  :
>   the environment 'RUnit' is locked; cannot assign methods for
>   function '=='

I see.  Usually one has a test runner file that sets up the RUnit test
suite.  Have you tried putting these helpers there?  That is, defined
before you actually start running the tests?

> My comment was more about the motivations on the RUnit side than
> on the S4 implementation side. Lets's keep the S4 implementation
> discussion for another day ;-)

Well, the namespace on RUnit is quite useful.  If you have ever had a
name collision with your testing framework, then you have probably
spent hours trying to sort out a really strange bug (yes, it has
happened to me).

One point of view is that if you need to define new methods to test
your code then there is likely a design improvement to be had.  A nice
way to use unit testing is to help it drive your design.

I think there is a difference between helper functions in tests that
reduce duplication across tests and adding new behaviors to the
objects you are testing (that is your case with "==").  In Python, for
example, that would be like defining a subclass within your test
method and testing that.  In most cases, that just seems like a weird
thing to do in test code.

An additional consideration is that unit tests can be a valuable API
reference for other developers.  That value is diminished if many of
the method calls are test-only code.

+ seth

More information about the Bioc-devel mailing list