liveBook · Manning

Testdata directory

As explained in Learn Go with Pocket-Sized Projects

A testdata directory is the conventional Go folder for ancillary test data, and its contents are ignored by the go tool.

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

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines the directory convention and shows a JSON fixture, but does not detail test code or file-loading errors.
Examples
Uses testdata/bookworms.json as a sample file containing readers and books.
What you can do after
Operate/configure: Place ancillary test inputs under testdata so Go tooling excludes them from builds and runs.
How it connects
Connects test fixtures with JSON, the Go tool, compilation, and static analysis.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.