📜 ⬆️ ⬇️

Codeception - testing in a new way

PHP is a very popular programming language, but testing it is more of an expert prerogative than a vital necessity. Is it really from the fact that PHP developers are all cattle-coders? I think not. Rather, all of the fact that testing systems are sometimes unnecessarily complicated. And tests, on the contrary, should have been extremely simple: easy to read, write, debug, and of course, quickly run. My vision of how this can be translated into PHP resulted in a project called Codeception .

With it, tests for your web applications might look like this:
<?php $I = new TestGuy($scenario); $I->wantTo('create new blog post'); $I->amOnPage('/blog/posts'); $I->click('Create new post'); $I->fillField('Title','Codeception, a new way of testing!'); $I->fillField('Text','Codeception is new PHP full-stack testing framework.'); $I->click('Send'); $I->see('Congratulations, your post is successfully created!'); 


Agree, such a test is clear without additional comments.
And now the most interesting thing: without any changes, this code can be executed as a functional test in the symfony , Symfony2 , Zend Framework frameworks , as well as in the Goutte browser emulator and even through Selenium . Thus, you are offered a single interface for writing functional tests for virtually any site.

In Codeception , you describe the entire test as a script in the simplest possible way. You repeat the actions of the user who uses your web application: clicks on links, fills out forms and expects to see some result. It would be logical to write a test that would accurately reflect his actions. At the same time, the process of writing a test is extremely simple, if you use autocompletion in your IDE:
')


Codeception runs on three pillars:
- how the test environment is used by PHPUnit .
- for acceptance tests - Mink . For him, many thanks to Konstantin Kudryashov everzet .
- and of course, symfony components . They are used for almost everything. Of particular note is BrowserKit, which is used for functional tests.

To get started, set up a Codeception via PEAR:

 $ pear channel-discover codeception.com/pear $ pear install codeception/Codeception 


Now install the necessary dependencies.
 $ codecept install 


Select the project that you want to test and execute in its root command.

 $ codecept bootstrap $ codecept build 


The bootstrap command will create a configuration file and a tests folder for three categories of tests:

- unit
- functional
- acceptance

It is important to note that we distinguish between functional and acceptance tests. The latter require a web server, on which it will be tested using the same Goutte or Selenium. Functional applications run the application within the test itself, as is the case, for example, when testing in Symfony or the Zend Framework.

The easiest way to start is to write an acceptance test. In the tests / acceptance directory, create a new StartPageCept.php file:

 <?php $I = new WebGuy($scenario); $I->wantToTest('front page of my site'); $I->amOnPage('/'); $I->see('A sample text on my site'); 


It checks that there is a text on the main page of your site: 'A sample text on my site'. Enter in see the text that is really there, and your test is ready to run.
But before you start it, remember that acceptance tests require that the site be run inside the web server. So it is necessary to inform the url through which you can access our site. Open the file: tests / acceptance.suite.yml

  config: PhpBrowser: url: '   url  ' 


Now run.

 $ codecept run acceptance 


And you will see something like this:

 Suite acceptance started Trying to see front page of my site (StartPageCept) - Ok Time: 1 second, Memory: 21.00Mb OK (1 test, 1 assertions) 


Now you can start writing more complex tests for your application. For example, a test for authorization on a site might look like this:

 <?php $I = new WebGuy($scenario); $I->wantTo('log in as regular user'); $I->amOnPage('/login'); $I->fillField('Username','davert'); $I->fillField('Password','qwerty'); $I->click('Login'); $I->see('Hello, davert'); ?> 


Read more about writing tests in the documentation .

To perform one of these tests inside the framework, you must copy the test into 'tests / functional', and edit the file test / functional.suite.yml.
I’ll give an example for Symfony2, but in absolutely the same way you can connect Zend Framework or symfony, as well as Doctrine1 or Doctrine2.

 class_name: TestGuy modules: enabled: [Symfony2, TestHelper] 


So we specified the Symfony2 module for use in functional tests. If your AppKernel is standard in the 'app' folder, the test will be performed without additional configuration.

To run functional tests you should use:
 $ codecept run functional 


I'll tell you about the modules. The modules define the actions that are available when writing tests. In the TestGuy or WebGuy script description class, methods taken from several modules are available.
The modules also implement integration with various libraries with almost no configuration and do not require the installation of additional plug-ins or bundles into your application.

Codeception not only executes tests, but also tries to create the necessary environment for testing from scratch. An important part of testing is preparing the database and clearing it.
It is implemented through the module Db. Create a test database and fill it with the data required for testing. You can do it manually or using fixtures. Create a database dump and place it in the tests / _data folder. Then open the configuration file codeception.yml in the root of your project and enter the database access parameters and the dump path for the Db module. Now, if you add a Db module to the list of modules in the tests / acceptance.suite.yml or functional.suite.yml file, the database will be updated automatically after each test.

However, this is not the only way to clean. If you use Doctrine or Doctrine2, then the appropriate modules place all your requests into a transaction and rollback at the end.
Thus, the database is not clogged, and therefore it does not need constant cleaning. Such tests will work very quickly, but transactions are available only in functional and unit tests.

More complete documentation can be found on the official website . There are available as review materials, and descriptions of specific modules.

Codeception is currently in beta testing and your feedback will be highly appreciated.
Follow the project on GitHub .

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


All Articles