A simple `fs.FS` implementation to be used inside tests.

testfs

A simple fs.FS which is contained in a test (using testing.TB's TempDir()) and with a few helper methods.

PS: This lib only works on Go 1.16+.

Example

func TestSomething(t *testing.T) {
	tmpfs := testfs.New(t)
	testfile := "foo/bar/foobar"
	_ = tmpfs.MkdirAll(filepath.Dir(testfile), 0o764)
	_ = tmpfs.WriteFile(testfile, []byte("example"), 0o644)

	// you can now use tmpfs as a fs.FS...
	// fs.WalkDir(tmpfs, ".", func(path string, d fs.DirEntry, err error) error { return nil })

	// and read files of course:
	bts, _ := fs.ReadFile(tmpfs, testfile)
	fmt.Println(string(bts))
}

Why

The idea is to able to test code that use a fs.FS, without having to, for example, commit a bunch of files inside testdata and without using in-memory implementation that might not do the same thing as a real FS.

This is a real FS, it only limits itself to a temporary directory and cleans after itself once the test is done. You also get a couple of helper methods to create the structure you need.

Other options

If you don't mind testing against an in-memory implementation, the native fstest.MemFS is a good option.

Owner
Carlos Alexandro Becker
Creator @goreleaser; SRE @totvslabs; I try to delete more code than I write.
Carlos Alexandro Becker
Similar Resources

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022

gostub is a library to make stubbing in unit tests easy

gostub gostub is a library to make stubbing in unit tests easy. Getting started Import the following package: github.com/prashantv/gostub Click here t

Dec 28, 2022

Golang application focused on tests

Golang application focused on tests

Maceio Golang application that listens for webhook events coming from Github, runs tests previously defined in a configuration file and returns the ou

Sep 8, 2021

Robust framework for running complex workload scenarios in isolation, using Go; for integration, e2e tests, benchmarks and more! 💪

e2e Go Module providing robust framework for running complex workload scenarios in isolation, using Go and Docker. For integration, e2e tests, benchma

Jan 5, 2023

gomonkey is a library to make monkey patching in unit tests easy

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Jan 4, 2023

How we can run unit tests in parallel mode with failpoint injection taking effect and without injection race

This is a simple demo to show how we can run unit tests in parallel mode with failpoint injection taking effect and without injection race. The basic

Oct 31, 2021

Package has tool to generate workload for vegeta based kube-api stress tests.

Package has tool to generate workload for vegeta based kube-api stress tests.

Nov 22, 2021

Learning go with tests! Also setting up automated versioning via SemVer.

learn-go-with-tests The purpose of this repo is to complete the learn-go-with-test course located here. I am also interested in learning how to automa

Nov 23, 2021

S3 etag tests for golang

S3-etag-tests Quickstart Run docker-compose up. Execute tests in /test with $ go

Dec 16, 2021
Comments
  • fix(ci): workflow

    fix(ci): workflow

    Fixes #3

    You can't "depend" on jobs from another workflow:

    https://github.com/caarlos0/testfs/blob/7cf8b73835889e9d91c9f19003134bc99a1ae282/.github/workflows/goreleaser.yml#L11

    In this PR I've merged the workflows to be able to do that.

    cc. @caarlos0

  • feat: check for out of bound links

    feat: check for out of bound links

    not yet sure this is correct, basically copied the implementation from os.dirFS and added the Lstat and link check... pretty dumb but seems to do the trick.

A simple and expressive HTTP server mocking library for end-to-end tests in Go.

mockhttp A simple and expressive HTTP server mocking library for end-to-end tests in Go. Installation go get -d github.com/americanas-go/mockhttp Exa

Dec 19, 2021
Package for comparing Go values in tests

Package for equality of Go values This package is intended to be a more powerful and safer alternative to reflect.DeepEqual for comparing whether two

Dec 29, 2022
Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs
Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Dec 22, 2022
Record and replay your HTTP interactions for fast, deterministic and accurate tests

go-vcr go-vcr simplifies testing by recording your HTTP interactions and replaying them in future runs in order to provide fast, deterministic and acc

Dec 25, 2022
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Testing framework for Go. Allows writing self-documenting tests/specifications, and executes them concurrently and safely isolated. [UNMAINTAINED]

GoSpec GoSpec is a BDD-style testing framework for the Go programming language. It allows writing self-documenting tests/specs, and executes them in p

Nov 28, 2022
Ruby on Rails like test fixtures for Go. Write tests against a real database

testfixtures Warning: this package will wipe the database data before loading the fixtures! It is supposed to be used on a test database. Please, doub

Jan 8, 2023
Automatically update your Go tests

autogold - automatically update your Go tests autogold makes go test -update automatically update your Go tests (golden files and Go values in e.g. fo

Dec 25, 2022
A next-generation testing tool. Orion provides a powerful DSL to write and automate your acceptance tests

Orion is born to change the way we implement our acceptance tests. It takes advantage of HCL from Hashicorp t o provide a simple DSL to write the acceptance tests.

Aug 31, 2022
Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks,

Dec 30, 2022