odemarken
odemarken

Reputation: 141

Yii2: loading fixtures for specific functional tests

Short version: Using Yii2 Advanced Application Template and Codeception functional tests, is there a way to load a specific fixture only before a particular scenario (Cest class)?

(Background / side note: I have a vague feeling I might be approaching this wrong, since arguably, if tests should be completely isolated with regards to db, they should be unit tests and not functional ones. However, due to time constraints on the project I started with functional tests while postponing unit testing. I am testing a REST API and have a case which is strongly dependent on the testing data, and fails when the data is modified by other tests.)

One approach I found is to write a new _support\ApiHelper class extending the default FixtureHelper and put the loadFixtures()/unloadFixtures() calls in _before instead of _beforeSuite(). However, this makes all fixtures load before every test, which makes the suite's execution very slow.

Upvotes: 1

Views: 1353

Answers (1)

odemarken
odemarken

Reputation: 141

I found a better approach than the one described in the question: Extend the FixtureHelper class and add a method haveCleanDb which calls unloadFixtures()/loadFixtures(). Then, in the Cest scenario where I need it, I call it in the _before and _after methods: $I->haveCleanDb(). That way, all the other tests are still fast.

Upvotes: 3

Related Questions