Skip to content

Commit

Permalink
Add tests for mapek_framework components (#14)
Browse files Browse the repository at this point in the history
* Add test suite skeleton

* Add `Group` tests

* Fix assert_called() AttributeError

* Interaction TestCase

* Update test dependecies

* ManagedSystem TestCase

* MapeElement TestSuite

* Ticker TestCase
  • Loading branch information
imcatta1 committed Jan 31, 2019
1 parent b872470 commit b5aba1c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mapek_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,6 @@ catkin_install_python(
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
if (CATKIN_ENABLE_TESTING)
catkin_add_nosetests(src/test_mapek_framework/run_tests.py)
endif()
8 changes: 8 additions & 0 deletions mapek_framework/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
<author email="a.cattaneo20@studenti.unibg.it">Andrea Cattaneo</author>

<buildtool_depend>catkin</buildtool_depend>

<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>rostest</build_depend>

<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>

<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>

<test_depend>rospy</test_depend>
<test_depend>std_msgs</test_depend>
<test_depend>python-mock</test_depend>

<export>
</export>
</package>
Empty file.
21 changes: 21 additions & 0 deletions mapek_framework/src/test_mapek_framework/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
import unittest
import rosunit
from .test_group import GroupTestCase
from .test_interaction import InteractionTestCase
from .test_managed_system import ManagedSystemTestCase
from .test_mape_element import MapeElementTestCase
from .test_ticker import TickerTestCase


if __name__ == '__main__':
rosunit.unitrun('test_mapek_framework',
'test_group_mapek_framework', GroupTestCase)
rosunit.unitrun('test_mapek_framework',
'test_interaction_mapek_framework', InteractionTestCase)
rosunit.unitrun('test_mapek_framework',
'test_managed_system_mapek_framework', ManagedSystemTestCase)
rosunit.unitrun('test_mapek_framework',
'test_mape_element_mapek_framework', MapeElementTestCase)
rosunit.unitrun('test_mapek_framework',
'test_ticker_mapek_framework', TickerTestCase)
41 changes: 41 additions & 0 deletions mapek_framework/src/test_mapek_framework/test_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
import mock
from mapek_framework import Group


class GroupTestCase(unittest.TestCase):

@mock.patch('rospy.spin')
@mock.patch('rospy.init_node')
def test_group_spin(self, mock_init_node, mock_spin):
node_name = 'test_node'

mock_monitor_element = mock.Mock()
mock_analyze_element = mock.Mock()
mock_plan_element = mock.Mock()
mock_execute_element = mock.Mock()

knowledge_instance = {}
managed_system_instance = None

class MyGroup(Group):
elements = [
mock_monitor_element,
mock_analyze_element,
mock_plan_element,
mock_execute_element
]
knowledge = knowledge_instance
managed_system = managed_system_instance

my_group_instance = MyGroup(node_name)
self.assertEqual(mock_init_node.call_count, 1)
self.assertEqual(mock_spin.call_count, 0)
mock_monitor_element.assert_called_with(knowledge_instance, managed_system_instance)
mock_analyze_element.assert_called_with(knowledge_instance, managed_system_instance)
mock_plan_element.assert_called_with(knowledge_instance, managed_system_instance)
mock_execute_element.assert_called_with(knowledge_instance, managed_system_instance)

my_group_instance.spin()
self.assertEqual(mock_init_node.call_count, 1)
self.assertEqual(mock_spin.call_count, 1)
14 changes: 14 additions & 0 deletions mapek_framework/src/test_mapek_framework/test_interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
from std_msgs.msg import Empty
from mapek_framework import Interaction


class InteractionTestCase(unittest.TestCase):

def test_interaxction_fields(self):
name = 'foo'
data_class = Empty

interaction_instance = Interaction(name, data_class)
self.assertEqual(interaction_instance.name, name)
self.assertEqual(interaction_instance.data_class, data_class)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import unittest
from mapek_framework import ManagedSystem

class ManagedSystemTestCase(unittest.TestCase):
pass

12 changes: 12 additions & 0 deletions mapek_framework/src/test_mapek_framework/test_mape_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
import mock
from mapek_framework.mape_element import _MapeElement, MonitorElement, AnalyzeElement, PlanElement, ExecuteElement


class MapeElementTestCase(unittest.TestCase):

def test_mape_element_inheritance(self):
self.assertTrue(issubclass(MonitorElement, _MapeElement))
self.assertTrue(issubclass(AnalyzeElement, _MapeElement))
self.assertTrue(issubclass(PlanElement, _MapeElement))
self.assertTrue(issubclass(ExecuteElement, _MapeElement))
15 changes: 15 additions & 0 deletions mapek_framework/src/test_mapek_framework/test_ticker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest
from mapek_framework import Ticker


class TickerTestCase(unittest.TestCase):

def test_ticker_init(self):
node_name = 'foo_name'
topic = 'foo_topic'
rate = 42

ticker_instance = Ticker(node_name, topic, rate)
self.assertEqual(ticker_instance._node_name, node_name)
self.assertEqual(ticker_instance._topic, topic)
self.assertEqual(ticker_instance._rate, rate)

0 comments on commit b5aba1c

Please sign in to comment.