473,739 Members | 6,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1485
Kai Grossjohann <ka************ *@verizonbusine ss.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 ("myEssentialTe stFile")
except IOError:
self.fail ("Hey, dummy, myEssentialTest File is missing!")
May 10 '06 #2
Roy Smith wrote:
Kai Grossjohann <ka************ *@verizonbusine ss.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 ("myEssentialTe stFile")
except IOError:
self.fail ("Hey, dummy, myEssentialTest File 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************ *@verizonbusine ss.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
2583
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 testcase-as-a-class model tends to imply a relatively high granularity in testing. A good example of this comes from "Dive Into Python" (http://diveintopython.org) section 7.3. Here, the author has written a module which converts numbers to Roman numerals. The...
3
1956
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 results (verbose mode) somehow. I'm open: logger module, stdmsg redirect, whatever. Thanks.
0
2054
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 TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to TestResult, _TextTestResult and
2
2457
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' self.failUnlessRaises(IntegrityError, self.failhelper(certs.Subcacert, self.ca_client_tmpl))
41
10284
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 new project now and I'm thinking of trying py.test (http://codespeak.net/py/current/doc/test.html). It looks pretty cool from the docs. Is there anybody out there who has used both packages and can give a comparative review?
7
2073
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 old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
18
1871
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 checkin. Recently there was a checkin of a test that _should_ work but doesn't. The discussion got around to means of indicating such tests (because the effort of creating a test should be captured) without disturbing the development flow. ...
0
988
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 import_tests.py"> class PruebasDePrueba(unittest.TestCase): def testUnTest(self): a = 2 b = 1
2
1335
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 initially added some commandline options, e.g. to specify a configuration file like so
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9266
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.