unittest: How to fail if environment does not allow execution?

K

Kai Grossjohann

I wrote a test case that depends on a certain file existing in the
environment. So, I guess I should test that the file exists in the
setUp method. But what if it doesn't exist? How do I fail in that case?

I would like to emit an error message explaining what is wrong.

tia,
Kai
 
R

Roy Smith

Kai Grossjohann said:
I wrote a test case that depends on a certain file existing in the
environment.

In theory, unit tests should not depend on any external factors, but
we all know the difference between theory and practice, right?
So, I guess I should test that the file exists in the
setUp method. But what if it doesn't exist? How do I fail in that case?

def setUp (self):
try:
open ("myEssentialTestFile")
except IOError:
self.fail ("Hey, dummy, myEssentialTestFile is missing!")
 
K

Kai Grossjohann

Roy said:
In theory, unit tests should not depend on any external factors, but
we all know the difference between theory and practice, right?

:) I am trying to figure out whether a message is logged by syslog.
Not sure how I would do that except require the user to configure syslog
to log foo messages to the /var/log/foo file and to then check that
the message is written.
def setUp (self):
try:
open ("myEssentialTestFile")
except IOError:
self.fail ("Hey, dummy, myEssentialTestFile is missing!")

Thank you, sir. I didn't realize that I can fail like that from the
setUp method, as well. Works like a charm.

Why did you use the open/IOError combination, instead of
os.stat/OSError? I am using the latter. But I don't know what I'm doing...

Kai
 
K

Kent Johnson

Kai said:
I wrote a test case that depends on a certain file existing in the
environment. So, I guess I should test that the file exists in the
setUp method. But what if it doesn't exist? How do I fail in that case?

I would like to emit an error message explaining what is wrong.

I would just use the file normally in the test. If it's not there you
will get an IOError with a traceback and a helpful error message.

Kent
 
R

Roy Smith

Kai Grossjohann said:
I am trying to figure out whether a message is logged by syslog.
Not sure how I would do that except require the user to configure syslog
to log foo messages to the /var/log/foo file and to then check that
the message is written.

Well, I suppose that depends on what exactly it is that you're trying to
test. Are you testing the operation of the syslog system itself, or are
you testing that *your program* calls syslog at the appropriate time? If
the latter, then you might consider writing your own test stub replacement
for the syslog module that looks something like this:

history = []

def clearHistory():
global history
history = []

def syslog (arg1, args=None):
history.append ((time.ctime(), arg1, arg2))

Arrange to have this module be found in your PYTHONPATH before the standard
one. Have your setUp() method call clearHistory(). Each test case that
should write something to syslog can interrogate syslog.history to see if
the appropriate thing was added to the list.

There is so much that can go wrong with syslog that it's hard to do any
deterministic testing using it. The syslogd could crash. It could be
configured wrong (or in a way which forwards all local syslog messages to
another machine). Or, the UDP messages could just plain get dropped by the
network without a trace. This is not the kind of stuff you want to be
trusting for a unit test.
Why did you use the open/IOError combination, instead of
os.stat/OSError? I am using the latter. But I don't know what I'm doing...

I simply tried opening a non-existent file and saw what exception I got:
Traceback (most recent call last):

I suppose it would have been smarter to have read the docs, but I'm lazy.
Larry Wall says that's a good thing in programmers :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top