|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Codeception 5" |
| 4 | +date: 2022-07-02 01:03:50 |
| 5 | +--- |
| 6 | + |
| 7 | +Codeception 5.0 is out! |
| 8 | + |
| 9 | +This release is **PHP 8+** only, so we are back on track with modern PHP. We are **dropping support for PHPUnit < 9**, and are technically ready for PHPUnit 10. And we also support **Symfony 6** without dropping support of previous Symfony versions. As always, we did our best to keep backward compatibility so if you can update your dependencies, all tests should be working for you. |
| 10 | + |
| 11 | +So let's take a look at new features: |
| 12 | + |
| 13 | +## New Directory Structure |
| 14 | + |
| 15 | +Codeception 5 will match PSR-12 standard. So all tests and classes will have their own namespace `Tests`. The directory structure was updated accordingly: |
| 16 | + |
| 17 | +``` |
| 18 | +tests/ |
| 19 | + _output |
| 20 | + Acceptance |
| 21 | + Functional |
| 22 | + Support/ |
| 23 | + Data/ |
| 24 | + _generated/ |
| 25 | + Helper/ |
| 26 | + Unit/ |
| 27 | +``` |
| 28 | + |
| 29 | +All suite name will have their own namespace, as well as actor and helper classes: |
| 30 | + |
| 31 | +```php |
| 32 | +<?php |
| 33 | + |
| 34 | +namespace Tests\Acceptance; |
| 35 | + |
| 36 | +use \Tests\Support\AcceptanceTester; |
| 37 | + |
| 38 | +class LoginCest |
| 39 | +{ |
| 40 | + public function tryToTest(AcceptanceTester $I) |
| 41 | + { |
| 42 | + $I->amOnPage('/'); |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +New directory structure will be generated by running `codecept bootstrap`. The directory structure is set with a new default config, so the previous directory structure is still valid. |
| 48 | + |
| 49 | +## Attributes |
| 50 | + |
| 51 | +Annotations were an essential part of Codeception testing framework. Even though they were not native language constructs, they proved to be quite good to separate a test from its metadata. We believe that test should not include code that doesn't belong to the test scenario. |
| 52 | + |
| 53 | +So we were glad that native Attributes have landed PHP world. In this release we encourage our users to start using them: |
| 54 | + |
| 55 | +```php |
| 56 | +#[Group('important')] |
| 57 | +#[Group('api')] |
| 58 | +#[Examples('GET', '/users')] |
| 59 | +#[Examples('GET', '/posts')] |
| 60 | +#[Env('staging-alpha')] |
| 61 | +#[Env('staging-beta')] |
| 62 | +#[Env('production')] |
| 63 | +#[Prepare('startServices')] |
| 64 | +public function testApiRequests(ApiTester $I, Example $e) |
| 65 | +{ |
| 66 | + $I->send($e[0], $e[1]); |
| 67 | + $I->seeResponseCodeIsSuccessful(); |
| 68 | + $I->seeResponseIsJson(); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +As you see, attributes decouple all preparation steps, keeping the test scenario minimal. We also keep supporting annotations, so an urgent upgrade is not needed. Attributes can't do something that traditional annotations can't, they are just a modern alternative. |
| 73 | + |
| 74 | +List of available attributes (all under `Codeception\Attribute`) namespace: |
| 75 | + |
| 76 | +* `Before` - specifies the method that should be executed before each test |
| 77 | +* `After` - specifies the method that should be executed after each test |
| 78 | +* `Group` - set the group for the test |
| 79 | +* `Skip` - skips the current test |
| 80 | +* `Incomplete` - marks test as incomplete |
| 81 | +* `Depends` - sets the test that must be executed before the current one |
| 82 | +* `Prepare` - sets a method to execute to initialize the environment (launch server, browser, etc) |
| 83 | +* `DataProvider` - specifies a method that provides data for data-driven tests |
| 84 | +* `Examples` - sets data for data-driven tests inside the annotation |
| 85 | +* `Env` - sets environment value for the current test |
| 86 | +* `Given`, `When`, `Then` - marks a method as BDD step |
| 87 | + |
| 88 | +## Debugging |
| 89 | + |
| 90 | +Do you remember, `Hoa\Console`? Unfortunately, this library was deprecated and we were looking for a modern alternative that could power `codecept console` and `$I->pause();` commands. We switched to [PsySH](https://psysh.org) a PHP REPL. |
| 91 | + |
| 92 | +An interactive console is used to pause a test in the given state. While in pause you can try different Codeception commands, and check variable values. Instead of fixing tests blindly, you can start an interactive session. This is quite a similar effect you can get with a real debugger like XDebug but focused on Codeception commands. Especially this is helpful to write acceptance tests as the test scenario can be planned while executing a test. So basic scenario can be written as: |
| 93 | + |
| 94 | +```php |
| 95 | +$I->amOnPage('/'); |
| 96 | +$I->pause(); |
| 97 | +``` |
| 98 | + |
| 99 | +After opening a page you will be able to try commands in a browser. If a command succeeds you can use it in your tests. |
| 100 | + |
| 101 | +Also new functions were added: |
| 102 | + |
| 103 | +* `codecept_pause()` - starts interactive pause anywhere in debug mode |
| 104 | +* `codecept_debug()` - prints a variable into console using Symfony VarDumper |
| 105 | + |
| 106 | +## Sharding |
| 107 | + |
| 108 | +[Parallel Execution](/docs/ParallelExecution) guide has been rewritten and focused on a new feature: sharding. It is the simplest way to run slow tests (think of acceptance tests first) in parallel on multiple agents. |
| 109 | + |
| 110 | +In this case, you specify the batch of tests that should be executed independently and each job picks up its own not intersecting group of tests to run them. |
| 111 | + |
| 112 | +``` |
| 113 | +# first job |
| 114 | +./venodor/bin/codecept run --shard 1/3 |
| 115 | +
|
| 116 | +# second job |
| 117 | +./venodor/bin/codecept run --shard 2/3 |
| 118 | +
|
| 119 | +# third job |
| 120 | +./venodor/bin/codecept run --shard 3/3 |
| 121 | +``` |
| 122 | + |
| 123 | +This feature reduces the need for complex configuration and usage of `robo` task runner to split tests. |
| 124 | + |
| 125 | +It is recommended to use sharding to parallelize tests between multiple jobs as the simplest approach. Unfortunately, PHP doesn't have native multi-threading for test parallelization, and even if it had, it doesn't solve the problem of running slow browser tests that interacts with a whole application. So only horizontal scaling by jobs can be suggested as a long-running approach. The more build agents you add to your Continuous Integration server, the fastest tests will run. That's it! |
| 126 | + |
| 127 | +## Grep and Filter |
| 128 | + |
| 129 | +New options `--grep` and `--filter` were introduced to select tests by part of their name. Actually, it is the same option and an alias. `--grep` is a common way to select tests to execute in NodeJS test runners, so we ported it to Codeception. But as usual, specific tests can also be executed by group or specifying a test signature. |
| 130 | + |
| 131 | +``` |
| 132 | +php venodor/bin/codecept run --grep "user" |
| 133 | +``` |
| 134 | + |
| 135 | +## Other Changes |
| 136 | + |
| 137 | +Please go through the list of changes introduced to see if they don't affect your codebase: |
| 138 | + |
| 139 | +* Requires PHP 8.0 or higher |
| 140 | +* Compatible with PHPUnit 9 and ready for PHPUnit 10 |
| 141 | +* Compatible with Symfony 4.4 - 6.0 |
| 142 | +* Stricter check for phpdotenv v5 (older versions are not supported) |
| 143 | +* Throw exception if actor setting is missing in suite configuration |
| 144 | +* Removed `generate:cept` command (Cept format is deprecated) |
| 145 | +* Removed settings `disallow_test_output` and `log_incomplete_skipped`. |
| 146 | +* Removed setting `paths.log` (it was replaced by `paths.output` in Codeception 2.3) |
| 147 | +* Removed suite setting `class_name` (replaced by `actor` in Codeception 2.3) |
| 148 | +* Removed global setting `actor` (replaced by `actor_prefix` in Codeception 2.3) |
| 149 | +* Removed `Configuration::logDir` method (replaced by `Configuration::outputDir` in 2.0) |
| 150 | +* Moved XmlBuilder class to module-soap |
| 151 | +* Decoupled test execution and reporting from PHPUnit |
| 152 | +* Custom reporters implementing TestListener are no longer supported and must be converted to Extensions |
| 153 | +* Added optional value to `fail-fast` option (#6275) by #Verest |
| 154 | +* Removed `JSON` and `TAP` loggers |
| 155 | +* Removed code coverage blacklist functionality |
| 156 | +* Removed deprecated class aliases |
| 157 | + - Codeception\TestCase\Test |
| 158 | + - Codeception\Platform\Group |
| 159 | + - Codeception\Platform\Group |
| 160 | + - Codeception\TestCase |
| 161 | +* Introduced strict types in the code base. |
| 162 | + |
| 163 | +[Complete Changelog](https://raw.githubusercontent.com/Codeception/Codeception/5.0/CHANGELOG-5.x.md) |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +We really happy that we are finally here with Codeception 5. This release was crafted during wartime, which happens in Ukraine. It is mentally and morally hard to work on tech products knowing that at any point this peaceful virtual life can end at any moment by a random missile. Codeception was created in 2011 by Michael Bodnarchuk in Kyiv, and today in 2022 he also stays there writing this post. If you want to support Codeception, all the Ukrainian PHP community, and all our brave nation who stands for democracy against barbaric Russian invasion, consider **[donating to Ukrainian charities](https://stand-with-ukraine.pp.ua)**. Not a single time. Every month until the war ends. Every time you travel or enjoy tasty food in a restaurant think of people who are forced to defend their land, or who fled their homes. Glory to Ukraine! |
| 168 | + |
| 169 | +This release wouldn't be possible without the hard work of [Gintautas Misselis](https://github.com/Naktibalda) who keeps constant work on modernizing internals and keeping Codeception up to date. Also we are really thankful to [Gustavo Nieves |
| 170 | +](https://github.com/TavoNiievez) who did a lot of work transitioning Codeception to new Symfony and more! Thanks to our maintainers! If you want to support our work we have [OpenCollective](https://opencollective.com/codeception)! |
| 171 | + |
| 172 | + |
0 commit comments