|
| 1 | +# Debugging |
| 2 | + |
| 3 | +Writing a test is always the process of learning the code and the application. |
| 4 | +It is ok, if a test can't be writtng from scratch or you don't understand the effects of the code. |
| 5 | +By looking into the following debugging practices you will learn how to get all required information inside a test to finish it. |
| 6 | + |
| 7 | +## Output |
| 8 | + |
| 9 | +Codeception provides `codecept_debug` function to print a debug output when running test. |
| 10 | +Think of it as `var_dump` but for testing: |
| 11 | + |
| 12 | +{% highlight php %} |
| 13 | +codecept_debug($user); |
| 14 | +{% endhighlight %} |
| 15 | + |
| 16 | +Unlinke var_dump, the output will be printed to screen only if tests are executed with `--debug` flag. |
| 17 | + |
| 18 | +``` |
| 19 | +php vendor/bin/codecept run --debug |
| 20 | +``` |
| 21 | + |
| 22 | +So it is safe to keep `codecept_debug` inside a test, it won't affect the code running on Continuous Integration server. |
| 23 | + |
| 24 | +`codecept_debug` can be used in any place of your tests, but it is prohibited to use it in application code. |
| 25 | +This function is loaded only by Codeception, so the application may be broken trying to call this line. |
| 26 | + |
| 27 | +Inside a [Helper](/docs/06-ModulesAndHelpers#Helpers) you can use analogs of this function to provide a debug output for a complex action. |
| 28 | + |
| 29 | + |
| 30 | +{% highlight php %} |
| 31 | +// print variable from helper |
| 32 | +$this->debug($response); |
| 33 | + |
| 34 | +// print variable with a short comment |
| 35 | +$this->debugSection('Response', $response); |
| 36 | +{% endhighlight %} |
| 37 | + |
| 38 | +Codeception Modules use debug output to give more information to user about the data used by a test. For instance, in debug mode you can see request and responses body when testing REST API. |
| 39 | + |
| 40 | + |
| 41 | +## Pause |
| 42 | + |
| 43 | +When running acceptance or functional test it might be needed to pause execution at one point to figure out what to do next. For instance, when interacting with a page in a web browser, you might need the execution to be paused to interact with elements on a page, discover proper locators, and next steps for the scenario. That's why Codeception has an interactive pause mode (powered by [PsySH](https://psysh.org)) which can be started by `codecept_pause` function or `$I->pause()`. |
| 44 | + |
| 45 | +Writing a new acceptance from scratch can be more convenient if you hold a browser window open. It is recommended to start writing a new acceptance test with these two commands: |
| 46 | + |
| 47 | +{% highlight php %} |
| 48 | +$I->amOnPage('/'); |
| 49 | +$I->pause(); |
| 50 | +{% endhighlight %} |
| 51 | + |
| 52 | +Interactive pause is launched only when `--debug ` option is enabled: |
| 53 | + |
| 54 | +``` |
| 55 | +php vendor/bin/codecept run --debug |
| 56 | +``` |
| 57 | + |
| 58 | +To launch interactive pause in a context when the `$I` object is not available, use `codecept_pause` function instead. To inspect local variables pass them into interactive shell using an array: |
| 59 | + |
| 60 | +{% highlight php %} |
| 61 | +$I->pause(['user' => $user]) |
| 62 | +// or |
| 63 | +codecept_pause(['user' => $user]); |
| 64 | +{% endhighlight %} |
| 65 | + |
0 commit comments