Improve unit test coverage

Adding a unit test for the "trace" decorator to check the
case where the decorated function raises an exception. It
also fixes a typo in a dummy function name.

Change-Id: I1c62e5aa7cbc8d5082d9323f6965c13b3a7be9ff
This commit is contained in:
Vipin Balachandran 2017-08-01 13:30:08 -07:00
parent 08eaf7de63
commit 88c1e8b9d3
1 changed files with 22 additions and 3 deletions

View File

@ -171,7 +171,7 @@ class WithTraceTestCase(test.TestCase):
@profiler.trace("function", info={"info": "some_info"})
def tracede_func(i):
def traced_func(i):
return i
@ -180,6 +180,11 @@ def trace_hide_args_func(a, i=10):
return (a, i)
@profiler.trace("foo", hide_args=True)
def test_fn_exc():
raise ValueError()
class TraceDecoratorTestCase(test.TestCase):
@mock.patch("osprofiler.profiler.stop")
@ -198,11 +203,11 @@ class TraceDecoratorTestCase(test.TestCase):
@mock.patch("osprofiler.profiler.stop")
@mock.patch("osprofiler.profiler.start")
def test_with_args(self, mock_start, mock_stop):
self.assertEqual(1, tracede_func(1))
self.assertEqual(1, traced_func(1))
expected_info = {
"info": "some_info",
"function": {
"name": "osprofiler.tests.unit.test_profiler.tracede_func",
"name": "osprofiler.tests.unit.test_profiler.traced_func",
"args": str((1,)),
"kwargs": str({})
}
@ -223,6 +228,20 @@ class TraceDecoratorTestCase(test.TestCase):
mock_start.assert_called_once_with("hide_args", info=expected_info)
mock_stop.assert_called_once_with()
@mock.patch("osprofiler.profiler.stop")
@mock.patch("osprofiler.profiler.start")
def test_with_exception(self, mock_start, mock_stop):
self.assertRaises(ValueError, test_fn_exc)
expected_info = {
"function": {
"name": "osprofiler.tests.unit.test_profiler.test_fn_exc"
}
}
expected_stop_info = {"etype": "ValueError"}
mock_start.assert_called_once_with("foo", info=expected_info)
mock_stop.assert_called_once_with(info=expected_stop_info)
class FakeTracedCls(object):