[Grok-dev] want to use Utility for test - but where to put it?
Brandon Craig Rhodes
brandon at rhodesmill.org
Wed Aug 8 08:56:19 EDT 2007
Yesterday I puzzled over how to build tests for my application that
accesses objects from our relational database. After some discussion,
I realized that I wanted to provide the database as a utility! (When
I went to check, the Zope Book actually lists database access as the
very first example of a reason why you would want a utility!)
So if I define "IMageDB" as an interface that lets the caller search
for and create database objects, then my code that needs to access
objects can run:
>>> db = getUtility(IMageDB)
>>> p = db.Person('900010011')
>>> print p.name
When my Grok instance actually runs, I want to provide a "real"
IMageDB utility, "MageDB", that actually connects to the database.
But when doing tests, I do not want to have to create and populate a
whole database, so I want to register a "FakeMageDB" utility that
keeps everything in RAM so that I can create a few objects for each
test that will go away when the test finishes running.
This leads to some questions.
Where can I put modules that are not tests, but are used to support
tests? I want a "fakedb.py" module that provides the RAM-only IMageDB
utility, that tests can import, but that will *not* be imported when I
run the main application. I had thought that putting it in the
"tests" directory would be enough to avoid its being grokked at
instance runtime; but I was wrong!
It looks like Grok currently loads every module that does not begin
with "test" when running an instance! This surprised me. I had not
expected things put in the "tests" module to be available at instance
runtime. Furthermore, this gives me nowhere to "hide" alternate
implementations of utilities for testing purposes.
I did try putting my utility in a module named "test_fakedb.py",
thinking that I would have my tests "import tests.test_fakedb", and
that the "test" name would hide it from the instance. But since this
makes it look like a test, the test runner gives the error:
AttributeError: 'module' object has no attribute 'test_suite'
I could add an empty test at the bottom of "test_fakedb.py", of
course, but that seems unforgivably kludgey!
Could Grok adopt the rule that the instance ignores modules inside of
Or am I approaching this wrong to begin with?
Brandon Craig Rhodes brandon at rhodesmill.org http://rhodesmill.org/brandon
More information about the Grok-dev