Use a folder named testdata for ancillary files that support tests. The Go tool ignores this directory, keeping its contents available to tests without compiling them as program sources. section 3.1.1 section 8.7
A practical testdata directory can hold:
- JSON fixtures and other structured input files. section 3.1.1
- Files that tests open and parse while exercising application code. section 3.1.1
- Ancillary data that linters and other static analysis tools should also ignore. section 3.1.1
A JSON fixture
Create testdata/bookworms.json with representative book data, then have a test open the file and parse its contents. The directory remains outside the compiled program sources. section 3.1.1
Listing 3.1 testdata/bookworms.json: Example of input file
[
{
"name": "Fadi",
"books": [
{
"author": "Margaret Atwood",
"title": "The Handmaid's Tale"
},
{
"author": "Sylvia Plath",
"title": "The Bell Jar"
}
]
},
{
"name": "Peggy",
"books": [
{
"author": "Margaret Atwood",
"title": "Oryx and Crake"
},
{
"author": "Margaret Atwood",
"title": "The Handmaid's Tale"
},
{
"author": "Charlotte Brontë",
"title": "Jane Eyre"
}
]
}
]
Browse the complete testdata/bookworms.json fixture used for the example. section 3.1.1
