How to write data sets

While writing tests,i would like to use data sets as stores for the test data,But couldn’t find better documentation on doing this,could we be having some good links on this? cc @dkayiwa @ibacher @mogoodrich @mseaton

@herbert24 kindly have a look at the already existing datasets in core

Hope it can be of help

1 Like

thanks let me look through

Thanks @gcliff for that link you shared, adding on what @gcliff shared, @herbert24 can you also deeply take a look at this link, Initialising a dataset, it has some good information

1 Like

And also hopefully you have looked at and this:


Thanks every one for the links,this suggests use of CreateInitialDataSet class to create a data set.I access the class and run it as a junit,then i look out for the writen data set for example as per the location here, FlatXmlDataSet.write(initialDataSet, new FileOutputStream(“test/resources/org/openmrs/api/include/LocationServiceTest-attributes.xml”)); unfortunately,i do not get any thing overwriten in LocationServiceTest-attributes.xml.I do have C:\Users\ODEL\AppData\Roaming\OpenMRS\openmrs-runtime file arleady.Could i be missing out some thing on this cc @dkayiwa

@herbert24 Personally, I just hand-write the XML, since generally I need far fewer elements than included in an entire database. The format is really straight-forward, thought it does require some knowledge of the OpenMRS data model (specifically what fields must be filled in and what kind of fields are expected). The test database usually has both the initialMemoryTestDataSet.xml and standardTestDataset.xml data loaded, which gives a pretty good background for tests. From there, it’s a matter of adding the specific rows to specific tables you need in the format:

<?xml version='1.0' encoding='UTF-8'?>
    <table column_1="value" column_2="value"/>
    <table column_1="value" column_2="value"/>

While the CreateInitialDataSet thing might be “easier” in the sense that you don’t have to think too much about the XML format, it will ultimately result in more data than you would generally need and is a bit tricky to use. In particular, the reason it’s likely failing is that if you run it inside a unit test, it’s running against the empty test H2 instance. You need to somehow point it to an actual existing OpenMRS database with the data that you want included in your tests.


Yes, agree with @ibacher, I usually just handrite the test data set… there are a lot of example hand-written data sets in the HTML Form Entry much you could take a look at @herbert24

Take care, Mark


thanks much @ibacher and @mogoodrich,let me checkout the different hand written data sets as i look towards writing a data set made of visit locations with there child locations

@ibacher thanks for the steps here,they got me moving