try:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass
py.test (from codespeak.net's svn) has py.test.raises.
i.e.
in test_simple.py::
import py.test
def inverse(x):
return 1/x
def test_half():
assert inverse(2.0) == 0.5
def test_raises():
py.test.raises(ZeroDivisionError, inverse, 0.0)
def test_raises_string():
py.test.raises(ZeroDivisionError, "1.0/0.0")
Surely nose has a similar feature?
--
Regards,
Stephen Thorne
Development Engineer
Scanned by the NetBox from NetBox Blue
(http://netboxblue.com/)
Hi
I have been reading about nose and I find it great.
I have been looking into TG's test and all exceptions related are like this, so say the exception name in the string but what if I must be sure that I get exception type Xtry:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass
I have this, but it will actually fail all the time,
On Aug 4, 2006, at 1:46 AM, Jorge Vargas wrote:Hi
I have been reading about nose and I find it great.
I have been looking into TG's test and all exceptions related are like this, so say the exception name in the string but what if I must be sure that I get exception type Xtry:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass
I have this, but it will actually fail all the time,It fails all the time because you can't get the exception to raise?
What you have above is generally what I do, but if I'm testing for an exception there's usually a way to make the exception come up.
I'm not sure what you mean. That except clause up there will only handle ValueErrors. Anything else that comes up will "error" the test. (Not a failure, mind you, but it still wouldn't pass..)
You could also use a boolean if you want.got_exception = Falsetry:do_something()except NotImplementedError:got_exception = Trueassert got_exception
import nose.tools
@nose.tools.raises(NotImplementedError)
def test_something():
...
I'm not sure if I'm understanding what you're really trying to do,
anyway:
try:
raise NotImplementedError
except NotImplementedError:
assert True,"True passes"
else:
assert False,"but here it crashes"
> python -u assertTest.py
Ciao
Michele
Jorge Vargas wrote:
> On 8/4/06, Kevin Dangoor <dan...@gmail.com> wrote:
> >
> >
> > I'm not sure what you mean. That except clause up there will only handle
> > ValueErrors. Anything else that comes up will "error" the test. (Not a
> > failure, mind you, but it still wouldn't pass..)
> >
>
> because even if it goes in the last assert will make it always fail
> try:
> raise NotImplementedError
> except NotImplementedError:
> assert True,"True passes"
> assert False,"but here it crashes"
>
> >python -u " assertTest.py"
> Traceback (most recent call last):
> File "assertTest.py", line 5, in ?
> assert False,"but here it crashes"
> AssertionError: but here it crashes
> >Exit code: 1
>
I'm not sure if I'm understanding what you're really trying to do,
anyway:
try:
raise NotImplementedError
except NotImplementedError:
assert True,"True passes"
else:
assert False,"but here it crashes"
> python -u assertTest.py
Maybe the attached hack helps. Just make sure you don't keep it inside the path nosetests scans or it will spew a funky TypeError when trying to load the module when looking for tests in it.To use:from TestCaseMod import *test_foo():failUnlessRaises(NotImplementedError, source.create )Disclaimer:Minimally tested (sic)
HTH,Alberto