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: 2 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.
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
.