testfixtures icon indicating copy to clipboard operation
testfixtures copied to clipboard

Provide method to teardown fixture data

Open sproutworks opened this issue 3 years ago • 4 comments

I've run into a situation where my build fails depending on the order of the tests and the nature of their foreign key relationships. I am using the UseAlterConstraint method with Postgres, and not running tests in parallel.

For example:

Test 1 creates fixtures for Table A & Table B. Table A has a record which references some record in Table B. Test 2 only creates fixtures in Table B. Because there is leftover data in table A from test 1, now test 2 fails when testfixtures tries to clear Table B.

What I had to do to solve this is to copy the logic to make the constraints deferrable (I am using Postgres). Then I can do a teardown at the end of each test where I delete all the fixture data.

So what I am looking for is a method something like: fixtures.Unload()

This would remove everything created by fixtures.Load(), to avoid potential conflicts with subsequent tests.

sproutworks avatar Mar 12 '21 20:03 sproutworks

I've also run into something similar and would benefit from something like this.

EDIT: I was under the impression this was already happening from:

Before the execution of every test, the test database is cleaned and the fixture data is loaded into the database

so maybe I'm doing something wrong?

EDIT2:

I'm doing:

func TestX(t *testing.T) {
    tests := []testType{ ... }
    for _, test := range tests {
        t.Run(test.Name, func(t *testing.T) {
             prepare()
             ...
        }
    }
}

Balake avatar Apr 14 '21 19:04 Balake

Hi @sproutworks,

I have the impression that the problem you're having is a consequence of how you're using testfixtures. Ideally, you should load all tables before every test.

If you're loading only some tables than surely you can have integrity problems, but I think it's not the library's responsibility to prevent that.

andreynering avatar Oct 30 '21 13:10 andreynering

Hi @sproutworks,

I have the impression that the problem you're having is a consequence of how you're using testfixtures. Ideally, you should load all tables before every test.

If you're loading only some tables than surely you can have integrity problems, but I think it's not the library's responsibility to prevent that.

Hi, @andreynering! We are facing the same issue and would be so glad if there is a possibility to wipe test data after running a test. I hope this PR - https://github.com/go-testfixtures/testfixtures/pull/110 could give us such opportunity. In this case library's user would have a possibility to choose if he needs or not to clear the test data.

masv87 avatar Dec 07 '21 16:12 masv87

@andreynering +1 to this. I think it´s a common use case to have a different set of data for different test scenarios.

Ideally, fixtures should be able to clean themselves after each test.

Is there any chance of #110 to be reviewed and merged?

brpaz avatar Dec 05 '22 18:12 brpaz