Skip Navigation LinksHome > Categories > Code from a Category

Writing automated tests and the environment phpUnit



User Name: serfcompany
Name: Serf
Contact Me: www.datawebcoder.com
Home Page: www.datawebcoder.com
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1785
Add Date: 04/11/2012
The main task of a professional php-developer - create as soon as possible the software, fully satisfying the customer. Scripts should be doing exactly what is expected of them. The simplest example - the feedback form, which at the touch of a button, provided you fill in the fields sends mail or writes data in a database table. If she did not, obviously, that has crept into the program error.

Thus, testing - an activity aimed at identifying such discrepancies between expected and actual.
By identifying discrepancies, incorrect behavior at the design stage, the developer deliberately and systematically reduces the likelihood that this will have to face the user.
Test programs (in particular php-scripts), it is possible manually.


On the example of feedback forms, it is clear that the test is very simple.
Launch your browser, go to the correct url, fill in the fields, click on "OK" and then go into the database and check to see if there is a record.
Fine, but after any change in the script can not be sure it is working.


One can hope to at random and check whether the script after the next update or not, but remember that if you're not done testing, it will make the customer at the time of delivery of the project or the end user during operation.
In the first case, it threatens your financial instability, and the second your image will suffer. In order not to test the manual (it's long, complex and requires a special process of perseverance), you can write a program that tests the other programs, or create automated tests.


In any modern software development methodologies testing is an integral part of the process, and the significance of his writing not less than the significance of writing code.
The professional developer must create a set of different types of automated tests.


Despite the obvious usefulness of the methodologies and the emphasis on the process of testing, the authors personal experience suggests the following range of opinions about a web-programmers own automated testing scripts:


* "No one writes, why should I write"0
* "I do not allocate time specifically for writing tests, although it would be useful"
* "I've been writing tests and do not understand how anyone can live without it"


Automated tests and web-programming


Weekdays bulk of web-developers - select a good server to host the site. It is difficult to find a hosting provider (php + mysql for a reasonable price), which would suit immediately by 100%, and pereezd to the new server, even for an interpreted language, which is my beloved PHP, could be "fatal."
Even the documentation for Java - a language originally oriented cross-platform, says that the price of portability is never equal to zero. These people know what they say. After moving to the beautifully-established and stable working earlier scripts may appear warning'i, and some programs will stop working completely: changed the way the library, the version of the php.


In the end, having a set of tests to move much easier. It is necessary to rewrite the scripts to run the test suite, to see those tests that fail, to understand, correct, run the test suite again. Everything.
Instead of leaving, "half dead" project at the new hosting, "surface"'ll click on the links in the browser, we get a stable working site on the new server without much headache.
Automated testing ultimately improves the quality of the code, and given the specifics of web-programming, simplifies the process of moving from server to server.


Following the decision to test, comes the need to organize the process. For example, to allocate a separate test scripts directory, create a program that runs all the scripts and, depending on their outcome will write "OK" or "Error".
It is possible to do this "from scratch", but is ready to test the infrastructure (utility programs) that bring together groups of tests "on", they consistently maintain, run and display the results.
Creating tests phpUnit.


Mainly because of the passion of XP methodology, the authors are familiar with the test infrastructure, JUnit, respectively, for php they chose the phpUnit, both at the port created by enthusiasts of "big brother".
Today, there are several different versions of the port of JUnit on php. At least three variants of c phpUnit name and port php_simpletest.
Regularly updated version of PEAR :: PHPUnit (Sebastian Bergmann). Php_simpletest is in the first beta, the other phpUnit'y updated a year ago (phpUnit project) and two years ago (phpunit-1.0.0), respectively.
All the following examples of unit tests written using PEAR :: PHPUnit.


Create a set of tests for the class Message, which formats and checks the message for later sending it by e-mail.


The class constructor gets three parameters (the sender's name, his e-mail addresses and the message body). Method format_message () formats the message before sending, as is_valid () checks to see if the message is sent (whether fields are filled, and correct to put e-mail). Source code for the class Message - here.


The "heart" is testing class PHPUnit, which runs the TestSuite (test set) and returns a TestResult (test result).


In order to write a minimal set of tests using phpUnit, should:


1.Connect the library PHPUnit.php
2. Create a subclass of TestCase base class
3. Add to it any number of testing methods, which begin with "test". In this case "test_empty_input", "test_email_invalid", "test_valid_input". They will be called class methods testiruemogo Message.
The input will be made known in advance the parameters and the result is compared with the reference by the family of functions Assert, a legacy to our test class from TestCase (assertEquals method examines the expected and actual result for equality, assertTrue checks whether the submitted parameter to "true", etc. etc.)
4. Establish an class PHPUnit_TestSuite, passing it as parameter the name of the class with a set of tests
5. Run the test suite, and display the result


Create a set of tests for the class, following the instruction manual.


We will test the following aspects of the class Message: its response to the blank message and an incorrect e-mail, as well as the object of a class compare formatted message with a reference.


Create a message that we submit to the input of the designer wrong e-mail, expecting that the class method is_valid (), responsible for its inspection, return 'false'.



Create a message that we submit to the input of the designer an empty string, we expect that a class method is_valid (), just like last time, will return false.



And finally, we submit to the input of well-formed message and expect that it correctly Format the class.



Run the test suite (full version testmessage.php - here) and we obtain the following result:



All tests were successful. Now we can continue to build the functionality of the class Message, knowing that after the next update it is working correctly (of course, on the condition that triggered all of the tests).


Design and development of the Message class Message c set of tests is not very different in time, but in the second case we have a strong "support" for the future of each function we think at least twice, and later we can easily determine the performance of a new script for hardware and software configuration.


Appendix: the source code message.inc.php



Appendix: the source code testmessage.php.txt


Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.