Unit Testing
The assert rule
The assert rule checks for error systems or codes in the output of the validator. If the error is there, the assertion will pass as OK. If the error isn't there, it will report an error. You could see it as an error-inverter.
Example
The following rule will feed all resources in the invalid-examples
folder, that end on .missingref.xml
.
And it will make sure that there is an error code of 4005
in the output. The full error for missing references is
http://hl7.org/fhir/dotnet-api-operation-outcome|4005
, but just the code is sufficient in most cases.
- files: /invalid-examples/*.missingref.xml
assert: 4005
# error code for missing references
Approach
The general approach to create unit tests is to put all profiles and extensions in one folder, your good examples in one folder and your failing examples in another. This way you can run the regular validation on all but the failing examples. And run the unit test on those instead of the validator. This is an example of that approach:
- action: validate
files: /conformance-resources/*.*
- action: validate
files: /good-examples/*.json
- assert:
files: /failing-examples/*-cardinality.json