|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Codeception 2.5: Snapshots" |
| 4 | +date: 2018-09-24 01:03:50 |
| 5 | +--- |
| 6 | + |
| 7 | +The Autumn is a good time to start something new. How about new Codeception? |
| 8 | +As you know, each minor release indicates some important change, new features we wanted to share. |
| 9 | +This time we want to introduce you to |
| 10 | + |
| 11 | +## Snapshots |
| 12 | + |
| 13 | +That's the new feature, which might be useful if you are tired of harcoding your data into fixtures. For instance, when you test the search engine you don't know the exact list of items to check, however, you are interested that the list would be the same every time for a search term. |
| 14 | +What about API responses? You don't want to hardcode it fully but you may want to check that response is the same as it was before. |
| 15 | + |
| 16 | +So now Codeception can do what [php-vcr](https://github.com/php-vcr/php-vcr) was doing all the time. Saving data into the file (called snapshot), and comparing with it on next runs. |
| 17 | + |
| 18 | +This is nice feature for testing REST APIs. |
| 19 | + |
| 20 | +Let's assume you have such JSON response. |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "firstName": "John", |
| 25 | + "lastName" : "doe", |
| 26 | + "phoneNumbers": [ |
| 27 | + { |
| 28 | + "type" : "iPhone", |
| 29 | + "number": "0123-4567-8888" |
| 30 | + }, |
| 31 | + { |
| 32 | + "type" : "home", |
| 33 | + "number": "0123-4567-8910" |
| 34 | + } |
| 35 | + ] |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +And you want to check that phone number type is the same on each run. |
| 40 | +For this we can use a snapshot. |
| 41 | + |
| 42 | +```php |
| 43 | +namespace Snapshot; |
| 44 | + |
| 45 | +class PhoneNumberSnapshot extends Codeception\Snapshot { |
| 46 | + |
| 47 | + /** @var \ApiTester */ |
| 48 | + protected $i; |
| 49 | + |
| 50 | + public function __construct(\ApiTester $I) |
| 51 | + { |
| 52 | + $this->i = $I; |
| 53 | + } |
| 54 | + |
| 55 | + protected function fetchData() |
| 56 | + { |
| 57 | + return $this->i->grabDataFromResponseByJsonPath('$.phoneNumbers[*].type'); |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Then in test we can check if data matches the snapshot by calling the snapshot class: |
| 64 | + |
| 65 | +```php |
| 66 | +public function testUsers(ApiTester $I, \Snapshot\PhoneNumberSnapshot $shapsnot) |
| 67 | +{ |
| 68 | + $I->sendGET('/api/users'); |
| 69 | + $snapshot->assert(); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +If the data changes, snapshot is easy to update. Just run the test in `--debug` mode. |
| 74 | +The snapshot will be overwritten with a new data. |
| 75 | + |
| 76 | +So, the good thing about snapshots: |
| 77 | + |
| 78 | +* you don't keep flaky data in your code |
| 79 | +* you don't need to hardcode data values |
| 80 | +* data can be easily updated on change |
| 81 | + |
| 82 | +Use them! |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +There are also some other minor changes: |
| 87 | + |
| 88 | +* **[Db module](https://codeception.com/docs/modules/Db#Example-with-multi-databases) now supports multiple database** connections. If you use few databases we got you covered! That was a long awaited feature and finally we have a very nice implementation from @eXorus. |
| 89 | +* `--seed` parameter added to `run` so you could rerun the tests in same order. This feature works only if you enabled [shuffle](https://codeception.com/docs/07-AdvancedUsage#Shuffle) mode. |
| 90 | +* Possible breaking: `seeLink` behavior changed. |
| 91 | + * Previous: `$I->seeLink('Delete','/post/1');` matches `<a href="/post/199">Delete</a>` |
| 92 | + * Now: `$I->seeLink('Delete','/post/1');` does NOT match `<a href="/post/199">Delete</a>` |
| 93 | + |
| 94 | +[See changelog](https://codeception.com/changelog) for the complete list of fixes and improvements. |
| 95 | + |
| 96 | +Thanks to all our contributors who made this release happen! |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Call to Sponsors! |
| 101 | + |
| 102 | +We try to keep Codeception going and bring more releases to you. If your company uses this framework and you'd like to give back please consider **sponsoring Codeception**. That's right. We are asking to invest into open source, to get the features you expect and to give back to open source. |
| 103 | + |
| 104 | +For instance, how would you like improving stability of WebDriver tests? |
| 105 | +It would be cool to automatically **retry failed steps** and **rerun failed tests**. |
| 106 | +These could be a very cool features but they can't be made without your help. |
| 107 | + |
| 108 | +If you are interested consider sponsoring us: |
| 109 | + |
| 110 | +<p class="text-center"> |
| 111 | +<a href="https://docs.google.com/forms/d/e/1FAIpQLSeVJWu2HJTjAE81SLiYJ1xqxAXeNNSCR_GO9R0_4CKka_nFvA/viewform?usp=send_form" class="btn btn-lg btn-warning" role="button">Sponsor Codeception</a></p> |
| 112 | + |
| 113 | +Yes, we also provide [enterprise support](http://sdclabs.com/codeception?utm_source=codeception.com&utm_medium=top_menu&utm_term=link&utm_campaign=reference) and [trainings](http://sdclabs.com/trainings?utm_source=codeception.com&utm_medium=top_menu&utm_term=link&utm_campaign=reference). This is another way you can support the development. Thank you! |
| 114 | + |
0 commit comments