Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeception run before all tests inside a CEST

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?

like image 982
Casteurr Avatar asked Apr 27 '16 10:04

Casteurr


People also ask

What is Cest 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.

How do you skip tests in Codeception?

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 .

Does Codeception use selenium?

Codeception is very flexible framework that you can use to write your Selenium tests.


2 Answers

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

like image 58
VK RT Avatar answered Sep 28 '22 05:09

VK RT


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.

like image 36
Naktibalda Avatar answered Sep 28 '22 06:09

Naktibalda