Unfortunately Codeception has no events for test files itself.
E.g. I want to cleanup a DB not before a suite nor before each test, but before each test class or each cept test file. Now I have a DB cleanup only for a huge suite of tests... It is very uncomfortable to maintain fixtures for such tests. (I'm testing an Yii2 application)
you can use setUpBeforeClass() and tearDownAfterClass() or use $this->getModule('DB') to perform clean - up
Maybe you can get along with GroupObjects? You just mark all tests by group and listen to events only for that group. http://codeception.com/docs/08-Customization#Group-Objects
@Ragazzo setUpBeforeClass() is a method of a \PHPUnit_Framework_TestCase class.
I don't know the way I can use it in functional or acceptance tests.
@DavertMik It is a some kind of an acceptable workaround for me. But I believe that the lack of such events is a systemic weakness of the Codeception. And it shows up in Yii2 default tests implementation.
I'm using this implementation and become faced with problems that I can't write a lot of tests because of fixtures reloaded only for the whole functional suite. I can't maintain tests to be granular and isolated. I have a lot of workarounds such as dynamic fixtures creation and a lot of code that can't be tested because DB state depends on executed tests number, tests order etc.
My application has a not very complicated business logic but it become untestable now. And this all is about a lack of some kind of this events.
@TriAnMan you can use before and after afaik it is called correctly in cests , as show in yii2-basic application
@Ragazzo no, I can't.
before and after are called for each test, not before and after a cest class. And I need a similar behavior for cept files. E.g. an event beforeFile fires before all methods of the cept are called and before cest file execution. So the proposed event sequence is: beforeSuite, beforeFile, before, beforeStep, afterStep, after, afterFile, afterSuite in terms of the \Codeception\Module class.
@Ragazzo I can't because I want to rely on results of some previous tests in a cept file. Now for such purpose I can only do a DB cleanup on beforeSuite and afterSuite events. This approach (used by default in Yii2) leads to an untestable application. And in this situation I am now.
@DavertMik I've closely read the Group Objects doc. And it is not a workaround for my problem.
For groups, events are fired in the same behavior as for all other tests i.e. there is no event that will be fired at the start/and of a cest class or cept file only.
@Ragazzo by the way I believe that Yii2 functional and acceptance tests must do a cleanup more frequently than once at the suite start. But a cleanup before each test is very frequent and blocks ability to use http://codeception.com/docs/07-AdvancedUsage#BeforeAfter-Annotations to make some sequential tests.
I can't because I want to rely on results of some previous tests in a cept file
use cests, and structure your tests in one public method but with protected methods. Also rely on previous test is bad, since tests should be done in isolation and should not depend on each other and so on.
But a cleanup before each test is very frequent and blocks ability to use
no, you should not make them depend on each other. The idea was that in beforeSuite / afterSuite we only load schema, or some additional data that should be present while all tests are being executed. Your test-case should make clean-up after it by itself.
Maybe additional events can be added for cests, like it is done in phpunit test case, @DavertMik can you handle this and add two events beforeCest and afterCest for cest files, similar to phpunit setUpBeforeClass and tearDownAfterClass ?
can you handle this and add two events beforeCest and afterCest for cest files
It will be great if there will be such method and for cept test files also.
For the glance the places to fire such events are definitions of the addTest(), addCest(), addCept() in the \Codeception\Lib\TestLoader
It will be great if there will be such method and for cept test files also.
as i remember, we had with @DavertMik such discussion long ago about cept files, but it was closed dont remember reason why, maybe we decided that cest files are best suitable for this
@Ragazzo
use cests, and structure your tests in one public method but with protected methods
Looks very "hacky". And the test output will be a mess...
since tests should be done in isolation and should not depend on each other
You are right but sometimes they do depend and it is normal.
The idea was that in beforeSuite / afterSuite we only load schema
Schema is maintained by migrations.
or some additional data that should be present while all tests are being executed
So this approach breaks concepts of isolation and granularity of the tests. (:
And there is no way to control when and what fixtures I want to see in the DB (by default in Yii2).
Unfortunately, the straightforward way of using of yii2-advanced app leads to my problem. (:
However I'm not blaming anyone's code, I'm just telling that there is way to make something better. (:
but it was closed dont remember reason why, maybe we decided that cest files are best suitable for this
Sorry but I'm telling more about \Codeception\Module events... They are fired for all kind of tests.
it is normal.
holywar theme, lets skip it
So this approach breaks concepts of isolation and granularity of the tests
not as i said, some data maybe needed by all tests, but they will not change it, some read-only fixtures
And there is no way to control when and what fixtures I want to see in the DB (by default in Yii2).
you can use getFixture() method from FixtureHelper to load them or in unit tests you can load them as described in docs
Sorry but I'm telling more about \Codeception\Module events... They are fired for all kind of tests.
Ok , it is more to @DavertMik now
you can use getFixture() method from FixtureHelper to load them
Hmm... This can be a workaround. Thanks! But I also have to play with loadFixtures() and getGlobalFixtures() methods. Maybe this approach should be reflected into yii2-app-advanced, shouldn't it?
https://github.com/yiisoft/yii2/issues/5575#issuecomment-60486471
feel free to submit PR , since i dont have enough time for this now
@Ragazzo done: https://github.com/yiisoft/yii2-app-advanced/pull/63
@DavertMik I think that this events are still useful. What I've proposed is only a workaround for this case.
E.g.: I definitely want to have a clean database before each test file. Currently I can do it only for the whole suite or for each test. The yii patch only works around my particular problem, but does nothing with possibility to clean the db up before each test file.
:+1:
@DavertMik could you reopen this issue?
I am also in such a need for something like beforeCest, afterCest hooks and forced to use Cept instead of Cest because of this limitation
I'm also interested in hooks such beforeCest, afterCest.
Me too. I think beforeCest and afterCest fixtures would be a really nice addition.
How long to wait for implementation of beforeCest, afterCest?
Most helpful comment
How long to wait for implementation of beforeCest, afterCest?