Technology extreme programming suggests the existence of certain initial conditions. For example, you should be prepared from the initial design to your project implement redundant code. Moreover, with increasing functionality of the problem, this code should also grow in volume. Its main purpose – checking of your classes and functions, as well as testing the input and output data for the application. In my first article ("Test Functions in PHP-projects") about the use of technology impact the design (or extreme programming, as they call it, most developers), emphasis was placed on the concept of introducing such methods in the design of the finished project. In today’s article I would like to focus on the use of ready-made software that is designed specifically for such technology in a ready-made and engineered systems. So today’s article will shed some light on SimpleTest.
In contrast, the simplest approach to the testing organization through self-writing class SimpleTest testing methods can dramatically increase the possibility of code being tested. Ideologically self testing beta-code is a wrapper class that inherits the capabilities of the underlying object SimpleTest. This base class allows you to perform a lot of standard actions such as checking the value of the variable to True or False, and issuing reports. So, if you decide to introduce a professional approach to solving the problem of automatic test your Web application, you should be ready for use very often the object code and transferring part of your functionality in PHP-classes. So, where do you start the process of translating your objectives on track Extreme Programming? Initially, all the same SourceForge download the software modules for testing. It should be noted that a software product, though perfectly functional, but is in alpha testing, which should set aside a mark on the entire process of your acquaintance with him (for example, be prepared to send a report about the error you found it to developers). Next, place the contents of the archive in the subdirectory simpletest your web application. After that you should define a list of the functions of your problem to be a priority to be tested. They can be selected as the criterion for frequency of use, and other critical for your specific application parameters. For example, you can choose for the beginning of the most prolonged time of execution or the most common correctable function.
It should be noted that in terms of convenience for the developer himself to the task of testing any Web application can use two approaches. You can embed code in the testing task itself, and thus transfer the responsibility for testing applications on the shoulders of users (you never know what will come out error in the use of your scripts), or initially separate location code, and test of a Web-based applications. In any case a good idea is to incorporate the functionality of testing code in the resulting web application, at least as an additional information service for the team. So, perhaps the most suitable location for the functions of the same name SimpleTest is a subdirectory of your web application. By placing the scripts in this way, you get to check the code is located on the server side, which is rather convenient in terms of support for Web applications, and in terms of security of the customer himself from attacks on its security.
To demonstrate the process to try to make sure the tested functions of the class intended to issue messages to users, and text of the log. For simplicity, we confine most simple functionality of this class. We assume it will create a text log of a call from a user script. The following screen shot shows a simplified code of this class, which, however, can give an idea about the testing process.
So, how is the connection class SimpleTest to your application? In the first step is to physically connect your task and testing functions. For this to be included in the test code the classes themselves (at least the files simpletest / unit_tester.php and simpletest / reporter.php) SimpleTest with directives require_once. Sam testing code on a plan of development should be allocated in a separate class that inherits from the parent. This object is a class UnitTestCase, which contains the functions necessary to test a variety of values ??that can return the methods of your classes.
The simplest method of this class are the methods and assertFalse assertTrue, which allow you to functions that return Boolean values ??True or False. Thus, verification can be done by calling one of the methods as a parameter is passed to the function under test is necessary for her set of parameters. More precisely, the test functions will be transferred to the output of the method of your class. In the following listing, you can see an example of calling these methods for a class of PHP-logger.
This should give some explanation of the code. The first call assertFalse used to verify the absence of a log file immediately after class initialization. Thus, it is assumed that your log-class creates a text log is not from its constructor (the method of PHP-class with a name that matches the name of the class), but only during the output messages. A test case is written incorrectly in this regard, since the log file is created during class initialization. Thus, calling this method as applied to an existing log class should fail. The next call to the tester method assertTrue checks for a text log file after displaying the message in the log file. As a result of running this test script you get a report (pointing to the incorrect work of the class), shown in the following figure.
In addition to the simplest methods for checking Boolean values ??SimpleTest has a lot of other test functions. Here is a list of test functions that can be used in the current version of SimpleTest:
To send a message in the final report and the automation of certain functions, as well as to test their own health SimpleTest offers a special set of helper methods. The method setUp (setDown) allows you to specify a function to be executed before (after) each invocation of the test method of the class. Function pass () and fail () called in as a test for the classes themselves SimpleTest. The method sendMessage () sends a custom message in the final report. To clear the error queue can apply the method swallowErrors.
To simplify running multiple test scripts (assuming that each class will have your test script) SimpleTest offers a special class GroupTest, which can cause the test suite. In fact, you must create a set of test scripts and register them in separate calls (you can call it main) test script. Thus, you will be able to automatic testing of all functions of your application in one fell swoop.
Using SimpleTest as the tester class for your web application will add not only the stability of his work. You can be sure of the correctness of the code written in any point in development when over the same class of work a few programmers. As the functionality of SimpleTest is a lot of features that allow you to test your application. This article contains very simple examples, which, however, provide an opportunity to enter the testing procedure in your application. If you use Eclipse, you can also use a special extension, which will simplify the process of running the testing code. In the simplest case, as it makes sense to use a simple hyperlink with pop-up window, which will show the results of testing functions in the current code.
The testing process brings several negative factors, such as delays in project development, the need to explore new possibilities of testing classes. For the introduction of functional testing you need to write a lot of extra features. Some of them may be harder to themselves "experimental" techniques. It should, of course, be remembered that from the perspective of the majority of customers are testing procedure – a waste of their time and money. But do not forget the fact that the reliability of your code quality and increase many times. Therefore, even if there are strict time limits in the course of project implementation is to use testing procedures in the most narrow and difficult places in your application.