📖 8 min read (~ 1600 words).

File

Asserting OS Files

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 6 functionalities.

DirExists

DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists.

Examples
	assertions.DirExists(t, "path/to/directory")
	success: filepath.Join(testDataPath(),"existing_dir")
	failure: filepath.Join(testDataPath(),"non_existing_dir")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestDirExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestDirExists(t *testing.T)
    	success := assert.DirExists(t, filepath.Join(testDataPath(), "existing_dir"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestDirExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestDirExists(t *testing.T)
    	require.DirExists(t, filepath.Join(testDataPath(), "existing_dir"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }

DirNotExists

DirNotExists checks whether a directory does not exist in the given path. It fails if the path points to an existing directory only.

Examples
	assertions.DirNotExists(t, "path/to/directory")
	success: filepath.Join(testDataPath(),"non_existing_dir")
	failure: filepath.Join(testDataPath(),"existing_dir")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestDirNotExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestDirNotExists(t *testing.T)
    	success := assert.DirNotExists(t, filepath.Join(testDataPath(), "non_existing_dir"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestDirNotExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestDirNotExists(t *testing.T)
    	require.DirNotExists(t, filepath.Join(testDataPath(), "non_existing_dir"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }

FileEmpty

FileEmpty checks whether a file exists in the given path and is empty. It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file.

Examples
	assertions.FileEmpty(t, "path/to/file")
	success: filepath.Join(testDataPath(),"empty_file")
	failure: filepath.Join(testDataPath(),"existing_file")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileEmpty(t *testing.T)
    	success := assert.FileEmpty(t, filepath.Join(testDataPath(), "empty_file"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileEmpty(t *testing.T)
    	require.FileEmpty(t, filepath.Join(testDataPath(), "empty_file"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }

FileExists

FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file.

Examples
	assertions.FileExists(t, "path/to/file")
	success: filepath.Join(testDataPath(),"existing_file")
	failure: filepath.Join(testDataPath(),"non_existing_file")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileExists(t *testing.T)
    	success := assert.FileExists(t, filepath.Join(testDataPath(), "existing_file"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileExists(t *testing.T)
    	require.FileExists(t, filepath.Join(testDataPath(), "existing_file"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }

FileNotEmpty

FileNotEmpty checks whether a file exists in the given path and is not empty. It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file.

Examples
	assertions.FileNotEmpty(t, "path/to/file")
	success: filepath.Join(testDataPath(),"existing_file")
	failure: filepath.Join(testDataPath(),"empty_file")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileNotEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileNotEmpty(t *testing.T)
    	success := assert.FileNotEmpty(t, filepath.Join(testDataPath(), "existing_file"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileNotEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileNotEmpty(t *testing.T)
    	require.FileNotEmpty(t, filepath.Join(testDataPath(), "existing_file"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }

FileNotExists

FileNotExists checks whether a file does not exist in a given path. It fails if the path points to an existing file only.

Examples
	assertions.FileNotExists(t, "path/to/file")
	success: filepath.Join(testDataPath(),"non_existing_file")
	failure: filepath.Join(testDataPath(),"existing_file")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileNotExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileNotExists(t *testing.T)
    	success := assert.FileNotExists(t, filepath.Join(testDataPath(), "non_existing_file"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestFileNotExists(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"path/filepath"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestFileNotExists(t *testing.T)
    	require.FileNotExists(t, filepath.Join(testDataPath(), "non_existing_file"))
    	fmt.Println("passed")
    
    }
    
    func testDataPath() string {
    	return filepath.Join("..", "internal", "assertions", "testdata")
    }


Generated with github.com/go-openapi/testify/codegen/v2