问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/31873977/yii2-loading-fixtures-for-specific-functional-tests