views:

410

answers:

2

Is it possible for the nose unit testing framework to perform tests during the compilation phase of a module?

In fact, I'd like to test something with the following structure:

x = 123
# [x is used here...]
def test_x():
  assert (x == 123)
del x  # Deleted because I don't want to clutter the module with unnecessary attributes

nosetests tells me that x is undefined, as it apparently runs test_x() after importing the module. Is there a way of having nose perform test during the compilation phase while having the module free unnecessary resources after using them?

+2  A: 

A simple way to handle this would be to have a TESTING flag, and write:

if not TESTING:
    del x

However, you won't really be properly testing your modules as the tests will be running under different circumstances to your code.

The proper answer is that you shouldn't really be bothering with manually cleaning up variables, unless you have actually had some major performance problems because of them. Read up on Premature Optimization, it's an important concept. Fix the problems you have, not the ones you maybe could have one day.

Singletoned
Thank you for the reply!I'm not sure how the TESTING variable you suggest fits with automated (nose) tests... Are you suggesting to have a nose setup function that actually sets the variable? I tried to set it in a nose setUp() function, but this did not work.I would not always call deleting unnecessary variables premature optimization: it also tells users that some variables become irrelevant and have no influence on the code that follows.
EOL
@EOL: if you want module variables to remain local to the module, use leading _'s. Or package your tests in a function so the variables are removed when the function exits.
S.Lott
@ S. Lott: thanks! I do use "_"; what I really want is to delete variables that are not used anymore (they are used for constructing class attributes). I don't see how to put them in the test function, as they must be used outside of it (during class construction)… It looks like temporary variables used in class construction and nose test functions don't play well together, right?
EOL
A: 

According to nose's main developer Jason Pellerin, the nose unit testing framework cannot run tests during compilation. This is a potential annoyance if both the module "construction" and the test routines need to access a certain variable (which would be deleted in the absence of tests).

One option is to discourage the user from using any of these unnecessarily saved variables by prepending "__" to their name (this works also for variables used in class construction: they can be one of these "private" globals).

Another, perhaps cleaner option is to dedicate a module to the task: this module would contain variables that are shared by the module "itself" (i.e. without tests) and its tests (and that would not have to be shared were it not for the tests).

The problem with these option is that variables that could be deleted if there were no tests are instead kept in memory, just because it is better for the test code to use them. At least, with the above two options, the user should not be tempted to use these variables, nor should he feel the need to wonder what they are!

EOL