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

Kai Grossjohann kai.grossjohann at verizonbusiness.com
Thu May 11 07:28:59 EDT 2006


Roy Smith wrote:
> Kai Grossjohann  <kai.grossjohann at verizonbusiness.com> wrote:
>> 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?

:-)  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.

>> 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!")

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



More information about the Python-list mailing list