|
| 1 | +--- |
| 2 | +layout: doc |
| 3 | +title: Getting Started - Codeception - Documentation |
| 4 | +--- |
| 5 | + |
| 6 | +# Getting Started |
| 7 | + |
| 8 | +Let's take a look into Codeception architecture. We assume that you already [installed](http://codeception.com/install) it, and bootstrapped your first test suites. Codeception has generated three of them: unit, functional, and acceptance. They are well described in the previous chapter. Inside your __/tests__ folder you will have three config files and three directories with names corresponding to these suites. Suites are independent groups of tests with a common purpose. |
| 9 | + |
| 10 | +## Actors |
| 11 | + |
| 12 | +One of the main concepts of Codeception is representation of tests as actions of a person. We have a UnitTester, who executes functions and tests the code. We also have a FunctionalTester, a qualified tester, who tests the application as a whole, with knowledge of its internals. And a AcceptanceTester, a user who works with our application through an interface that we provide. |
| 13 | + |
| 14 | +Each of these Actors are PHP classes along with the actions that they are allowed to do. As you can see, each of these Actors have different abilities. They are not constant, you can extend them. One Actor belongs to one testing suite. |
| 15 | + |
| 16 | +Actor classes are not written but generated from suite configuration. When you change configuration actor classes are **rebuilt automatically**. |
| 17 | + |
| 18 | +If Actor classes are not created or updated as you expect, try to generate them manually with `build` command: |
| 19 | + |
| 20 | +{% highlight yaml %} |
| 21 | + |
| 22 | +$ php codecept.phar build |
| 23 | + |
| 24 | +{% endhighlight %} |
| 25 | + |
| 26 | + |
| 27 | +## Writing a Sample Scenario |
| 28 | + |
| 29 | +By default tests are written as narrative scenarios. To make a PHP file a valid scenario, its name should have a `Cept` suffix. |
| 30 | + |
| 31 | +Let's say, we created a file `tests/acceptance/SigninCept.php` |
| 32 | + |
| 33 | +We can do that by running command: |
| 34 | + |
| 35 | +{% highlight yaml %} |
| 36 | + |
| 37 | +$ php codecept.phar generate:cept acceptance Signin |
| 38 | + |
| 39 | +{% endhighlight %} |
| 40 | + |
| 41 | +{% highlight php %} |
| 42 | + |
| 43 | +<?php |
| 44 | +$I = new AcceptanceTester($scenario); |
| 45 | +?> |
| 46 | + |
| 47 | +{% endhighlight %} |
| 48 | + |
| 49 | +A Scenario always starts with Actor class initialization. After that, writing a scenario is just like typing `$I->` and choosing a proper action from the auto-completion list. |
| 50 | + |
| 51 | +Let's sign in to our site. We assume that we have a 'login' page where we are getting authenticated by login and password. Then we are moved to a user page, where we see the text `Hello, %username%`. Let's look at how this scenario is written in Codeception. |
| 52 | + |
| 53 | +{% highlight php %} |
| 54 | + |
| 55 | +<?php |
| 56 | +$I = new AcceptanceTester($scenario); |
| 57 | +$I->wantTo('log in as regular user'); |
| 58 | +$I->amOnPage('/login'); |
| 59 | +$I->fillField('Username','davert'); |
| 60 | +$I->fillField('Password','qwerty'); |
| 61 | +$I->click('Login'); |
| 62 | +$I->see('Hello, davert'); |
| 63 | +?> |
| 64 | + |
| 65 | +{% endhighlight %} |
| 66 | + |
| 67 | +Before we execute this test, we should make sure that the site is running on a local web server. Let's open the `tests/acceptance.suite.yml` file and replace the URL with the URL of your web application: |
| 68 | + |
| 69 | +{% highlight yaml %} |
| 70 | + yaml |
| 71 | +config: |
| 72 | + PhpBrowser: |
| 73 | + url: 'http://myappurl.local' |
| 74 | + |
| 75 | +{% endhighlight %} |
| 76 | + |
| 77 | +After we configured utl we can run this test with `run` command: |
| 78 | + |
| 79 | +{% highlight yaml %} |
| 80 | + bash |
| 81 | +$ php codecept.phar run |
| 82 | + |
| 83 | +{% endhighlight %} |
| 84 | + |
| 85 | +Here is the output we should see: |
| 86 | + |
| 87 | +{% highlight yaml %} |
| 88 | + bash |
| 89 | +Acceptance Tests (1) ------------------------------- |
| 90 | +Trying log in as regular user (SigninCept.php) Ok |
| 91 | +---------------------------------------------------- |
| 92 | + |
| 93 | +Functional Tests (0) ------------------------------- |
| 94 | +---------------------------------------------------- |
| 95 | + |
| 96 | +Unit Tests (0) ------------------------------------- |
| 97 | +---------------------------------------------------- |
| 98 | + |
| 99 | +Time: 1 second, Memory: 21.00Mb |
| 100 | + |
| 101 | +OK (1 test, 1 assertions) |
| 102 | + |
| 103 | +{% endhighlight %} |
| 104 | + |
| 105 | +Let's get a detailed output: |
| 106 | + |
| 107 | +{% highlight yaml %} |
| 108 | +bash |
| 109 | +$ php codecept.phar run acceptance --steps |
| 110 | + |
| 111 | +{% endhighlight %} |
| 112 | + |
| 113 | +We should see a step-by-step report on the performed actions. |
| 114 | + |
| 115 | +{% highlight yaml %} |
| 116 | +bash |
| 117 | +Acceptance Tests (1) ------------------------------- |
| 118 | +Trying to log in as regular user (SigninCept.php) |
| 119 | +Scenario: |
| 120 | +* I am on page "/login" |
| 121 | +* I fill field "Username" "davert" |
| 122 | +* I fill field "Password" "qwerty" |
| 123 | +* I click "Login" |
| 124 | +* I see "Hello, davert" |
| 125 | + OK |
| 126 | +---------------------------------------------------- |
| 127 | + |
| 128 | +Time: 0 seconds, Memory: 21.00Mb |
| 129 | + |
| 130 | +OK (1 test, 1 assertions) |
| 131 | + |
| 132 | +{% endhighlight %} |
| 133 | + |
| 134 | +That was a very simple test that you can reproduce for your own site. |
| 135 | +By emulating the user's actions you can test all of your site the same way. |
| 136 | + |
| 137 | +Give it a try! |
| 138 | + |
| 139 | +## Modules and Helpers |
| 140 | + |
| 141 | +The actions in Actor classes are taken from modules. Generated Actor classes emulate multiple inheritance. Modules are designed to have one action performed with one method. According to the [DRY principle](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself), if you use the same scenario components in different modules, you can combine them and move them to a custom module. By default each suite has an empty module, which can be used to extend Actor classes. They are stored in the ___support__ directory. |
| 142 | + |
| 143 | +## Bootstrap |
| 144 | + |
| 145 | +Each suite has its own bootstrap file. It's located in the suite directory and is named `_bootstrap.php`. It will be executed before test suite. There us also a global bootstrap file located in `tests` directory. It can be used to include additional files. |
| 146 | + |
| 147 | +## Test Formats |
| 148 | + |
| 149 | +Codeception supports three test formats. Besides the previously described scenario-based Cept format, Codeception can also execute [PHPUnit test files for unit testing](http://codeception.com/docs/06-UnitTests), and [class-based Cest](http://codeception.com/docs/07-AdvancedUsage#Cest-Classes) format. They are covered in later chapters. There is no difference in the way the tests of either format will be run in the suite. |
| 150 | + |
| 151 | +## Configuration |
| 152 | + |
| 153 | +Codeception has a global configuration in `codeception.yml` and a config for each suite. We also support `.dist` configuration files. If you have several developers in a project, put shared settings into `codeception.dist.yml` and personal settings into `codeception.yml`. Same goes for suite configs. For example, the `unit.suite.yml` will be merged with `unit.suite.dist.yml`. |
| 154 | + |
| 155 | + |
| 156 | +## Running Tests |
| 157 | + |
| 158 | +Tests can be started with the `run` command. |
| 159 | + |
| 160 | +{% highlight yaml %} |
| 161 | +bash |
| 162 | +$ php codecept.phar run |
| 163 | + |
| 164 | +{% endhighlight %} |
| 165 | + |
| 166 | +With the first argument you can run tests from one suite. |
| 167 | + |
| 168 | +{% highlight yaml %} |
| 169 | +bash |
| 170 | +$ php codecept.phar run acceptance |
| 171 | + |
| 172 | +{% endhighlight %} |
| 173 | + |
| 174 | +To run exactly one test, add a second argument. Provide a local path to the test, from the suite directory. |
| 175 | + |
| 176 | +{% highlight yaml %} |
| 177 | +bash |
| 178 | +$ php codecept.phar run acceptance SigninCept.php |
| 179 | + |
| 180 | +{% endhighlight %} |
| 181 | + |
| 182 | +Alternatively you can provide full path to test file: |
| 183 | + |
| 184 | +{% highlight yaml %} |
| 185 | +bash |
| 186 | +$ php codecept.phar run tests/acceptance/SigninCept.php |
| 187 | + |
| 188 | +{% endhighlight %} |
| 189 | + |
| 190 | +You can provide a directory path as well: |
| 191 | + |
| 192 | +{% highlight yaml %} |
| 193 | +bash |
| 194 | +$ php codecept.phar run tests/acceptance/backend |
| 195 | + |
| 196 | +{% endhighlight %} |
| 197 | + |
| 198 | +Which will execute all tests from backend dir. |
| 199 | + |
| 200 | +To execute a group of tests that are not stored in the same dir you can organize them in [groups](http://codeception.com/docs/07-AdvancedUsage#Groups). |
| 201 | + |
| 202 | +### Reports |
| 203 | + |
| 204 | +To generate JUnit XML output you can provide `--xml` option, and `--html` for HTML report. |
| 205 | + |
| 206 | +{% highlight yaml %} |
| 207 | +bash |
| 208 | +$ php codecept.phar run --steps --xml --html |
| 209 | + |
| 210 | +{% endhighlight %} |
| 211 | + |
| 212 | +This command will run all tests for all suites, displaying the steps, and building HTML and XML reports. Reports will be store in `tests/_output/` directory. |
| 213 | + |
| 214 | +And to learn all available options: |
| 215 | + |
| 216 | +{% highlight yaml %} |
| 217 | +bash |
| 218 | +$ php codecept.phar help run |
| 219 | + |
| 220 | +{% endhighlight %} |
| 221 | + |
| 222 | +## Debugging |
| 223 | + |
| 224 | +To receive detailed output tests can be executed with `--debug` option. |
| 225 | +You may print any information inside a test using `codecept_debug` function. |
| 226 | + |
| 227 | +### Generators |
| 228 | + |
| 229 | +There are plenty of useful Codeception commands. |
| 230 | + |
| 231 | +* `generate:cept` *suite* *filename* - Generates a sample Cept scenario. |
| 232 | +* `generate:cest` *suite* *filename* - Generates a sample Cest test. |
| 233 | +* `generate:test` *suite* *filename* - Generates a sample PHPUnit Test with Codeception hooks. |
| 234 | +* `generate:phpunit` *suite* *filename* - Generates a classic PHPUnit Test. |
| 235 | +* `generate:suite` *suite* *guy* - Generates a new suite with the given Guy class name. |
| 236 | +* `generate:scenarios` *suite* - Generates text files containing scenarios from tests. |
| 237 | + |
| 238 | + |
| 239 | +## Conclusion |
| 240 | + |
| 241 | +We took a look into the Codeception structure. Most of the things you need were already generated by the `bootstrap` command. After you have reviewed the basic concepts and configurations, you can start writing your first scenarios. |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | +* **Next Chapter: [ModulesAndHelpers.md >](/docs/03-ModulesAndHelpers.md)** |
| 246 | +* **Previous Chapter: [< Introduction.md](/docs/01-Introduction.md)** |
0 commit comments