|
| 1 | +<meta http-equiv="refresh" content="0;url=https://codeception.com/docs/Introduction"> |
1 | 2 | ---
|
2 | 3 | layout: doc
|
3 | 4 | title: Introduction - Codeception Docs
|
4 | 5 | ---
|
5 | 6 |
|
6 |
| -<div class="alert alert-success">💡 <b>You are reading docs for latest Codeception 5</b>. <a href="https://github.com/Codeception/codeception.github.com/blob/4.x/docs/01-Introduction.md">Read for 4.x</a></div> |
7 |
| - |
8 |
| -# Introduction |
9 |
| - |
10 |
| -The idea behind testing is not new. You can't sleep well if you are not confident |
11 |
| -that your latest commit didn't take down the entire application. |
12 |
| -Having your application covered with tests gives you more trust in the stability of your application. That's all. |
13 |
| - |
14 |
| -In most cases tests don't guarantee that the application works 100% as it is supposed to. |
15 |
| -You can't predict all possible scenarios and exceptional situations for complex apps, |
16 |
| -but with tests you can cover the most important parts of your app and at least be sure they work as predicted. |
17 |
| - |
18 |
| -There are plenty of ways to test your application. |
19 |
| -The most popular paradigm is [Unit Testing](https://en.wikipedia.org/wiki/Unit_testing). |
20 |
| -For web applications, testing just the controller and/or the model doesn't prove that your application is working. |
21 |
| -To test the behavior of your application as a whole, you should write functional or acceptance tests. |
22 |
| - |
23 |
| -Codeception supports all three testing types. |
24 |
| -Out of the box you have tools for writing unit, functional, and acceptance tests in a unified framework. |
25 |
| - |
26 |
| -| | Unit Tests | Functional Tests | Acceptance Tests |
27 |
| -| --- | --- | --- | --- | |
28 |
| -| Scope of the test | Single PHP class | PHP Framework (Routing, Database, etc.) | Page in browser (Chrome, Firefox, or [PhpBrowser](https://codeception.com/docs/03-AcceptanceTests#PhpBrowser)) | |
29 |
| -| Testing computer needs access to project's PHP files | Yes | Yes | No | |
30 |
| -| Webserver required | No | No | Yes | |
31 |
| -| JavaScript | No | No | Yes | |
32 |
| -| Additional software required | None | None | Drivers for Firefox/Chrome | |
33 |
| -| Test execution speed | Very fast | Fast | Slow | |
34 |
| -| Configuration file | `Unit.suite.yml` | `Functional.suite.yml` | `Acceptance.suite.yml` | |
35 |
| - |
36 |
| -One of the main advantages of Codeception is that you don't have to decide on just *one* type of testing. You should have all three! |
37 |
| -And chances are, that you will (sooner or later) need all three. That's why Codeception consists of three so-called "suites": |
38 |
| -A "Unit suite" for all unit tests, a "functional suite" for all functional tests, and an "Acceptance suite" for all acceptance tests. |
39 |
| - |
40 |
| -Let's review those three test types in reverse order. |
41 |
| - |
42 |
| -### Acceptance Tests |
43 |
| - |
44 |
| -How does your client, manager, tester, or any other non-technical person know your website is working? By opening the browser, accessing the site, clicking on links, filling in the forms, and actually seeing the content on a web page. They have no idea of the programming language, framework, database, web-server, |
45 |
| -or why the application did (or did not) behave as expected. |
46 |
| - |
47 |
| -This is what acceptance tests are doing. They cover scenarios from a user's perspective. |
48 |
| -With acceptance tests, you can be confident that users, following all the defined scenarios, won't get errors. |
49 |
| - |
50 |
| -> **Any website** can be covered with acceptance tests, even if you use a very exotic CMS or framework. |
51 |
| -
|
52 |
| -#### Sample acceptance test |
53 |
| - |
54 |
| -```php |
55 |
| -$I->amOnPage('/'); |
56 |
| -$I->click('Sign Up'); |
57 |
| -$I->submitForm('#signup', [ |
58 |
| - 'username' => 'MilesDavis', |
59 |
| - 'email' => 'miles@davis.com' |
60 |
| -]); |
61 |
| -$I->see('Thank you for Signing Up!'); |
62 |
| -``` |
63 |
| - |
64 |
| -### Functional Tests |
65 |
| - |
66 |
| -What if you could check our application without running it on a server? |
67 |
| -That way you could see detailed exceptions on errors, have your tests run faster, and check the database against predictable and expected results. That's what functional tests are for. |
68 |
| - |
69 |
| -For functional tests, you emulate a web request (`$_GET` and `$_POST` variables) which returns the HTML response. Inside a test, you can make assertions about the response, and you can check if the data was successfully stored in the database. |
70 |
| - |
71 |
| -For functional tests, your application needs to be structured in order to run in a test environment. Codeception provides modules for all popular PHP frameworks. |
72 |
| - |
73 |
| -#### Sample functional test |
74 |
| - |
75 |
| -```php |
76 |
| -$I->amOnPage('/'); |
77 |
| -$I->click('Sign Up'); |
78 |
| -$I->submitForm('#signup', ['username' => 'MilesDavis', 'email' => 'miles@davis.com']); |
79 |
| -$I->see('Thank you for Signing Up!'); |
80 |
| -$I->seeEmailIsSent('miles@davis.com', 'Thank you for your registration'); |
81 |
| -$I->seeInDatabase('users', ['email' => 'miles@davis.com']); |
82 |
| -``` |
83 |
| - |
84 |
| -> This looks very similar to acceptance tests. The behavior is the same, however, the test is executed inside PHP without launching a real browser. |
85 |
| -
|
86 |
| -### Unit Tests |
87 |
| - |
88 |
| -Testing pieces of code before coupling them together is highly important as well. This way, |
89 |
| -you can be sure that some deeply hidden feature still works, even if it was not covered by functional or acceptance tests. |
90 |
| -This also shows care in producing stable and testable code. |
91 |
| - |
92 |
| -Codeception is created on top of [PHPUnit](https://www.phpunit.de/). If you have experience writing unit tests with PHPUnit |
93 |
| -you can continue doing so. Codeception has no problem executing standard PHPUnit tests, |
94 |
| -but, additionally, Codeception provides some well-built tools to make your unit tests simpler and cleaner. |
95 |
| - |
96 |
| -Requirements and code can change rapidly, |
97 |
| -and unit tests should be updated every time to fit the requirements. |
98 |
| -The better you understand the testing scenario, the faster you can update it for new behavior. |
99 |
| - |
100 |
| -#### Sample integration test |
101 |
| - |
102 |
| -```php |
103 |
| -public function testSavingUser() |
104 |
| -{ |
105 |
| - $user = new User(); |
106 |
| - $user->setName('Miles'); |
107 |
| - $user->setSurname('Davis'); |
108 |
| - $user->save(); |
109 |
| - $this->assertEquals('Miles Davis', $user->getFullName()); |
110 |
| - $this->tester->seeInDatabase('users', [ |
111 |
| - 'name' => 'Miles', |
112 |
| - 'surname' => 'Davis' |
113 |
| - ]); |
114 |
| -} |
115 |
| -``` |
116 |
| - |
117 |
| -## Conclusion |
118 |
| - |
119 |
| -The Codeception framework was developed to actually make testing fun. |
120 |
| -It allows writing unit, functional, integration, and acceptance tests in a single, coherent style. |
121 |
| - |
122 |
| -All Codeception tests are written in a descriptive manner. |
123 |
| -Just by looking at the test body, you can clearly understand what is being tested and how it is performed. |
124 |
| - |
125 |
| -<div class="alert alert-warning"><a href="https://github.com/Codeception/codeception.github.com/edit/master/guides/01-Introduction.md"><strong>Improve</strong> this guide</a></div> |
| 7 | +<div class="alert alert-warning"> |
| 8 | + See <a href="https://codeception.com/docs/Introduction">Introduction</a> |
| 9 | +</div> |
0 commit comments