📜 ⬆️ ⬇️

Codeception 1.1 released

The framework for automatic testing of PHP projects Codeception got the first serious update. I had to shovel the whole core, break everything, fix everything and do it so as not to break backward compatibility. As a result, now any PHP code can be used in tests, not just a script, the ability to find elements by XPath has been added, and the ability to use Codeception modules in PHPUnit tests has appeared.

In the comments of one of the past articles, the habrovcankin sqrt asked: “Is it possible to return the result when calling the helper method so that it can be used in a test script?” For example, like this:

$token = $I->grabTextValueFrom('#token'); $I->fillField('token', $token); 

Previously, this could not be done, but in the new version it is possible. Added special commands - grabbers . They all start with the prefix “grab” and are now included in almost all modules.

So now you have to write your helper less often, and most of the tasks can be done immediately in the test. Besides, now in the test you can write any PHP code. It would seem, why it is necessary - even if the test has a clear structure, and there is no need for the developer to write anything. In the absence of strict rules, the test will surely lose readability. But strict rules remain, but the ability to insert PHP simplifies working with fixtures.
')
For example:

 <?php if ($scenario->preload()) { require 'user_data.php' } $I = new TestGuy($scenario); $I->amLoggedAs($user1); $I->click('Log In'); $I->see("Hello, ".$user1->name); if ($scenario->running()) { $user1->delete(); } ?> 

User $ user1 was loaded from user_data.php file and deleted at the end of the test.
Previously, the following strategy was practiced in Codeception: all data is equally loaded and cleared for all tests in a group. Now developers are given more flexibility in working with data.

It is necessary to clarify that when inserting any code, it is necessary to indicate when it should be executed: on the analysis of the script or already during its execution. Actually, this is what the if ($ scenario-> preload ()) and if ($ scenario-> running ()) blocks are needed for.

As an indirect consequence of the conducted refactoring, now in PHPUnit tests you can use Codeception modules . Those. in principle, scripts can be thrown away and write all PHPUnit acceptance tests. Everything will look something like this:

 public function testUserIsLoggedIn() { $user = User::find(1); $user->setPassword('qwerty'); $user->save(); $this->testGuy->amOnPage('/login'); $this->testGuy->submitForm('#login', array('username' => $user->name, 'password' => 'qwerty')); $this->testGuy->see('Hello, '.$user->name); } 

But personally, I would recommend using such features for writing integration tests. Modules for working with a database or ORM Doctrine will be there very much to the point.

And finally, about another innovation. Now tests can find elements by XPath locators. However, no new commands were introduced into existing modules. Where CSS selectors were previously used, XPath locators can now be used.

That's all. Thanks for attention. Test it!
Details of the release in the official blog .

Source: https://habr.com/ru/post/149275/


All Articles