Skip to content

Instantly share code, notes, and snippets.

@echohack
Created May 28, 2013 18:07
Show Gist options
  • Save echohack/5664772 to your computer and use it in GitHub Desktop.
Save echohack/5664772 to your computer and use it in GitHub Desktop.
Jsonschema test validator.
import jsonschema
import json
import nose.tools
def test_schema_generator():
test_setup_data = [
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-validtests.json", "assert_function": nose.tools.assert_true},
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-invalidtests.json", "assert_function": nose.tools.assert_false}]
for test_setup in test_setup_data:
with open(test_setup["schema"]) as schemaf, open(test_setup["tests"]) as testf:
schema = json.loads(schemaf.read())
test_data = json.loads(testf.read())
for test in test_data:
yield validator, test_setup["assert_function"], test, schema
def validator(assert_function, data, schema):
"""
assert_function - function used to assert results
schema_filename - the name of the schema file to run the test against
test_filename - the name of the file with the test
"""
jsonschema.Draft4Validator.check_schema(schema)
jsonvalidator = jsonschema.Draft4Validator(schema)
assert_function(jsonvalidator.is_valid(data), "\n\t" + "\n\t".join(str(error) for error in jsonvalidator.iter_errors(data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment