From 563dc177a398e4bc76f957a8300411901b7a5ebb Mon Sep 17 00:00:00 2001 From: Bulkan Evcimen Date: Wed, 27 Feb 2013 11:23:00 +1100 Subject: [PATCH] when JSON data file is missing visibly fail * creates a fake test function that raises ValueError --- ddt.py | 9 ++++++++- test/test_functional.py | 27 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ddt.py b/ddt.py index c17506a..dc7bb54 100644 --- a/ddt.py +++ b/ddt.py @@ -87,7 +87,14 @@ def ddt(cls): """ cls_path = os.path.abspath(inspect.getsourcefile(cls)) data_file_path = os.path.join(os.path.dirname(cls_path), file_attr) - if os.path.exists(data_file_path): + + def _raise_ve(*args): + raise ValueError("%s does not exist" % file_attr) + + if os.path.exists(data_file_path) is False: + test_name = "{0}_{1}".format(name, "error") + setattr(cls, test_name, feed_data(_raise_ve, None)) + else: data = json.loads(open(data_file_path).read()) for elem in data: if isinstance(data, dict): diff --git a/test/test_functional.py b/test/test_functional.py index afb0183..b02256f 100644 --- a/test/test_functional.py +++ b/test/test_functional.py @@ -1,7 +1,7 @@ import os import json from ddt import ddt, data, file_data -from nose.tools import assert_equal, assert_is_not_none +from nose.tools import assert_equal, assert_is_not_none, assert_raises @ddt @@ -26,6 +26,18 @@ class FileDataDummy(object): return value +@ddt +class FileDataMissingDummy(object): + """ + Dummy class to test the file_data decorator on when + JSON file is missing + """ + + @file_data("test_data_dict_missing.json") + def test_something_again(self, value): + return value + + def test_data_decorator(): """ Test the ``data`` method decorator @@ -139,6 +151,19 @@ def test_feed_data_file_data(): assert_equal(set(values), set([10, 12, 15, 15, 12, 50])) +def test_feed_data_file_data_missing_json(): + """ + Test that a ValueError is raised + """ + tests = filter(is_test, FileDataMissingDummy.__dict__) + + values = [] + obj = FileDataMissingDummy() + for test in tests: + method = getattr(obj, test) + assert_raises(ValueError, method) + + def test_ddt_data_name_attribute(): """ Test the ``__name__`` attribute handling of ``data`` items with ``ddt``