How to measure the test coverage of a schema? #70
-
Given a schema and a bunch of instances that pass validation: how can I measure which parts of the schema have been "used" or "satisfied", and which (properties, oneOf/anyOf alternatives, ...) would need additional instances to be "hit"? Pretty much what branch coverage means for source code. Which tools are out there that I could use in a build pipeline? That is command-line interface, no GUI. Could this tool be used for this? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
That's a good question. This library doesn't have that functionality out-of-the-box and either does any library I've ever heard of. But, I do expose a lot of low-level components that could be used to build a simple script to get that functionality. It would be trivial to get the list of keyword locations in a schema and the list of keywords that failed validation across a number of schemas. I'd have to think about if there's a way to get a list of locations that passed validation, but if there is, you can easily use those three sets to determine the test coverage for the schema. I'll think about that last part an let you know. |
Beta Was this translation helpful? Give feedback.
-
Thanks, passing
Now I can combine the coverage analysis with the vitest schema tests you wrote for the OpenAPI specification schemas. |
Beta Was this translation helpful? Give feedback.
-
@jdesrosiers The schema coverage measurement worked fine until version 1.11.0 of It now reports zero coverage, so something seems to be broken with collecting visited schema locations in Could you please have a look into what needs to be changed in https://github.com/OAI/OpenAPI-Specification/blob/v3.2-dev/scripts/schema-test-coverage.mjs to make measuring schema coverage work again? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
@jdesrosiers Found it: the signature of
to
and the former How can schema coverage measurement become a standard feature of this package? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
Sorry about the breaking changes. I didn't think I'd be making changes in this area until v2, but inspiration strikes unexpectedly sometimes. Ironically, the change that broke your script is the foundation for providing this functionality in a stable way instead of the dirty hack we came up with. The change allows you to provide an Evaluation Plugin where you can hook into the validation process to do all kinds of things including test coverage. I need to do a little more still to make it easier to use, but ideally the plugin interface should be stable and any changes I make to the internals shouldn't affect your plugin. I'm going to try to prioritize finishing that up and maybe contributing an update to your script. I think test coverage is the perfect example to use to introduce is new feature! |
Beta Was this translation helpful? Give feedback.
There's not a good way to determine keywords that passed validation, but here's a hack I came up with. You can run it as a script that takes two arguments: a path to a schema and a path to a directory of JSON instances. Example:
node coverage.js subject.schema.json test
.