Fixtures and testing?

Hello everyone, I’m pretty new at Yii and even newer at fixtures. I’m not really getting what are they and how they works. What I understood was that, the fixtures are called before the tests run, to modify the table in the DB to a known state. During the test you do some changes to the table (add, remove new records) and then, when the test is finished, the DB is restored to the state before the fixture was used. After implementing it, I found that the table is getting modifed according to my fixture, but then, after the test finishes, the table is not restored. I’m I getting wrong how the fixtures work or did I get something missing in their implementation?

Thank you a lot

It’s the expected behavior. The testing framework does nothing to restore the db state after each test.

We need to have a fixture in order to get a certain fixed state of database before doing a test, otherwise we could not run that test repeatedly under the same condition.

Hello, thank you for the relpy. So, I don’t really get how to use the fixtures. I understood that they are used to put the DB in a known state before the test, but then, after the test, I don’t need (don’t want) this state anymore. The solutions are two: I restore the DB manually after each test suite, which for me does not make sense, or I use fixtures reflecting exaclty the table I want to manipulate, which also does not make sense (I don’t want to have fixtures with inside all the data of the DB).

Lets take this scenario as an example: I have a table Users in which I have some user recors, lets say 100 users. I have a controller which provide the actionCreate and actionUpdate, which I want to test. My idea was just to prepare the table with, for example 1 or 0 users, with the fixture. Then through the test I will populate this table by testing the actionCreate and modify this table testing the actionUpdate. Once my tests finishes, I’d like to have my 100 users back in my table. And I was thinking this was the behavior of a fixture, but since this is not the case, it confuses me alot.

The database for testing should be a dedicated one. You have to have a database that you can use as a temporary stuff. You should not use your precious database for production for testing.

The test framework doesn’t care at all about the state of database after the test has been completed. It’s a, say, useless garbage. No one will try to do something with it, except for the fixture of the next test. A fixture can do the following … 1) delete a table with the same name if any, 2) create a table from the scratch, 3) insert some rows to the table if necessary. It may sound rude and rough, but this is the simplest and the most reliable way to provide a certain fixed state of the database.

Hello, this was the missing part the testing DB. I already have a testing DB on which I do my tests and this DB is the exact copy of the real one. So, apparently, if I want to keep it as it is, I have to put all the data in the fixtures.

You can export a table in your production db to a sql, and import it to the testing db in a fixture.

OK thank you very much for the explanation!