473,413 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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

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
May 10 '06 #1
4 1461
Kai Grossjohann <ka*************@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?
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!")
May 10 '06 #2
Roy Smith wrote:
Kai Grossjohann <ka*************@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
May 11 '06 #3
Kai Grossjohann wrote:
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
May 11 '06 #4
Kai Grossjohann <ka*************@verizonbusiness.com> wrote:
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:
open ("/dev/foo") Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: '/dev/foo'


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 :-)
May 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Paul Moore | last post by:
One of the things I really dislike about Unittest (compared, say, to a number of adhoc testing tricks I've used in the past, and to Perl's "standard" testing framework) is that the...
3
by: EricN | last post by:
I like to use unittest. However, when my QA Manager shows up in my cube and says "Give me evidence that all your unit tests passed", I have nothing to provide. I'd like to log the unittest...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
2
by: paul koelle | last post by:
hi folks, while I got my own errors kind of sorted out now (I think), unittest still not behaves like it should. code: print 'this is expected to fail. The "Type" column is unique in the DB'...
41
by: Roy Smith | last post by:
I've used the standard unittest (pyunit) module on a few projects in the past and have always thought it basicly worked fine but was just a little too complicated for what it did. I'm starting a...
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
18
by: Scott David Daniels | last post by:
There has been a bit of discussion about a way of providing test cases in a test suite that _should_ work but don't. One of the rules has been the test suite should be runnable and silent at every...
0
by: =?ISO-8859-1?Q?Israel_Fern=E1ndez_Cabrera?= | last post by:
Hi I'm writing some code that automatically execute some registered unit test in a way to automate the process. A sample code follows to illustrate what I'm doing: <code requires="save as...
2
by: chrisber | last post by:
using the unittest module in Python 2.3.5, I've written some test code that ends up with if __name__ == "__main__": unittest.main() Since I want to run this code in various environments, I'd...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.