How best to initialize in unit tests?

Steve D'Aprano steve+python at pearwood.info
Wed Oct 4 11:52:40 EDT 2017


On Thu, 5 Oct 2017 02:23 am, Skip Montanaro wrote:

[...]
>> Ew. Sounds like a badly designed package.
> 
> I will refer interested readers to the nose documentation:
> 
>
https://nose.readthedocs.io/en/latest/doc_tests/test_init_plugin/init_plugin.html
> 
> where the author writes, "Many applications, especially those using
> web frameworks like Pylons or Django, can’t be tested without first
> being configured or otherwise initialized."

I haven't used Pylons or Django, but frameworks are a different story to
libraries, which is what I understood you to be discussing.

In any case, I would expect any decent framework to automatically read from a
configuration file, so that importing the framework automatically initialises
it as well (provided it can find a configuration file, and if not, it should
noisily complain unless explicitly silenced).


> My system, while not a web framework, does still require the caller of
> the client libraries (both Python and R, as it happens) to tell the
> system what server to contact.

And you do that globally? What if you want to talk to two servers?

Anyway, whatever your reasons (good, bad or indifferent) for designing the
system the way it is, you have to deal with it as it exists, not the way I
would prefer it. Did you read my suggestion to just put the initialise call
immediately after the import?

import system
system.initialise(server='foo')


You can certainly do that with unittest; doing so in doctest requires a bit
more nuance and care, but is still possible.

If this doesn't solve your problem, I'm afraid I don't understand your
problem.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list