assertRaises doesn't catch errors - python

I have this script
import unittest,itertools,random
##testclass
class Testcomb(unittest.TestCase):
def test_input(self):
self.assertRaises(TypeError,calculate_combinations,dict(comb1), 5)
def calculate_combinations(combin,target):
counter = 0
for L in range(0, len(combin)+1):
for subset in itertools.combinations(combin, L):
if sum(subset) == target: counter= counter+1
return counter
comb1=[1,2,3,4]
if __name__=='__main__': unittest.main()
but the self.assertRaises(TypeError,calculate_combinations,dict(comb1), 5) does not intercept the exception giving me this error:
E..
======================================================================
ERROR: test_input (__main__.Testcomb)
----------------------------------------------------------------------
Traceback (most recent call last):
File "total_combination.py", line 25, in test_input
self.assertRaises(TypeError,calculate_combinations,dict(comb1), 5)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (errors=1)
Can anyone help?

The exception that makes your test fail is triggered by the dict(comb1) part of the assertion.
>>> comb1=[1,2,3,4]
>>> dict(comb1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
On the contrary, the assertRaises will return True only if it is the callable (in your case calculate_combinations) to trigger it.
HTH!

Related

How i fix Traceback (most recent call last):

Hello I tried to fix this but I couldn't
This is my code:
import random
Cromosomas=[[random.randint(1, 1010) for i in range(3)] for j in range(800)]
Z=[]
Fitness=[]
P=[]
C=[]
Padres=[]
def funcOb(Arreglo):
return 0.2*Arreglo[0]**2+0.08*Arreglo[1]**2+0.18*Arreglo[2]**2+0.1*(Arreglo[0]*Arreglo[1])+0.04*(Arreglo[0]*Arreglo[2])+0.06*(Arreglo[1]*Arreglo[2])-0.14*Arreglo[0]-0.11*Arreglo[1]-0.1*Arreglo[2]+120+abs(Arreglo[0]+Arreglo[1]+Arreglo[2]-1000)-10
for i in range(0,800):
Z.append(funcOb(Cromosomas[i]))
for i in range(0,800):
Fitness.append(1/(1+Z[i]))
total=sum(Fitness)
for i in range(0,800):
P.append(Fitness[i]/total);
A=0
for i in P:
A=A+i
C.append(A)
R=[random.random()for i in range(800)]
for i in range(0,800):
for j in range(0,800):
if R[i]<=C[j]:
Cromosomas[i]=Cromosomas[j]
break
def mutar(Arreglo1,Arreglo2):
b=random.randint(0, 2)
if Arreglo1[b]<=Arreglo2[b]:
Arreglo2[b]=Arreglo1[b]
return Arreglo2
if Arreglo1[b]>=Arreglo2[b]:
Arreglo1[b]=Arreglo2[b]
return Arreglo1
for i in range(0,800):
r=random.random()
if r<(0.3):
n=Cromosomas[i]
if n not in Padres:
Padres.append(n)
r=0
for i in range(0,len(Padres)-1):
if len(Padres)==1:
Cromosomas[i]=[Padres[i][0],Padres[i][1],Padres[i][2]]
else:
lista=mutar(Padres[i],Padres[i+1])
Cromosomas[i]=[lista[0],lista[1],lista[2]]
for i in range(0,480):
Cromosomas[Cromosomas==random.randint(1, 2400)]=random.randint(1, 800)
for m in range(0,800):
print(funcOb(Cromosomas[m]))
error:
Traceback (most recent call last):
File "main.py", line 59, in <module>
print(funcOb(Cromosomas[m]))
File "main.py", line 10, in funcOb
return 0.2*Arreglo[0]**2+0.08*Arreglo[1]**2+0.18*Arreglo[2]
**2+0.1*(Arreglo[0]*Arreglo[1])+0.04*(Arreglo[0]*Arreglo[2])+0.
06*(Arreglo[1]*Arreglo[2])-0.14*Arreglo[0]-0.11*Arreglo[1]-0.1*
Arreglo[2]+120+abs(Arreglo[0]+Arreglo[1]+Arreglo[2]-1000)-10
TypeError: 'int' object has no attribute '__getitem__'
This error occurs when I try to run the last function of my code and I have no idea why the function that I call at the end is already one that I had already used and it does not give me a problem, also if I try other methods such as defining another function appears the same mistake.
Thank´s for help

Python 3/Doctest: Exception is not evaluated as expected result

I have a python module containing (amongst other functions) this piece of code:
def check_color_range(*argv):
"""
Abbreviated documentation and other tests.
>>> check_color_range(23, -1, 99, 10000)
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
"""
for c in argv:
if c < 0 or c > 255:
raise TypeError('Falscher Farbwert ' + str(c) + '!')
When I run this using doctest like so: python -m doctest -v demo.py, I get the following output:
Trying:
check_color_range(23, -1, 99, 10000)
Expecting:
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
**********************************************************************
File "C:\az\code_camp_python\src\EigeneProgramme\Tag3\arcade_base\demo.py", line 5, in demo.check_color_range
Failed example:
check_color_range(23, -1, 99, 10000)
Expected:
Traceback (most recent call last):
...
TypeError: Falscher Farbwert -1!
Got:
Traceback (most recent call last):
File "C:\az\miniconda3\envs\py37\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest demo.check_color_range[0]>", line 1, in <module>
check_color_range(23, -1, 99, 10000)
File "C:\az\code_camp_python\src\EigeneProgramme\Tag3\arcade_base\demo.py", line 12, in check_color_range
raise TypeError('Falscher Farbwert ' + str(c) + '!')
TypeError: Falscher Farbwert -1!
1 items had no tests:
demo
**********************************************************************
1 items had failures:
1 of 1 in demo.check_color_range
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
For me the expected and the actual Errors look the same, but I may be missing something. I already compared whitespace etc., which seems to be the same.
I then tried to paste the complete Traceback from the "Got:" section into the testcase - and I'm still get the failed test, so I guess I must be doing something wrong.
I'd be very happy, if you could give me a heads-up.
On line 8 you have: TypeError: Falscher Farbwert -1!____ (4 blank spaces at the end)
You should replace it with: TypeError: Falscher Farbwert -1!

Doctest fails when normal output and exception mixed together?

Does doctest support that both output and exception mixed together?
One example is:
>>> def foo():
... print 'hello world!'
>>> foo()
hello world!
>>> def bar():
... raise Exception()
>>> bar()
Traceback (most recent call last):
...
Exception
>>> def foo_bar():
... foo()
... bar()
>>> foo_bar()
hello world!
Traceback (most recent call last):
...
Exception
I expect all three cases should be successful, but only two of them does, see
$ python -m doctest -v /tmp/1.py
Trying:
def foo():
print 'hello world!'
Expecting nothing
ok
Trying:
foo()
Expecting:
hello world!
ok
Trying:
def bar():
raise Exception()
Expecting nothing
ok
Trying:
bar()
Expecting:
Traceback (most recent call last):
...
Exception
ok
Trying:
def foo_bar():
foo()
bar()
Expecting nothing
ok
Trying:
foo_bar()
Expecting:
hello world!
Traceback (most recent call last):
...
Exception
**********************************************************************
File "/tmp/1.py", line 16, in 1
Failed example:
foo_bar()
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python2.7/doctest.py", line 1315, in __run
compileflags, 1) in test.globs
File "<doctest 1[5]>", line 1, in <module>
foo_bar()
File "<doctest 1[4]>", line 3, in foo_bar
bar()
File "<doctest 1[2]>", line 2, in bar
raise Exception()
Exception
**********************************************************************
1 items had failures:
1 of 6 in 1
6 tests in 1 items.
5 passed and 1 failed.
***Test Failed*** 1 failures.
The docs say you can't do that:
Examples containing both expected output and an exception are not supported. Trying to guess where one ends and the other begins is too error-prone, and that also makes for a confusing test.
Regular output and tracebacks cannot be mixed since they are just indistinguishable text. However you can wrap the block, catching the exception you expect:
>>> try:
... foo_bar()
... except TheSpecificExceptionYouWant:
... pass
... else:
... raise AssertionError('Should have raised an exception')
hello world!

error in function: 'str' object is not an iterator

I have a problem with the following function in python (where swap is a function that I have previously created and that works fine):
def swap (cards):
"""
>>> swap('FBFFFBFFBF')
'BFBBBFBBFB'
>>> swap('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFBB'
>>> swap('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'BBFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFB'
"""
invert=""
for i in cards:
if i is "B":
invert+="F"
else:
invert+="B"
return (invert)
def swap2 (cards):
"""
>>> next('FBFFFBFFBF')
'FFBBBFBBFF'
>>> next('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFFF'
>>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
"""
indices=""
for pos, i in enumerate(cards):
if i =="B":
indices+=str(pos)
first= int(indices[0])
last= int(indices[-1])
prefix= cards [:first]
middle= cards [first:last+1]
suffix= cards [last+1:]
middle2=swap(middle)
return (prefix+middle2+suffix)
def turns (cards):
"""
>>> turns('FBFFFBFFBF')
3
>>> turns('BFFBFBFFFBFBBBFBBBBFF')
6
>>> turns('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
14
"""
turn=0
while cards != 'F'*len(cards):
cards=swap2(cards)
turn+=1
return (turn)
if __name__ == '__main__':
import doctest
doctest.testmod()
when I run this function it works fine but if I use doctest to see if there are mistakes it tells me:
TypeError: 'str' object is not an iterator
I don't know where this error comes from.
Can anyone help me?
complete output of the doctest:
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 25, in __main__.swap2
Failed example:
next('FBFFFBFFBF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[0]>", line 1, in <module>
next('FBFFFBFFBF')
TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 27, in __main__.swap2
Failed example:
next('BFFBFBFFFBFBBBFBBBBFF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[1]>", line 1, in <module>
next('BFFBFBFFFBFBBBFBBBBFF')
TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 29, in __main__.swap2
Failed example:
next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[2]>", line 1, in <module>
next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
TypeError: 'str' object is not an iterator
def swap2 (cards):
"""
>>> next('FBFFFBFFBF')
'FFBBBFBBFF'
>>> next('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFFF'
>>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
"""
# …
The function is called swap2 but within the doctests, you are using next which happens to be a built-in function that does something completely different. That’s why you are seeing that error.
At times like this, it’s really important to actually look at the error messages. It clearly told you what was called:
File "<doctest __main__.swap2[0]>", line 1, in <module>
next('FBFFFBFFBF')
So if you don’t know where that was supposed to come from, then check out the error message. Doctest will tell you what it is executing: swap2[0], swap2[1], etc. tells you the function name the docstring that is being executed is by doctest and which test case it is (0 is the first, 1 the second etc.). It even gives you the line number (within the doctest case) where the error appeared, and of course the line that was causing the error. So use that information to go to the problematic code, and figure out what the problem is.

How can I hide my stack frames in a TestCase subclass?

I want to add a custom assert method to a TestCase subclass. I tried to copy my implementation from the unittest module so that it would match the behaviour of the regular TestCase as closely as possible. (I would prefer to just delegate to self.assertEqual() but this causes even more backtrace noise, see below.) The unittest module seems to automatically hide some internal details of its implementation when reporting failed assertions.
import unittest
class MyTestCase(unittest.TestCase):
def assertLengthIsOne(self, sequence, msg=None):
if len(sequence) != 1:
msg = self._formatMessage(msg, "length is not one")
raise self.failureException(msg)
class TestFoo(MyTestCase):
seq = (1, 2, 3, 4, 5)
def test_stock_unittest_assertion(self):
self.assertEqual(len(self.seq), 1)
def test_custom_assertion(self):
self.assertLengthIsOne(self.seq)
unittest.main()
The output of this is as such:
amoe#vuurvlieg $ python unittest-demo.py
FF
======================================================================
FAIL: test_custom_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unittest-demo.py", line 16, in test_custom_assertion
self.assertLengthIsOne(self.seq)
File "unittest-demo.py", line 7, in assertLengthIsOne
raise self.failureException(msg)
AssertionError: length is not one
======================================================================
FAIL: test_stock_unittest_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unittest-demo.py", line 13, in test_stock_unittest_assertion
self.assertEqual(len(self.seq), 1)
AssertionError: 5 != 1
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=2)
Note that the custom assert method causes a stack trace with two frames, one inside the method itself, whereas the stock unittest method only has one frame, the relevant line in the user's code. How can I apply this frame-hiding behaviour to my own method?
This question was answered by Peter Otten on comp.lang.python.
Move MyTestCase in a separate module and define a global variable __unittest = True.
$ cat mytestcase.py
import unittest
__unittest = True
class MyTestCase(unittest.TestCase):
def assertLengthIsOne(self, sequence, msg=None):
if len(sequence) != 1:
msg = self._formatMessage(msg, "length is not one")
raise self.failureException(msg)
$ cat mytestcase_demo.py
import unittest
from mytestcase import MyTestCase
class TestFoo(MyTestCase):
seq = (1, 2, 3, 4, 5)
def test_stock_unittest_assertion(self):
self.assertEqual(len(self.seq), 1)
def test_custom_assertion(self):
self.assertLengthIsOne(self.seq)
if __name__ == "__main__":
unittest.main()
$ python mytestcase_demo.py
FF
======================================================================
FAIL: test_custom_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "mytestcase_demo.py", line 11, in test_custom_assertion
self.assertLengthIsOne(self.seq)
AssertionError: length is not one
======================================================================
FAIL: test_stock_unittest_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "mytestcase_demo.py", line 8, in test_stock_unittest_assertion
self.assertEqual(len(self.seq), 1)
AssertionError: 5 != 1
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=2)
$

Categories

Resources