when JSON data file is missing visibly fail
* creates a fake test function that raises ValueError
This commit is contained in:
9
ddt.py
9
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):
|
||||
|
||||
@@ -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``
|
||||
|
||||
Reference in New Issue
Block a user