Temporary Paths

In this tutorial we will show you how to use the functions {% tmp_path %} and {% tmp_dir %} to create and then reference a temporary file path or directory, respectively. If you want to see how to generate tests you might see the basic tutorial instead. If you haven’t installed gridtest, you should do this first.

Generate Testing Template

Let’s say that we have this script, called “”

# Basic functions for testing custom functions in format {% tmp_path %}

import os

def write_file(filename):
    """write a file with some random nonsense"""
    with open(filename, "w") as filey:
        filey.write("I heard there was an octupus living in that Christmas tree.")

def create_directory(dirname):
    """create a directory named according to input variable dirname"""
    if not os.path.exists(dirname):
    return dirname

Notice that the first function writes a temporary file that comes from an input, and the second creates a directory and returns it. For this gridtest, we will want to test these functions. We thus generated the initial template for the script shown above like this:

$ gridtest generate gridtest.yml
Extracting write_file from temp
Extracting create_directory from temp

The first argument is the input for the generate command, and this can be a filename, a folder name (that might contain multiple scripts) or a python module string (.e.g, requests.get). The second argument is the gridtest output file that will be produced with your tests. After you finish, the “gridtest.yml” file will have a list of tests that you can add values for. You can delete sections that aren’t relevant, or copy paste new entries to each list for another testing case.


You can then open the file in a text editor, and add arguments to each. If your library uses typing, the typing will be checked at testing time, and it’s not specified here. You’ll generally want to fill in args for each testing condition (or leave null for None). For more detail about templating, see the templating documentation. We ultimately updated the template to include the following:

    - args:
        dirname: "{% tmp_dir %}"
      returns: "{{ args.dirname }}"
    - args:
        filename: "{% tmp_path %}"
      exists: "{{ args.filename }}"

The above recipe says that we want to test the function create_directory, and we want gridtest to generate a temporary directory (the variable {% tmp_dir %} and then test that whatever name is generated ({{ args.dirname }}) is returned by the function. For the function write_file, the input “filename” will have a randomly generated temporary file created for it with {% tmp_path %}, and we will test that it exists by referencing it with {{ args.filename }}. Both will also be cleaned up at the completion of the test.

Temp Variables

For each of tmp_path and tmp_dir, you can optionally define a boolean to cleanup, or a prefix. That might look like this:

"{% tmp_dir cleanup=False %}"
"{% tmp_path prefix=mytest %}"

See the function helpers module for more details.


Once we have our testing file and the original script, we can run the tests as follows:

$ gridtest test gridtest.yml 
Name                           Status                         Summary                       
temp.create_directory.0        success                        returns /tmp/gridtest-dir._cfykliv
temp.write_file.0              success                        exists /tmp/gridtest-file-ns9yrb5g

2/2 tests passed

Or since gridtest.yml is the default, just leave it out to find the file in the present working directory:

$ gridtest test

And the directory mentioned, /tmp/gridtest-dir.j0aio0l6 will be cleaned up upon completion. If we don’t want to clean it up, we can add --no-cleanup:

$ gridtest test gridtest.yml --no-cleanup
[2/2] |===================================| 100.0% 
Name                           Status                         Summary                       
temp.create_directory.0        success                        returns /tmp/gridtest-dir.ntgs4pp3
temp.write_file.0              success                        exists /tmp/gridtest-file-x9hw6l12

2/2 tests passed

And then the directory generated would still exist after the run:

$ ls -l /tmp/gridtest-dir.ntgs4pp3
total 0

The same would be true for the testing file.

You might next want to browse tutorials available.