Testing PHP

Andres Baravalle

Testing PHP

  • Software testing
  • Using Putty
  • Using Codeception

Software testing

Software testing

"Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test.

Software testing can also provide an objective, independent view of the software [...]. Test techniques include, but are not limited to the process of executing a program or application with the intent of finding software bugs". (Wikipedia, 2013)

Software testing (2)

"Software testing can be stated as the process of validating and verifying that a computer program/application/product:

  • meets the requirements that guided its design and development,
  • works as expected,
  • can be implemented with the same characteristics,
  • and satisfies the needs of stakeholders."

(Wikipedia, 2013)

Static vs Dynamic testing

Static testing is a form of software testing focusing on checking the sanity of the code. This type of testing is often used by the developer who wrote the code, but can be used also in team environments.

Common approaches used for static testing include code reviewsinspections and software walkthroughs. Most modern editors for programmers support some automatic code reviews to some extent.

Dynamic testing refers to the examination of the response from the system when a software runs. It involves working with the software, giving input values and checking if the output is as expected by executing specific test cases.

Testing levels

The main levels of testing during the development process are:

  • Unit testing
  • Integration testing
  • System testing
  • Acceptance testing

Unit testing

Unit testing refers to tests that verify the functionality of a specific section of code, usually at the function level (procedural programming) or at class level (OO programming).

These types of tests are usually written by developers as they work on code, to ensure that the specific function is working as expected.

In PHP, unit testing is typically done using PHPUnit.

Integration testing

Integration testing is used to verify the interfaces between components. Integration tests aim to test a web application as a whole and is specifically important when different developers are working at different parts of the system.

"Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly." (Fowler, 2006) 

Integration testing (2)

There is no de-facto standard for integration testing in PHP applications; tools that can be used to do integration testing of PHP applications include CruiseControl and Hudson

Systems testing

System testing tests a completely integrated system to verify that it meets its requirements.  A PHP as well as working as expected in an environment that reflects the real-use conditions.

Example of system testing tecniques used to test web applications include:

  • Graphical user interface testing
  • Usability testing
  • Software performance testing
  • Load testing and stress testing
  • Security testing
  • Installation testing

Acceptance testing

Acceptance testing is the final  test conducted to determine if the requirements of a specification are met.

Expecially when it comes to web applications, testing the controller, or the model in isolation doesn't prove your application is working.

Testing your web applications

At this stage, we are not going to cover unit, integration or system testing any further - but we are going to focus on acceptance testing using CodeCeption.

Before we start using CodeCeption, you need to familiarise yourself with another tool - Putty.

Using Putty

Putty

PuTTY is an Open Source software that can be used as a terminal emulator.

It allows remote access to a server, and we are going to use it to connect through SSH to mastodon.

This will allow us to run some PHP scripts that require command-line access.

Connecting to a server

In the Host Name (or IP address) field enter the name of the computer you wish to connect to, select SSH as protocol and open the connection.

Now I'm connected - what's next?

You might already be familiar with the main main Linux shell commands (cd, mkdir, rm, chmod) but we are going to review them in the next slides.

You are going to need them. If you need a full guide, you can start with this tutorial.

Linux shell commands: cd, mkdir & rm

Command Explanation Example
cd To switch to any other directory cd /home/andres2/
mkdir To create folder mkdir /home/andres/tmp
rm To delete any file rm test.php
To delete a folder and all the files inside rm -Rf /home/andres/tmp

Linux shell commands: chmod

chmod is used to set the permissions for your files.

Permissions tell UNIX what can be done with that file and by whom. There are three operations you can (or can't) perform with a given file or folder:

  • read it
  • write (modify) it and
  • execute it

Unix permissions specify what can the owner do, what can the owner's group do, and what can everybody else do with the file.

Linux shell commands: chmod (2)

Basic permissions are set using a triplet. The first number identifies the permission for the user, the second for the group and the third for everyone else.

Your folders should have a permission of 755 (see below) and your PHP files should have a permission of 644.

  value
read 4
write 2
execute 1

Each permission digit is calculated by summing the value permissions that should be allocated. For now, you should not need to change the permissions - and you can use WinSCP to do it through a graphical interface.

Linux shell commands: running PHP from the command line

We have already been invoking PHP from the command line (e.g. in Notepad++).

The next activities use a similar approach - but require you to have access to the development environment in Mastodon.

Using CodeCeption

CodeCeption

In the next slides we are looking at how to use CodeCeption to test our PHP applications.

CodeCeption can be used to run a wide range of tests, but we are going to focus on acceptance testing.

To use CodeCeption you need command line access. You cannot run the tests simply from a browser interface, but you will need a shell to bootstrap your test environment and create your skeleton files.

To install and bootstrap CodeCeption, please complete the next activity. You will also need an application to test; if you do not have an application, you can test the form file that we have written earlier on.

Activity #1: installing and bootstrapping CodeCeption

Follow the tutorial here and:

  1. Log in into Mastodon using Putty and get into your public_html folder
  2. Install CodeCeption in the root of your application
  3. Write a basic test to check one of your previous PHP scripts
  4. Run the test!

CodeCeptions: testing a home page


<?php  
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that frontpage works');
$I->amOnPage('/');
$I->see('Home');
?>

Activity #2: testing your old activities

Write some basic tests for your older activities from the "PHP and NoSQL" lessons:

  • Activity #1: you can test if student u1111112 appears in the page
  • Activity #3: test if the data is saved to MongoDB (by looking at any "success" strings)

CodeCeptions: testing a form

You can use CodeCeption to test is features work as expected. For example, to see if a form is accepting a correct combination of user names and passwords (or refusing a wrong one).

CodeCeptions: testing a form (2)

<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');
$I->wantTo('log in as regular user');
$I->amOnPage('/lecture9_form.php');
$I->fillField('name','john');
$I->fillField('password','savage');
$I->click('submit');
$I->expect('to be logged in');
$I->see('You are now logged in in the system.')
?>
            

CodeCeptions: testing a form (3)

This is what you can see after calling the CodeCeption test for that form page:

Test, test, test!

This work

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License

Creative Commons License