# How to write data sets

**URL:** <https://talk.openmrs.org/t/how-to-write-data-sets/28514>\
**Category:** Uncategorized\
**Created:** [May 17, 2020, 9:21am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514 "2020-05-17T09:21:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![herbert24](https://talk.openmrs.org/user_avatar/talk.openmrs.org/herbert24/32/8178_2.png) [@herbert24](https://talk.openmrs.org/u/herbert24)\
**Post date:** [May 17, 2020, 9:21am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/1 "2020-05-17T09:21:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gcliff](https://talk.openmrs.org/user_avatar/talk.openmrs.org/gcliff/32/8783_2.png) [@gcliff](https://talk.openmrs.org/u/gcliff)\
**Post date:** [May 17, 2020, 9:31am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/2 "2020-05-17T09:31:34Z")

</div>

@herbert24 kindly have a look at the already existing datasets in core [https://github.com/openmrs/openmrs-core/tree/4a0feb8da351088f25fdc4e6d324a1f277aa3410/api/src/test/resources/org/openmrs/api/include](https://github.com/openmrs/openmrs-core/tree/4a0feb8da351088f25fdc4e6d324a1f277aa3410/api/src/test/resources/org/openmrs/api/include)

Hope it can be of help

---

<div class="post-metadata">

**Author:** ![herbert24](https://talk.openmrs.org/user_avatar/talk.openmrs.org/herbert24/32/8178_2.png) [@herbert24](https://talk.openmrs.org/u/herbert24)\
**Post date:** [May 17, 2020, 9:37am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/3 "2020-05-17T09:37:23Z")

</div>

thanks let me look through

---

<div class="post-metadata">

**Author:** ![sharif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/sharif/32/11982_2.png) [@sharif](https://talk.openmrs.org/u/sharif)\
**Post date:** [May 17, 2020, 9:46am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/4 "2020-05-17T09:46:19Z")

</div>

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](https://talk.openmrs.org/t/initialising-a-dataset/27925), it has some good information

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [May 17, 2020, 9:52am UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/5 "2020-05-17T09:52:48Z")

</div>

And also hopefully you have looked at [https://wiki.openmrs.org/display/docs/Unit+Tests](https://wiki.openmrs.org/display/docs/Unit+Tests) and this: [https://wiki.openmrs.org/display/docs/Mock+Doc](https://wiki.openmrs.org/display/docs/Mock+Doc)

---

<div class="post-metadata">

**Author:** ![herbert24](https://talk.openmrs.org/user_avatar/talk.openmrs.org/herbert24/32/8178_2.png) [@herbert24](https://talk.openmrs.org/u/herbert24)\
**Post date:** [May 17, 2020, 1:53pm UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/6 "2020-05-17T13:53:38Z")

</div>

Thanks every one for the links,this [https://wiki.openmrs.org/display/docs/Unit+Tests](https://wiki.openmrs.org/display/docs/Unit+Tests) 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

---

<div class="post-metadata">

**Author:** ![ibacher](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ibacher/32/19751_2.png) [@ibacher](https://talk.openmrs.org/u/ibacher)\
**Post date:** [May 18, 2020, 12:36pm UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/7 "2020-05-18T12:36:32Z")

</div>

@herbert24 Personally, I just hand-write the XML, since generally I need far fewer elements than included in an entire database. [The format](http://www.dbunit.org/apidocs/org/dbunit/dataset/xml/FlatXmlDataSet.html) 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](https://github.com/openmrs/openmrs-core/blob/master/api/src/test/resources/org/openmrs/include/initialInMemoryTestDataSet.xml) and [standardTestDataset.xml](https://github.com/openmrs/openmrs-core/blob/master/api/src/test/resources/org/openmrs/include/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
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <table column_1="value" column_2="value"/>
    <table column_1="value" column_2="value"/>
</dataset>

```

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.

---

<div class="post-metadata">

**Author:** ![mogoodrich](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mogoodrich/32/3324_2.png) [@mogoodrich](https://talk.openmrs.org/u/mogoodrich)\
**Post date:** [May 19, 2020, 7:10pm UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/8 "2020-05-19T19:10:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![herbert24](https://talk.openmrs.org/user_avatar/talk.openmrs.org/herbert24/32/8178_2.png) [@herbert24](https://talk.openmrs.org/u/herbert24)\
**Post date:** [May 19, 2020, 9:52pm UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/9 "2020-05-19T21:52:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![herbert24](https://talk.openmrs.org/user_avatar/talk.openmrs.org/herbert24/32/8178_2.png) [@herbert24](https://talk.openmrs.org/u/herbert24)\
**Post date:** [June 15, 2020, 8:57pm UTC](https://talk.openmrs.org/t/how-to-write-data-sets/28514/10 "2020-06-15T20:57:04Z")

</div>

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