From b38634429c03e0803d3fe53536a0e01d280bad18 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sun, 12 Apr 2009 22:38:46 +0000 Subject: [PATCH] + switch to nose as testing framework - thanks to Beau for patch and motivation (see #300) --- tests/python/test_load_map.py | 108 +++++++++++----------------------- tests/python/test_save_map.py | 90 +++++++++------------------- tests/python/utilities.py | 4 ++ 3 files changed, 65 insertions(+), 137 deletions(-) create mode 100644 tests/python/utilities.py diff --git a/tests/python/test_load_map.py b/tests/python/test_load_map.py index 84b7cc755..6126394d0 100755 --- a/tests/python/test_load_map.py +++ b/tests/python/test_load_map.py @@ -1,91 +1,49 @@ #!/usr/bin/python -from mapnik import * -import sys -import glob +from nose.tools import * +from mapnik import Map, load_map +from utilities import execution_path -def testGood( file ) : - print "Testing good file '" + file + "' ... ", - +import os, sys, glob + +def setup(): + # All of the paths used are relative, if we run the tests + # from another directory we need to chdir() + os.chdir(execution_path('.')) + +# We expect these files to not raise any +# exceptions at all +def assert_loads_successfully(file): m = Map(512, 512) - try: - load_map(m, file, True) - except RuntimeError, what: - print "FAILED" - print what - return False - except: - print "FAILED" - return False - else: - print "OK" - return True + print "Loading %s" % (file) -def testBroken( file ) : - print "Testing broken file '" + file + "' ... ", + strict = True + load_map(m, file, strict) +# We expect these files to raise a UserWarning +# and fail if there isn't one (or a different type +# of exception) +@raises(UserWarning) +def assert_raises_userwarning(file): m = Map(512, 512) - try: - strict = True - load_map(m, file, strict) - except UserWarning, what: - print "OK" - print "=== Error Message =============" - print what - print - return True - except RuntimeError, what: - print "FAILED (not a UserWarning)" - print "=== Error Message =============" - print what - print - else: - print "FAILED" - return False + print "Loading %s" % (file) + strict = True + load_map(m, file, strict) -def test(): - success = 0 - failed = 0 - failed_tests = [] - +def test_broken_files(): broken_files = glob.glob("../data/broken_maps/*.xml") - # eh, can't glob this ... :-) - broken_files.append( "../data/broken/does_not_exist.xml" ) + + # Add a filename that doesn't exist + broken_files.append("../data/broken/does_not_exist.xml") + for file in broken_files: - if testBroken( file ): - success += 1 - else: - failed += 1 - failed_tests.append( file ) + yield assert_raises_userwarning, file +def test_good_files(): good_files = glob.glob("../data/good_maps/*.xml") + for file in good_files: - if testGood( file ): - success += 1 - else: - failed += 1 - failed_tests.append( file ) - - print "=======================================================" - print "Status:", - if failed: - print "FAILED" - print "Errors in: ", failed_tests - else: - print "SUCCESS" - print "Success:", success, "Failed:", failed, "Total:", success + failed - print "=======================================================" - if failed: - return False - else: - return True - - -if __name__ == "__main__": - if test(): - sys.exit( 0 ) - else: - sys.exit( 1 ) + yield assert_loads_successfully, file diff --git a/tests/python/test_save_map.py b/tests/python/test_save_map.py index 359fe9d61..e169a02a3 100755 --- a/tests/python/test_save_map.py +++ b/tests/python/test_save_map.py @@ -1,76 +1,42 @@ #!/usr/bin/python from mapnik import * -import os -import sys -import glob +from nose.tools import * +from utilities import execution_path +import os, sys, glob + +def setup(): + # All of the paths used are relative, if we run the tests + # from another directory we need to chdir() + os.chdir(execution_path('.')) def test(): -# success = 0 -# failed = 0 -# failed_tests = [] - -# TODO: write a better test -# 1. construct map in memory -# 2. save map as XML -# 3. load map to a second object -# 4. Compare both map objects - + # TODO: Write a better test + # 1. Construct map in memory + # 2. Save map as XML + # 3. Load map to a second object + # 4. Compare both map objects map = Map(256, 256) in_map = "../data/good_maps/osm-styles.xml" - print "Loading map '" + in_map + "' ... ", - load_map( map, in_map ) - print "OK" + print "Loading map '%s' ... " % in_map + + load_map(map, in_map) test_map = "test_map.xml" - failed = False; - try: - print "Saving map '" + test_map + "' ... ", - save_map( map, test_map) - print "OK" - except: - print "FAILED" - failed = True; - - if not failed: - new_map = Map(256, 256) - try: - print "Reloading map '" + test_map + "' ... ", - load_map( new_map, test_map) - print "OK" - except UserWarning, what: - print "FAILED" - print "Error: ", what - failed = True; - except RuntimeError, what: - print "FAILED" - print "Error: ", what - failed = True; - except: - print "FAILED" - failed = True; + print "Saving map '%s' ... " % test_map - if not failed and os.path.exists( test_map ): - print "Removing '" + test_map + "'" - os.remove( test_map ) + save_map(map, test_map) + new_map = Map(256, 256) - print "=======================================================" - print "Status:", - if failed: - print "FAILED" + print "Reloading map '%s' ... " % test_map + + load_map(new_map, test_map) + + if os.path.exists(test_map): + print "Removing '%s'" % test_map + os.remove(test_map) else: - print "SUCCESS" - print "=======================================================" - return not failed - - - - - -if __name__ == "__main__": - if test(): - sys.exit( 0 ) - else: - sys.exit( 1 ) + # Fail, the map wasn't written + return False diff --git a/tests/python/utilities.py b/tests/python/utilities.py new file mode 100644 index 000000000..56b4f6ae6 --- /dev/null +++ b/tests/python/utilities.py @@ -0,0 +1,4 @@ +import os, sys, inspect + +def execution_path(filename): + return os.path.join(os.path.dirname(sys._getframe(1).f_code.co_filename), filename)