SourceForge.net Logo
prevtopnext
izh_test
    Use cases

PHP-pages.

Testing of php-pages is described 'in detail' in the corresponded specification of the test php .

Here you can find an attempt to write small and understandable but at the same time quite complete and wide example of test for php-page. Because of such different goals (small size and wideness) the sample looks slightly artificial but useful.

So, let's test first page "registration" of some web-site.

Let's store information about registered users in a mysql-table. Script to initialize empty database will be named create_db.sql and it will contain next text:
create database if not exists php_tests;
use php_tests;

create table Account(
  Account_id   int      NOT NULL primary key auto_increment,
  login        char(50) NOT NULL,
  password     char(50) NOT NULL,
  name         char(50) NULL,
  account_type enum('user','admin','programmer'),

  unique index (login),
  index Login_password (login,password)
) TYPE=MyISAM;

Let's prepare script to destroy test database (drop_db.sql). This script can be used as example to discard previous variant of database:
drop database if exists php_tests;

Let's prepare script with test data for accurate testing (fill_test_accounts.sql):
use php_tests;

insert into Account
  (login,     password,     name,     account_type)
values
  ("tlogin0", "tpassword0", null,     "user"),
  ("tlogin1", "tpassword1", "tname1", "user"),
  ("tlogin2", "tpassword2", "tname2", "admin"),
  ("tlogin3", "tpassword3", "tname3", "programmer");

To check state of DBMS let's prepare special script (check_accounts.sql):
use php_tests;

select * from Account order by Account_id desc\G

To repair state of DBMS after test easily let's registrate logins with prefix "test_" from our tests. So we will be able to restore state of DBMS after test by simple script (clear_test_accounts.sql):
use php_tests;

delete from Account where login like "test_%";

repair table php_tests.Account use_frm;
Query repair is used to reset counter of autoincrement field and to produce repeatable result.

Before executing of test we should do some preparations.

  1. start mysql server.
  2. creare database.
  3. fill database with test data.
Let's describe all the nesessary actions and substitusion variables in separate file (declaration.iti.xml) to use it in other tests:
<declarations>

  <params>
    <par><name>path_to_mysql</name> <value>C:\mysql\bin</value></par>
    <par><name>php</name>           <value>C:\php\cli\php.exe </value></par>
    <par><name>mysql</name>         <value>%path_to_mysql%\mysql.exe </value></par>
  </params>

  <test_templates>

    <template>
      <name>safe_start_mysqld</name>
      <test>
        <test_script>
          <spec><fname>safe_start_mysqld</fname></spec>
          <items>
            <if_property_not_equal>
              <name>mysqld_on</name>
              <value>yes</value>
              <items>
                <echo>## start mysqld ##</echo>
                <start>%path_to_mysql%\mysqld.exe</start>
                <sleep>5000</sleep>
                <set_property>
                  <name>mysqld_on</name>
                  <value>yes</value>
                </set_property>
              </items>
            </if_property_not_equal>
          </items>
        </test_script>
      </test>
    </template>

    <template>
      <name>safe_prepare_schema</name>
      <test>
        <test_script>
          <spec><fname>safe_prepare_schema</fname></spec>
          <items>
            <if_property_not_equal>
              <name>mysqld_schema</name>
              <value>flat_trade</value>
              <items>
                <echo>## drop databases ##</echo>
                <mysql><script>..\db\drop_db.sql</script></mysql>
                <echo>## create databases ##</echo>
                <mysql><script>..\db\create_db.sql</script></mysql>
                <set_property>
                  <name>mysqld_schema</name>
                  <value>flat_trade</value>
                </set_property>
              </items>
            </if_property_not_equal>
          </items>
        </test_script>
      </test>
    </template>

    <template>
      <name>safe_prepare_test_data</name>
      <test>
        <test_script>
          <spec><fname>safe_prepare_test_data</fname></spec>
          <items>
            <if_property_not_equal>
              <name>mysqld_content</name>
              <value>test</value>
              <items>
                <echo>## fill test data ##</echo>
                <mysql><script>..\db\fill_test_accounts.sql</script></mysql>
                <set_property>
                  <name>mysqld_content</name>
                  <value>test</value>
                </set_property>
              </items>
            </if_property_not_equal>
          </items>
        </test_script>
      </test>
    </template>

    <template>
      <name>safe_start_simple_test</name>
      <test>
        <test_script>
          <spec><fname>safe_start_simple_test</fname></spec>
          <items>
            <call_template><name>safe_start_mysqld</name></call_template>
            <call_template><name>safe_prepare_schema</name></call_template>
            <call_template><name>safe_prepare_test_data</name></call_template>
          </items>
        </test_script>
      </test>
    </template>

  </test_templates>

</declarations>

Let's use as registration data login, password, name and "type" of account (user, admin, account).

There are the cases that we should test for our page:

  1. Entered empty login
  2. Entered different passwords
  3. Entered empty passwords
  4. User with such an account is already registered
  5. Successful registration of user with empty name
  6. Registration of users having various types:
    1. Registration of user of type "user"
    2. Registration of user of type "admin"
    3. Registration of user of type "programmer"
    4. Wrong name of type for user was passed to the registration script ("unknown_type")
Note that after successful registration there should be changed state of the DBMS and session variables (after successful registration session is started at once for the new registered user). Let's store identifier of registered user in the session variable account_id.

To test html page let's use test php , and to work with DBMS let's use test command mysql . To test successful registration of users with various types let's use test iterate , and let's join all the tests by test package .

See full specification of test below:
<package>

  <spec>
    <sname>registrate</sname>
    <fname>tests for sample_registrate php form</fname>
    <descr>
      This is the test to illustrate testing 
      of php-pages using izh_test.
    </descr>
  </spec>

  <include>
    <declarations_from>declaration.iti.xml</declarations_from>
  </include>

  <on_start_terminal>
    <call_template><name>safe_start_simple_test</name></call_template>
  </on_start_terminal>

  <tests>

    <php>
      <spec><sname>empty_login</sname></spec>
      <script>..\php\registrate.php</script>
      <out>r\empty_login.html</out>
      <post>
        <par><name>login</name><value/></par>
        <par><name>image_path</name><value>../../</value></par>
      </post>
    </php>

    <php>
      <spec><sname>wrong_password</sname></spec>
      <script>..\php\registrate.php</script>
      <out>r\wrong_password.html</out>
      <post>
        <par><name>login</name><value>login</value></par>
        <par><name>pass1</name><value>pass1</value></par>
        <par><name>pass2</name><value>pass2</value></par>
        <par><name>image_path</name><value>../../</value></par>
      </post>
    </php>

    <php>
      <spec><sname>empty_password</sname></spec>
      <script>..\php\registrate.php</script>
      <out>r\empty_password.html</out>
      <post>
        <par><name>login</name><value>login</value></par>
        <par><name>pass1</name><value/></par>
        <par><name>pass2</name><value/></par>
        <par><name>image_path</name><value>../../</value></par>
      </post>
    </php>

    <php>
      <spec><sname>existed_login</sname></spec>
      <script>..\php\registrate.php</script>
      <out>r\existed_login.html</out>
      <post>
        <par><name>login</name><value>tlogin1</value></par>
        <par><name>pass1</name><value>pass</value></par>
        <par><name>pass2</name><value>pass</value></par>
        <par><name>type</name> <value>programmer</value></par>
        <par><name>image_path</name><value>../../</value></par>
      </post>
      <before><mysql><script>..\db\clear_test_accounts.sql</script></mysql></before>
      <after>
        <mysql>
          <script>..\db\check_accounts.sql</script>
          <etalon>r\test_data.accounts.txt</etalon>
        </mysql>
      </after>
    </php>

    <php>
      <spec><sname>empty_name</sname></spec>
      <script>..\php\registrate.php</script>
      <out>r\empty_name.html</out>
      <check_session>true</check_session>
      <post>
        <par><name>login</name><value>test_login</value></par>
        <par><name>pass1</name><value>pass</value></par>
        <par><name>pass2</name><value>pass</value></par>
        <par><name>name</name> <value/></par>
        <par><name>type</name> <value>user</value></par>
        <par><name>image_path</name><value>../../</value></par>
      </post>
      <before><mysql><script>..\db\clear_test_accounts.sql</script></mysql></before>
      <after>
        <mysql>
          <script>..\db\check_accounts.sql</script>
          <etalon>r\empty_name.accounts.txt</etalon>
        </mysql>
        <mysql><script>..\db\clear_test_accounts.sql</script></mysql>
      </after>
    </php>

    <iterate>
      <spec><sname>types_of_account</sname></spec>
      <names><name>type</name></names>
      <tuples>
        <tuple><value>user</value></tuple>
        <tuple><value>admin</value></tuple>
        <tuple><value>programmer</value></tuple>
        <tuple><value>unknown_type</value></tuple>
      </tuples>
      <template>
        <php>
          <spec><sname>%type%</sname></spec>
          <script>..\php\registrate.php</script>
          <out>r\type\%type%.html</out>
          <check_session>true</check_session>
          <post>
            <par><name>login</name><value>test_login</value></par>
            <par><name>pass1</name><value>pass</value></par>
            <par><name>pass2</name><value>pass</value></par>
            <par><name>name</name> <value>test_name</value></par>
            <par><name>type</name> <value>%type%</value></par>
            <par><name>image_path</name><value>../../../</value></par>
          </post>
          <before><mysql><script>..\db\clear_test_accounts.sql</script></mysql></before>
          <after>
            <mysql>
              <script>..\db\check_accounts.sql</script>
              <etalon>r\type\%type%.accounts.txt</etalon>
            </mysql>
            <mysql><script>..\db\clear_test_accounts.sql</script></mysql>
          </after>
        </php>
      </template>
    </iterate>

  </tests>
</package>

Ordinary web portal contains usually other pages except registration one. Let's write "complex test" included tests for all the pages on our site in the future. For now this test will include only registration part (php_sample.itd.xml):
<package>

  <spec>
    <sname>sample</sname>
    <fname>tests for sample php project</fname>
    <descr>
      This is the test to illustrate testing 
      of php-pages using izh_test.
    </descr>
  </spec>

  <tests>
    <link>registrate.itd.xml</link>
  </tests>

</package>

Ready sample can be downloaded from http://www.izhsoft.com/downloads.html or from http://www.sourceforge.org/projects/izh-test/ .

To use the sample you should:

  1. Download and unpack it to a directory.
  2. Prepare mysql-server. The sample was tested with version 4.1.7. You can download and install mysql from http://www.mysql.com .
  3. Prepare php. The sample was tested with version 4.3.0. You can download and install php from http://www.php.net/ .
  4. Write right path to php and mysql in file tests\declarations.iti.xml .
  5. Write right parameters of connection with mysql in file php\registrate.php (function db_connect) .
  6. Execute command line
    itr tests\php_sample.itd.xml
    or
    itr tests\php_sample.itd.xml --gui


prevtopnext

SourceForge.net Logo