testfixtures icon indicating copy to clipboard operation
testfixtures copied to clipboard

Implement "transactional fixtures" (or "transactional tests")

Open andreynering opened this issue 6 years ago • 3 comments

Originally asked in #23, but I've being thinking about it for some time.

Today, this lib cleans the database, load everything and then commit the changes for every test run. Rails has an option called "transactional fixtures" (or "transactional tests"), where fixtures are loaded once before all tests, and then each test runs inside a transaction. This would allow concurrent running of tests that touch the database.

Needs investigation on implementation and the better public interface to expose this.

Ideas and help would be very much appreciated.

andreynering avatar Mar 24 '18 22:03 andreynering

Would it simply work to just add another function like this

fixtures, err = testfixtures.NewFolderWithTx(tx, &testfixtures.PostgreSQL{}, "testdata/fixtures")

and replace the sql.DB with a sqlTx?

I haven't looked at closely but with my limited go knowledge I suspect it would cause an explosion of code duplication internallly, but I'm mostly thinking out loud about the public API

kswope avatar Mar 25 '18 00:03 kswope

I am currently doing this using this library in conjunction with go-txdb i have a shell script that runs my tests, and i load the fixtures once using the testfixtures cli, and then run go test ./...

I would be interested in seeing a go API to do this as well though.

KoduIsGreat avatar Feb 12 '20 21:02 KoduIsGreat

@KoduIsGreat I don't think there's a reason to specifically use the CLI to load fixtures. You could use the library to load fixtures once, and then use go-txdb to wrap each test in a transaction.

andreynering avatar Feb 14 '20 00:02 andreynering