20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Testes<br />

Visão Geral<br />

Note: The testing support described in this chapter requires <strong>Yii</strong> version <strong>1.1</strong> or higher.<br />

This <strong>do</strong>es not mean, however, that you cannot test applications developed using <strong>Yii</strong><br />

1.0.x. There are many great testing frameworks available to help you accomplish this<br />

task, such as PHPUnit, SimpleTest.<br />

Testing is an indispensable process of software development. Whether we are aware of it<br />

or not, we conduct testing all the time when we are developing a Web application. For<br />

example, when we write a class in PHP, we may use some echo or die statement to show<br />

that we implement a method correctly; when we implement a Web page containing a<br />

complex HTML form, we may try entering some test data to ensure the page interacts with<br />

us as expected. More advanced developers would write some code to automate this<br />

testing process so that each time when we need to test something, we just need to call up<br />

the code and let the computer to perform testing for us. This is known as automated<br />

testing, which is the main topic of this chapter.<br />

The testing support provided by <strong>Yii</strong> includes unit testing and functional testing.<br />

A unit test verifies that a single unit of code is working as expected. In object-oriented<br />

programming, the most basic code unit is a class. A unit test thus mainly needs to verify<br />

that each of the class interface methods works properly. That is, given different input<br />

parameters, the test verifies the method returns expected results. Unit tests are usually<br />

developed by people who write the classes being tested.<br />

A functional test verifies that a feature (e.g. post management in a blog system) is working<br />

as expected. Compared with a unit test, a functional test sits at a higher level because a<br />

feature being tested often involves multiple classes. Functional tests are usually developed<br />

by people who know very well the system requirements (they could be either developers or<br />

quality engineers).<br />

Test-Driven Development<br />

Below we show the development cycles in the so-called test-driven development (TDD):<br />

1. Create a new test that covers a feature to be implemented. The test is expected to<br />

fail at its first execution because the feature has yet to be implemented.<br />

2. Run all tests and make sure the new test fails.<br />

3. Write code to make the new test pass.<br />

4. Run all tests and make sure they all pass.<br />

5. Refactor the code that is newly written and make sure the tests still pass.<br />

Repeat step 1 to 5 to push forward the functionality implementation.<br />

Test Environment Setup

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!