473,399 Members | 3,656 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,399 software developers and data experts.

How can I test 'warnings' from testsuite?

Hi there,
into a module of mine I 'warn' a message if a certain situation
occurs:
def add_anonymous_user(permissions=('r'):
if 'w' in p:
import warnings
warnings.warn("it's not rencommended assigning 'w'
permission to anonymous user.", RuntimeWarning, stacklevel=2)
I'd like to test such event from test suite ("fail test if warn is not
raised") but don't know how.

Any suggestion?

Sep 10 '07 #1
1 1502
On Sep 10, 7:08 am, billiejoex <gne...@gmail.comwrote:
Hi there,
into a module of mine I 'warn' a message if a certain situation
occurs:

def add_anonymous_user(permissions=('r'):
if 'w' in p:
import warnings
warnings.warn("it's not rencommended assigning 'w'
permission to anonymous user.", RuntimeWarning, stacklevel=2)

I'd like to test such event from test suite ("fail test if warn is not
raised") but don't know how.

Any suggestion?


You can (temporarily) change warnings to exceptions
for the purposes of testing; see filterwarnings in
the warnings module. E.g.,

import warnings
import unittest

def foo():
warnings.warn("Foo", RuntimeWarning, stacklevel=2)

class testWarn(unittest.TestCase):
def setUp(self):
warnings.filterwarnings("error")
def test_1(self):
self.assertRaises(RuntimeWarning, foo)
def tearDown(self):
warnings.resetwarnings()

s = unittest.TestSuite()
s.addTest(unittest.makeSuite(testWarn))

if __name__ == '__main__':
import sys
sys.argv.append('-v')
unittest.TextTestRunner(verbosity=2).run(s)
--
Hope this helps,
Steven

Sep 10 '07 #2

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

Similar topics

3
by: Jan Decaluwe | last post by:
I'm working on a unit test for a finite state machine (FSM). The FSM behavior is specified in a dictionary called transitionTable. It has a key per state with a tuple of possible transitions as...
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...
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...
3
by: codefixer | last post by:
Hi, Does anybody know how to test string library functions ? I have already gone through "gcc-3.4.1/gcc/testsuite/gcc.c-torture/execute" I want to know if their is any other way to test the...
5
by: Sakcee | last post by:
Hi I am trying to use pyUnit to create a framework for testing functions I am reading function name, expected output, from a text file. and in python generating dynamic test functions...
5
by: VvanN | last post by:
hi, fellows I'd like to intruduce a new unit test framework for C++ freely available at: http://unit--.sourceforge.net/ It does not need bothering test registration, here is an example ...
2
by: Podi | last post by:
Hi, Newbie question about unittest. I am having trouble passing a variable to a test class object. MyCase class will potentially have many test functions. Any help would be much...
3
by: rdps | last post by:
I am a newbie and that says it all about posting a basic doubt. I still hope someone helps me out . I am unit testing some modules. My test module is in diff file than the modules to be tested....
0
by: Rob Wilkerson | last post by:
Hey guys - I have a test suite that's working when I include a test case via its absolute path. For portability, I'd rather include it just using its name, but it's failing with a "Failed to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...

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.