From 5b68d6827f2ea4cee385df7b5ec8a42d7e7cb2a9 Mon Sep 17 00:00:00 2001 From: dbs Date: Mon, 9 May 2011 05:19:51 +0000 Subject: [PATCH] Add Python unit testing and coverage report to "make check" If --enable-python is included in the arguments to configure, "make check" runs all Python unit tests using nosetests and generates a testing coverage report for all Python code. The original json_test.py is factored out to provide a separate file for testing osrf.net_obj vs. osrf.json vs. osrf.*, when we eventually get there. Signed-off-by: Dan Scott git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@2244 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/Makefile.am | 3 ++ src/python/osrf/net_obj.py | 2 +- src/python/tests/json_test.py | 29 +------------------ src/python/tests/net_obj_test.py | 61 +++++++++++++++++++++++++++++++++++++++ src/python/tests/test_coverage.py | 30 +++++++++++++++++++ src/python/tests/testobj.py | 11 +++++++ 6 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 src/python/tests/net_obj_test.py create mode 100644 src/python/tests/test_coverage.py create mode 100644 src/python/tests/testobj.py diff --git a/src/python/Makefile.am b/src/python/Makefile.am index 80c4904..577789b 100644 --- a/src/python/Makefile.am +++ b/src/python/Makefile.am @@ -2,6 +2,9 @@ DISTCLEANFILES = Makefile.in Makefile +check: + nosetests --with-coverage --cover-package=osrf + all-local: @echo $@ python @srcdir@/setup.py build diff --git a/src/python/osrf/net_obj.py b/src/python/osrf/net_obj.py index b5ddab7..7b2aad3 100644 --- a/src/python/osrf/net_obj.py +++ b/src/python/osrf/net_obj.py @@ -176,7 +176,7 @@ def parse_net_object(obj): exec(estr) return obj - # dict, but not a registered NetworObject + # dict, but not a registered NetworkObject for key, value in obj.iteritems(): obj[key] = parse_net_object(value) diff --git a/src/python/tests/json_test.py b/src/python/tests/json_test.py index 4ce15f9..3bd78d3 100644 --- a/src/python/tests/json_test.py +++ b/src/python/tests/json_test.py @@ -2,34 +2,7 @@ import sys, os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import osrf.json, osrf.net_obj, unittest - -class TestObject(object): - def __init__(self): - self.int = 1 - self.string = "two" - self.array = [1,2,3,4] - self.dict = {'foo': 'bar', 'key': 'value'} - self.true = True - self.false = False - self.null = None - -class CheckNetworkEncoder(unittest.TestCase): - """Tests the NetworkEncoder JSON encoding extension""" - - def setUp(self): - osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash') - self.testo = TestObject() - self.ne = osrf.json.NetworkEncoder() - - def test_connect(self): - test_json = self.ne.default( - osrf.net_obj.NetworkObject.osrfMessage({ - 'threadTrace' : 0, - 'type' : "CONNECT" - } - ) - ) - self.assertEqual(test_json, {'__p': {'threadTrace': 0, 'type': 'CONNECT'}, '__c': 'osrfMessage'}) +from testobj import TestObject class CheckObjectToJSON(unittest.TestCase): """Tests the osrf.json.to_json() method that converts Python objects into JSON""" diff --git a/src/python/tests/net_obj_test.py b/src/python/tests/net_obj_test.py new file mode 100644 index 0000000..a4017bd --- /dev/null +++ b/src/python/tests/net_obj_test.py @@ -0,0 +1,61 @@ +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + +import osrf.json, osrf.net_obj, unittest +from testobj import TestObject + +class CheckNetworkEncoder(unittest.TestCase): + """Tests the NetworkEncoder JSON encoding extension""" + + def setUp(self): + osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash') + self.testo = TestObject() + self.ne = osrf.json.NetworkEncoder() + + def test_connect(self): + test_json = self.ne.default( + osrf.net_obj.NetworkObject.osrfMessage({ + 'threadTrace' : 0, + 'type' : "CONNECT" + } + ) + ) + self.assertEqual(test_json, {'__p': + {'threadTrace': 0, 'type': 'CONNECT'}, + '__c': 'osrfMessage'} + ) + + def test_connect_array(self): + test_json = self.ne.default( + osrf.net_obj.NetworkObject.osrfMessage({ + 'threadTrace' : 0, + 'type' : "CONNECT", + 'protocol' : "array" + } + ) + ) + self.assertEqual(test_json, {'__p': + {'threadTrace': 0, 'protocol': 'array', 'type': 'CONNECT'}, + '__c': 'osrfMessage'} + ) + + def test_connect_to_xml(self): + test_json = self.ne.default( + osrf.net_obj.NetworkObject.osrfMessage({ + 'threadTrace' : 0, + 'type' : "CONNECT" + } + ) + ) + self.assertEqual( + osrf.net_obj.to_xml(test_json), + "" + "0" + "CONNECT" + "osrfMessage" + ) + + +if __name__ == '__main__': + unittest.main() + diff --git a/src/python/tests/test_coverage.py b/src/python/tests/test_coverage.py new file mode 100644 index 0000000..eca2e85 --- /dev/null +++ b/src/python/tests/test_coverage.py @@ -0,0 +1,30 @@ +""" +Trigger nosetests to give us a complete coverage statement + +nosetests only reports on the modules that are imported in +the tests it finds; this file serves as a placeholder until +we actually provide unit test coverage of the core files. +""" + +import osrf.app +import osrf.cache +import osrf.conf +import osrf.const +import osrf.ex +import osrf.gateway +# Triggers an exception if mod_python is not installed +#import osrf.http_translator +import osrf.json +import osrf.log +import osrf.net_obj +import osrf.net +import osrf.server +import osrf.ses +import osrf.set +import osrf.stack +import osrf.system +import osrf.xml_obj +import unittest + +if __name__ == '__main__': + unittest.main() diff --git a/src/python/tests/testobj.py b/src/python/tests/testobj.py new file mode 100644 index 0000000..e07eafb --- /dev/null +++ b/src/python/tests/testobj.py @@ -0,0 +1,11 @@ +class TestObject(object): + def __init__(self): + self.int = 1 + self.string = "two" + self.array = [1,2,3,4] + self.dict = {'foo': 'bar', 'key': 'value'} + self.true = True + self.false = False + self.null = None + + -- 2.11.0