does any know how can I skip the testsuite if there is a exception occurs on setup_class? I have the code like following
import pytest
##pytest.mark.skip(reason='skip if testsuite') #i can put skip here to skip all TestSuite
class TestSuite():
#classmethod
def setup_class(cls):
try:
raise Exception(f'can not find suitable strategy: {e}')
except Exception as a:
#I want to skip the testsuite for all testcase
def test_func1(self):
pass
def test_func2(self):
pass
def test_func3(self):
pass
http://doc.pytest.org/en/latest/skipping.html
You could put this in your exception block:
pytest.skip("failed to complete setup")
Related
try:
context.do_something()
except ValueError:
return False
I do i test this particular code. When i use side effort e.g.
context = mock.MagicMoc()
context.do_something.side_effect = ValueError
When i use pytest.raises, the test passes but the code is not tested.
I have tried using assert but it fails
Any suggestions
I'm assuming you're wrapping the try/except code in a function that you want to test. Here are two options to test this.
1) Use a context manager to check that an exception is raised, after changing your function to re-raise the ValueError (though if you're not going to do anything with it, you might as well just not catch it in the first place):
from unittest import TestCase, mock
def do_something(c):
try:
c.do_something()
except ValueError as e:
raise e
class TestSomething(TestCase):
def test_do_something(self):
context = mock.MagicMock()
context.do_something.side_effect = ValueError
with self.assertRaises(ValueError):
do_something(context)
2) Return True in the successful control path of your function, then just check this condition in your test:
from unittest import TestCase, mock
def do_something(c):
try:
c.do_something()
return True
except ValueError as e:
return False
class TestSomething(TestCase):
def test_do_something(self):
context = mock.MagicMock()
context.do_something.side_effect = ValueError
self.assertTrue(do_something(context))
****** # I want to mock an exception beyond the scope of class Exception in Python. Below is my not-functional code. Please help identifying where's wrong. ******
from mock import patch
import unittest2
class A():
pass
class B(object):
def a(self):
return True
def b(self):
***# This function runs without problem and created an error outside of class Exception***
try:
raise A()
except Exception as ex:
print type(ex).__name__
except:
print 'error outside of Exception is captured.'
def c(self):
# This is for test
try:
self.a()
except Exception as ex:
print type(ex).__name__
except:
print 'error outside of Exception is captured.'
class Test(unittest2)
def raise_error(self):
raise A()
#patch('B.a')
def test_c(self, mock_a):
***# This does not function as B().b(), instead it throws an TypeError Exception. I need to mock an error not inherited from class Exception ***
mock_a.side_effect = self.raise_error
B().c()
Given this code:
try:
#do something
except IOError as message:
logging.error(message)
raise message
I want to test the exception handling part in order to have full coverage.
In the unittest I've tried with:
with patch(new=Mock(side_effect=IOError(errno.EIO))):
self.assertRaises(IOError)
but it doesnt work.
Is this approach correct?
Actually you need to start the Mock so that the side_effectstarts, for example the following:
class Test(unittest.TestCase):
def test(self):
mock = m.Mock()
mock.side_effect = Exception("Big badaboum")
self.assertRaises(Exception, mock)
self.assertRaises can take a callable as second argument, making it equivalent to:
class Test(unittest.TestCase):
def test(self):
mock = m.Mock()
mock.side_effect = Exception("Big badaboum")
with self.assertRaises(Exception):
mock()
And if you want to use it in a test with patch, you can do the following:
import unittest.mock as m
import unittest
def raise_error():
try:
print("Hello") #placeholder for the try clause
except Exception as e:
print(e) #placeholder for the exceptclause
class Test(unittest.TestCase):
#m.patch("__main__.raise_error", side_effect=Exception("Big badaboum")) #replace __main__ by the name of the module with your function
def test(self, mock):
with self.assertRaises(Exception):
mock()
unittest.main()
Edit: And to test the raise of an error inside an except block you need to mock a function call inside the try block you wrote, for instance:
import unittest.mock as m
import unittest
def do_sthing():
print("Hello")
def raise_error():
try:
do_sthing() #this call can be mocked to raise an IOError
except IOError as e:
print(e.strerror)
raise ValueError("Another one")
class Test(unittest.TestCase):
def test(self):
with m.patch("__main__.do_sthing", side_effect=IOError("IOError")):
self.assertRaises(ValueError, raise_error)
unittest.main()
You can use the decorator syntax as well (just putting the test above rewritten to spare some CPU cycle):
class Test(unittest.TestCase):
#m.patch("__main__.do_sthing",side_effect=IOError("IOError"))
def test(self, mock):
self.assertRaises(ValueError, raise_error)
So, I have the most trivial in the world example. This is my class to be tested:
# My_Class.py
class My_Class(object):
#staticmethod
def doit(name, params):
try:
raise Exception("This is my error message")
except Exception:
print("Exception: I raised Exception")
And this is the tester itself:
# test.py
import unittest
from My_Class import My_Class
class Test_MyClass(unittest.TestCase):
def setUp(self):
self.my_class = My_Class()
def test_my_class(self):
name = "Abrakadabra"
params = {}
self.assertRaises(Exception, self.my_class.doit, name, params)
And this is what I see in the console, when I'm running my test.py:
$ nosetests test.py
F
======================================================================
FAIL: test_my_class (test.Test_MyClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File ....
nose.proxy.AssertionError: Exception not raised by doit
-------------------- >> begin captured stdout << ---------------------
Exception: I raised Exception
--------------------- >> end captured stdout << ----------------------
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
It is reaaly iteresting, because it is controversial. On the one hand the test says that "Exception not raised by doit", but one line below it clearly prints out a message from the Exception block. So, what I'm doing wrong here??? Thanks!
To directly answer your question, the reason why you are getting that message is because with this assertion:
self.assertRaises(Exception, self.my_class.doit, name, params)
You are testing to make sure an exception was raised. But your try/except suppresses this. If you actually remove your try/except your test will in fact pass, because now your method will raise.
Since you do not want to do this, what you should be doing instead is testing the behaviour of your method when an exception is raised. Ultimately, you want to make sure that your print method is called in your except. I have put together an example below to help understand this.
Keeping in mind what #user2357112 mentioned, which is very important to keep in mind when unittesting, here is an example to help expand on that to provide a practical use for what you are trying to do:
Let us just put together some method:
def some_method():
pass
We will now put this in to your staticmethod you defined as such:
# My_Class.py
class My_Class(object):
#staticmethod
def doit(name, params):
try:
some_method()
except Exception:
print("Exception: I raised Exception")
So now, when it comes to your unittesting, you want to test the behaviour of your method doit. With that in mind, what you will do in this case, is test that some_method will raise an exception and you will validate how your doit method behaves to that exception being raised.
At this point, I suggest taking a look at the documentation behind unittest and mock to get more familiar with what you can do with your testing, but here is an example using mock patching to test the behaviour of your code if an exception is being raised:
#patch('builtins.print')
#patch('__main__.some_method')
def test_my_class(self, m_some_method, m_print):
name = "Abrakadabra"
params = {}
# have the side_effect raise the exception when some_method is called in doit
m_some_method.side_effect = Exception()
self.my_class.doit(name, params)
# check to make sure you caught the exception by checking print was called
self.assertEqual(m_print.call_count, 1)
When you put it all together, the following is functional code that I ran on my end that you can play around with to understand what is happening:
def some_method():
pass
# My_Class.py
class My_Class(object):
#staticmethod
def doit(name, params):
try:
some_method()
except Exception:
print("Exception: I raised Exception")
# test.py
import unittest
from mock import patch
class Test_MyClass(unittest.TestCase):
def setUp(self):
self.my_class = My_Class()
#patch('builtins.print')
#patch('__main__.some_method')
def test_my_class(self, m_some_method, m_print):
name = "Abrakadabra"
params = {}
m_some_method.side_effect = Exception()
self.my_class.doit(name, params)
self.assertEqual(m_print.call_count, 1)
if __name__ == '__main__':
unittest.main()
assertRaises is an assertion about the function's visible behavior, not its internals. It asserts that the stated exception passes out of the function. Any exceptions that are handled inside the function are not assertRaises's concern.
assertRaises failed since there was actually no exception raised. Well, it was raised but handled with except inside the doit() method. The problem is here:
try:
raise Exception("This is my error message")
except Exception:
print("Exception: I raised Exception")
You are raising an exception and then catching it without re-raising. From a caller (assertRaises is the caller in your case) perspective, no errors were thrown during the function call. Re-raising an exception allows a caller to handle an exception as well. Put a raise after the print:
try:
raise Exception("This is my error message")
except Exception:
print("Exception: I raised Exception")
raise # re-raising
Also see Handling Exceptions.
Here's a generalized example of the code I'm trying to test using mock. I get an AttributeError.
Here's __init__.py:
import logging
log = logging.getLogger(__name__)
class SomeError(Exception):
pass
class Stuff(object):
# stub
def method_a(self):
try:
stuff = self.method_b()
except SomeError, e:
log.error(e)
# show a user friendly error message
return 'no ponies'
return 'omg ponies'
def method_b(self):
# can raise SomeError
return ''
In tests.py I have something like this:
import mock
import unittest
from package.errors import SomeError
from mypackage import Stuff
some_error_mock = mock.Mock()
some_error_mock.side_effect = SomeError
class MyTest(unittest.TestCase):
#mock.patch.object('Stuff', 'method_b', some_error_mock)
def test_some_error(self):
# assert that method_a handles SomeError correctly
mystuff = Stuff()
a = mystuff.method_a()
self.assertTrue(a == 'no ponies')
When running the test, mock raises AttributeError saying: "Stuff does not have the attribute 'method_b'"
What am I doing wrong here?
The decorator first argument should be the class of the object, you mistakenly used a string of the name of the class
#mock.patch.object('Stuff', 'method_b', some_error_mock)
should become
#mock.patch.object(Stuff, 'method_b', some_error_mock)
http://docs.python.org/dev/library/unittest.mock#patch-object