Skip to content

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

3 messages · Hervé Pagès, Seth Falcon

#
Herve Pages <hpages at fhcrc.org> writes:
I would say that the "normal" way to access non-exported objects is to
not access them :-)

IMHO, unit tests should focus on the exported API.  Part of the value
that unit tests give you is that when you change implementation, they
tell you whether or not you broke anything.  If you instrument your
code with unit tests at too fine grain a level, every code change will
require a test change.  That gets boring quickly.

So I guess I would argue that it's a feature :-)
If you are interested, how about some specifics on what doesn't work.

I don't think there is any "motivation" behind the observed
restrictions.  Certainly there are limitations of the current S4
implementation, but I do not believe anyone has sat down and said,
"let's make this particular thing not work for people wanting to do
unit testing".


+ seth
#
Hi Seth,

Quoting Seth Falcon <sfalcon at fhcrc.org>:
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 '=='
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 ;-)

H.
#
hpages at fhcrc.org writes:
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?
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