From 67fdeca179031593e7d9ad6bd0b416423d2aaeb2 Mon Sep 17 00:00:00 2001 From: Tong Li Date: Thu, 3 Oct 2013 01:52:18 -0400 Subject: [PATCH] add more test cases to improve the test code coverage #4 In this patch, the following module test coverage improved to 100% ceilometer/alarm/evaluator/__init__ Since the module defines an abstract class, it is important to change the abstract method body from using "pass" to comments so that the test coverage can possibly to be ushed to 100%. If the abstract method body is the "pass" statement, then the test code coverage can not be possibly 100% since abstract class can not be instantiated, thus can not be tested. Change-Id: Icaac1e45a695544d9c22fd5d20b79616d2e4d504 --- ceilometer/alarm/evaluator/__init__.py | 6 +++- tests/alarm/evaluator/test_base.py | 46 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/alarm/evaluator/test_base.py diff --git a/ceilometer/alarm/evaluator/__init__.py b/ceilometer/alarm/evaluator/__init__.py index d417b2a88..512965fdf 100644 --- a/ceilometer/alarm/evaluator/__init__.py +++ b/ceilometer/alarm/evaluator/__init__.py @@ -80,4 +80,8 @@ class Evaluator(object): @abc.abstractmethod def evaluate(self, alarm): - pass + '''interface definition + + evaluate an alarm + alarm Alarm: an instance of the Alarm + ''' diff --git a/tests/alarm/evaluator/test_base.py b/tests/alarm/evaluator/test_base.py new file mode 100644 index 000000000..63d48b8ec --- /dev/null +++ b/tests/alarm/evaluator/test_base.py @@ -0,0 +1,46 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 IBM Corp +# +# Author: Tong Li +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""class for tests in ceilometer/alarm/evaluator/__init__.py +""" +import mock + +from ceilometer.tests import base +from ceilometer.alarm import evaluator + + +class TestEvaluatorBaseClass(base.TestCase): + def setUp(self): + super(TestEvaluatorBaseClass, self).setUp() + self.called = False + + def _notify(self, alarm, previous, reason): + self.called = True + raise Exception('Boom!') + + def test_base_refresh(self): + notifier = mock.MagicMock() + notifier.notify = self._notify + + class EvaluatorSub(evaluator.Evaluator): + def evaluate(self, alarm): + pass + + ev = EvaluatorSub(notifier) + ev.api_client = mock.MagicMock() + ev._refresh(mock.MagicMock(), mock.MagicMock(), mock.MagicMock()) + self.assertTrue(self.called)