I want to run something before all the tests inside a particular Cest, and then clean it up after all tests have run, similar to the setUpBeforeClass and tearDownAfterClass method in PHPUnit.
Is there a method to do something like this in Codeception?
Cest is a common test format for Codeception, it is “Test” with the first C letter in it. It is scenario-driven format so all tests written in it are executed step by step. Unless you need direct access to application code inside a test, Cest format is recommended.
The codeception library is built on top of phpunit, so to skip test there is a function markTestSkipped . Every code after this function will not be executed. It is good to add message explaining why test was skipped. Skipped tests will be marked with capital letter S .
Codeception is very flexible framework that you can use to write your Selenium tests.
I have a crude solution for this problem for now, before Codeception guys give you reliable method for this.
Just create another Actor above all your existing actors (test cases) like this:
class MyCest
{
function _before(AcceptanceTester $I)
{
$I->amOnPage('/mypage.php');
}
public function _after(AcceptanceTester $I)
{
}
function beforeAllTests(AcceptanceTester $I,\Page\MyPage $myPage,\Helper\myHelper $helper){
//Do what you have to do here - runs only once before all below tests
//Do something with above arguments
}
public function myFirstTest(AcceptanceTester $I){
$I->see('Hello World');
}
function afterAllTests(){
//For something after all tests
}
}
You can put the function beforeAllTests as public but not protected nor should start with "_", for it to run before all your tests.
Another bunch of functions which will only run once before all tests begin which should be instantiated in /tests/_support/Helper/Acceptance.php for acceptance and so on. In this you can call the function :
// HOOK: used after configuration is loaded
public function _initialize()
{
}
// HOOK: before each suite
public function _beforeSuite($settings = array())
{
}
For more functions , go to : https://codeception.com/docs/06-ModulesAndHelpers#Hooks
From Codeception point of view, Cest class is just a bunch of Cept scenarios. There is no object scope and no before/after class hooks.
My advice is to use Test format instead and use PhpUnit hooks.
Test format extends PHPUnit_Framework_TestCase so setUpBeforeClass should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With