DEV Community

Cover image for 10 Of The Best PHP Testing Frameworks For 2021
Shalini Baskaran for LambdaTest

Posted on • Updated on • Originally published at lambdatest.com

10 Of The Best PHP Testing Frameworks For 2021

A framework is a collection or set of tools and processes that work together to support testing and developmental activities. It contains various utility libraries, reusable modules, test data setup, and other dependencies. Be it web development or testing, there are multiple frameworks that can enhance your team’s efficiency and productivity. Web testing, in particular, has a plethora of frameworks, and selecting a framework that suits your needs depends on your language of choice.

Amongst all server-side programming languages, 80% of websites use PHP, and the right framework can make the job easier. We decided to dive deeper into PHP and find out what the best PHP testing frameworks are. In this blog, we will be focusing on automated testing frameworks and will be listing out the best PHP frameworks that will allow you to write your test cases in a standard format.

Choosing The Best PHP Testing Frameworks

If you want to pick the best framework, no matter whether its a new PHP framework or a well-known JavaScript framework, it has to be structured in such a manner that it provides various benefits as listed below-

  • Maintains a well-defined code structure
  • Maintains reusable modules and libraries which can effectively be used for testing, thereby achieving code reusability.
  • Enhances the speed of the testing process
  • Improves test efficiency
  • Avoids code duplication
  • Analyzes test coverage as well as requirement coverage

A framework is an integral part of testing, and hence it is crucial to select the framework type based on our project requirement. First of all, to set up a framework, one must know each framework’s pros and cons. Only then it will be easy to list out our requirements and select specific automated testing frameworks. Once you have an idea about your needs, you can go ahead and choose one amongst the best PHP testing frameworks.

To help you in doing that, let’s take you through the best PHP frameworks in detail.

Best PHP Testing Frameworks Of 2021

Several frameworks have been and are being used for development and testing purposes. But each one is unique in its own way and offers unique features. We will discuss how the best PHP testing frameworks approach test automation and what pros or cons they all offer. Without further ado, let’s look at the best PHP frameworks.

  1. PHPUnit
  2. Codeception
  3. Storyplayer
  4. SeleniumHQ
  5. Behat
  6. Atoum
  7. SimpleTest
  8. PhpSpec
  9. Peridot
  10. Kahlan

As promised above, we will now analyze each of these PHP testing frameworks in detail to help you make the final choice.

1. PHPUnit

PHPUnit is the most commonly used PHP testing framework, and it is considered a programmer-oriented framework. It is mostly preferred for unit testing. It was developed by Sebastian Bergmann and is an instance of the xUnit framework architecture.

Steps To Install

There are a few prerequisites that must be satisfied before installing this framework. Firstly, you need to install a higher version of PHP for installing the latest version of PHPUnit. As per their official documentation, it is recommended to use PHP version 7.3 for PHPUnit 9.3, the latest PHPUnit version. Below are the steps to install PHPUnit in your system.

Step 1: PHPUnit can be installed by downloading the PHAR (PHP Archive) from the below link

https://www.php.net/phar

PHAR has all the required PHPUnit dependencies bundled in a single file.

Step 2: You may also install Composer which manages all the dependencies in the project.

Advantages Of Using PHPUnit Framework

PHPUnit is considered one of the best PHP frameworks due to several reasons. Listed below are some of the advantages of using PHPUnit for automated testing-

  1. It is one of the frameworks which allows us to analyze the code coverage efficiently. Based on an in-depth analysis, it can also generate code coverage reports in HTML and also XML log files with more information. Sometimes there might be a few blocks of code that cannot be tested. In such cases, we can use different annotations like @codeCoverageIgnore, @codeCoverageIgnoreStart, and @codeCoverageIgnoreEnd, which are used to ignore certain code blocks while running through code coverage analysis. We can also run the code coverage analysis for certain code blocks by specifying them with @covers annotation.
  2. While writing the test cases, some tests would be left without any implementations. When executed, those cases return a success message, but it isn’t very meaningful to have such a report. PHPUnit provides an interface that raises an exception when an unimplemented test is run.
  3. All the tests can be grouped together into a suite and run at once with the help of an XML configuration file.A simple XML configuration file would look like below-
      tests/FirstTest.php
      tests/SecondTest.php
      tests/ThirdTest.php
Enter fullscreen mode Exit fullscreen mode

*** Did you know? CSS text-indent property specifies the indentation that lines of inline content in a block are indented from its "normal" position. ***

2. Codeception

Codeception is one of the most widely used PHP testing frameworks, and it has gained immense popularity because of ease of use and its ability to maintain the code modules. It supports three levels of testing, namely acceptance testing, functional testing, and unit testing. It provides multiple modules that can be utilized for testing purposes in a single framework.

Let us see the steps to install Codeception and the advantages of using this automated testing framework.

Steps To Install

Before you install Codeception, you need to know if the prerequisites are met. You will need-

  1. PHP with version greater than 5.6 installed in your machine
  2. Curl has to be enabled

Once you have met the prerequisites, you need to follow the below-mentioned steps to proceed further-

Step 1: Install composer which is used for managing the dependencies in PHP. It helps in declaring the libraries to be used in the project and manages them.

You may install using a setup file or you can even do it manually. You can install the composer from their official website.

Step 2: Install PHAR, and you may install it from the official website of Codeception.

Advantages Of Using Codeception Framework

  1. It supports Unit testing, functional testing, as well as acceptance testing. We can opt for any of the three types of testing or sometimes can opt for all three to test the application effectively. If you are wondering, this is the exact reason why it is also known as a ‘full stack testing framework.’
  2. It uses simple naming conventions, which helps everyone to understand the code. It contains action keywords, assertions, and grabbers. The actions keywords are used to perform user actions like clicking, pressing the keys, filling the input fields, etc. The assertion keywords are used to perform the verification like the one that is done using TestNG and Junit. The grabbers are used to extract the information.
  3. It also provides an option of running the user stories in BDD like JBehave and Cucumber.
  4. It provides various database modules, which would be quite helpful in database testing.
  5. It has a WebDriver module, which could be used for acceptance testing.
  6. It has a REST module, which could be very helpful for testing web services. This module also validates the JSON responses, extracting data from specific JSON tags, verifying xml responses, etc.
  7. It helps integrate with continuous integration tools like Jenkins and Teamcity, which would help generate the test reports.

3. Storyplayer

Storyplayer is an open-source PHP testing framework which is used to perform end to end testing. It is best suited for API automation testing and testing web applications. It provides multiple programming language support, which is one reason behind its popularity as one of the best PHP frameworks.

Steps To Install

Step 1: As it is a PHP testing framework, it requires PHP to be installed before setting up this framework. Currently it supports only Apple OSX Yosemite and Ubuntu Linux Desktop 14.10.

Step 2: It is also required to add some extensions like CURL, JSON, OpenSSL etc. You can read further in their official website.

Advantages Of Using Storyplayer Framework

  1. It offers an open source framework, which is quite useful if you are a newbie in PHP web development. You don’t need to take a subscription or pay a fee to avail the services of Storyplayer.
  2. Another great advantage of using the Storyplayer PHP testing framework is the amazing support it provides for multiple programming languages. This makes PHP development and testing much easier and competitive.

4. SeleniumHQ

Selenium is the one of most popularly used automated testing frameworks that has been extensively used for testing web applications. It has four main components namely-

  1. Selenium IDE
  2. Selenium RC
  3. Selenium WebDriver
  4. Selenium Grid

These components are designed for specific purposes, and the selection of the component purely depends upon our testing requirements.

Steps To Install

To work with Selenium, you need to set up the latest version of PHP. Also, it is required to have Composer, which manages all the dependencies for our project.

Advantages Of Using Selenium Framework

  1. Selenium is an open source automation testing framework.
  2. It has an amazing support community.
  3. It is easy to install, and test case implementation is quite easy as well.
  4. It allows us to run our test cases in different browsers like Google Chrome, Firefox, Safari, Internet Explorer and allows seamless browser compatibility testing.
  5. It executes our test scripts in parallel in multiple browsers and operating systems for faster test execution.
  6. It is also used to perform the keyboard and mouse interactions with the browser.

Did you know? CSS text-orientation property can be used to set the orientation of texts. It has no effect in Western horizontal typographic modes, like English.

How To Overcome The Limitations Of Selenium With LambdaTest Selenium Grid?

LambdaTest offers a great platform to run your automation scripts on a cloud Selenium Grid and achieve faster test execution. There are endless benefits of running the tests in the LambdaTest grid.

  1. Quick and easy setup to run your automation scripts.
  2. Execute your tests in different browsers with different versions.
  3. Integration with various continuous integration and deployment tools.
  4. Allows to define various parameters like browser name, version of the browser, operating system.
  5. Provides the option to take screenshots and record videos while executing the test cases.
  6. Most importantly, it provides 24X7 customer support and detailed documentation.

Below is the simple code to search a product on Google using LambdaTest grid-


<?php 
require 'vendor/autoload.php';

class LambdaTestGrid{

  /*
      Setting up remote driver in LambdaTestGrid
      Params
      ----------
      platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks)
      browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge, Safari)
      version :  Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/
  */
  protected static $driver;

  public function searchTextOnGoogle() {
    # username: replace your username that can be found in your LambdaTest dashboard    
    $LT_USERNAME = "{username}";

    # accessKey:  replace your access token that has been generated once you signed up
    $LT_APPKEY = "{accessKey}";

    $LT_BROWSER = "chrome";
    $LT_BROWSER_VERSION ="70.0";
    $LT_PLATFORM = "windows 10";

    # URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub
    $url = "https://". $LT_USERNAME .":" . $LT_APPKEY ."@hub.lambdatest.com/wd/hub";       

    # setting desired capabilities for the test
    $desired_capabilities = new DesiredCapabilities();
        $desired_capabilities->setCapability('browserName',$LT_BROWSER);
        $desired_capabilities->setCapability('version', $LT_BROWSER_VERSION);
        $desired_capabilities->setCapability('platform', $LT_PLATFORM);
        $desired_capabilities->setCapability('name', "Php");
        $desired_capabilities->setCapability('build', "Php Build");
        $desired_capabilities->setCapability('network', true);
        $desired_capabilities->setCapability('visual', true);
        $desired_capabilities->setCapability('video ', true);
        $desired_capabilities->setCapability('console', true);

        self::$driver = RemoteWebDriver::create($url, $desired_capabilities);      
      //specify the browser URL           
    self::$driver->get("https://www.google.com/");
 //specify the locator of the search box
        $element = self::$driver->findElement(WebDriverBy::name("q"));
    if($element) {
//type the name to be searched in the google search box
      $element->sendKeys("IPhone");
      $element->submit();
    }  
    print self::$driver->getTitle();
    self::$driver->quit();
  }    
}

?>
Enter fullscreen mode Exit fullscreen mode

Once the tests have been executed, you can view the logs and other builds related information in the Automation dashboard. Thus, running the tests in LambdaTest Grid improves the execution speed and also provides efficient results.

5. Behat

Behat is a Behavior Driven Development (BDD) framework for PHP. It was built entirely for PHP, and it has many core PHP modules in it. The code is usually written in simple English language, easily understandable by all the stakeholders in the project. The BDD structure usually contains the Context, Action, and Outcome, and this format is called Gherkin. Below is a simple example of writing tests in the Gherkin language.

Scenario: Login into Facebook and verify if the user is successfully logged in

Given the user enters his username and password
When the user clicks sign in button
Then the user should be successfully logged into his Facebook homepage
Enter fullscreen mode Exit fullscreen mode

These scenarios are written in a file with extension features. For example, loginTest.feature in the above case. Behat is an executable that is used to run the tests from the command line for testing the application exactly similar to the execution of feature files.

Steps To Install

To setup Behat framework make sure you have installed the below prerequisites-

Step 1: PHP latest version.

Step 2: Composer which manages all the PHP dependencies for the project.

Step 3: If you don’t have a Composer, you can easily set up the framework by downloading the latest version of behat.phar.

Advantages Of Using Behat Framework

  1. Easy to install and implement.
  2. Easy to understand the test cases as everything is written in BDD. The tests are written in simple English language, which helps all the project stakeholders understand the requirement and the test cases mapped to it. The scenarios are written in a feature file and consist of the context, the subsequent action, and the final expected result. These are written using the Given, When and Then keywords in Gherkin language.
  3. Detailed documentation which helps everyone to implement the framework easily.
  4. It is an open source testing framework.

6. Atoum

Atoum is another one of the popular PHP testing frameworks which is used for Unit testing. It is very simple and easy to implement, and it is amongst the new PHP frameworks you will come across. Also, it’s among the best PHP frameworks. It provides various inbuilt execution engines such as the inline engine, isolation engine, and concurrent engines. Those are used to execute test cases one by one, one after another test case each time in a new process, and run all the test cases in parallel simultaneously in different processes separately. Atoum also provides advanced features that help to mock the build, minimizing the dependencies for running the tests.

Now let us see the steps involved in the installation of the Atoum framework along with its advantages.

Steps To Install

Before installation, make sure you have the latest version of PHP installed in your system. You can set up an Atoumframework by either one of the ways.

Step 1: By using Composer which can be downloaded from their official website.

Step 2: By using a PHAR file which can be downloaded from this link.

Step 3: By cloning from the Git repository

http://github.com/atoum/atoum.git

Advantages Of Using Atoum Framework

  1. Easy to write test cases based on our convenience.
  2. It provides multiple assertions, which makes them highly effective when implemented in test cases.
  3. Mocking can be quickly done using the Atoum framework, which reduces the waiting time for resolving our dependencies and makes test execution faster.
  4. Generate test reports in different formats.
  5. Atoum Framework can also be integrated with continuous integration tools like Jenkins, Travis CI, CircleCI, etc.
  6. Several plugins can be added to this framework, thereby making the testing framework more stable. Some of the extensions are-
    • json-schema-extension – for validating the JSON tags
    • bdd-extension – for writing BDD style test cases which has better readability
    • reports-extension – for writing code coverage reports

7. SimpleTest

SimpleTest is a PHP testing framework that could be used for unit testing. Apart from unit testing, it is also used for website testing and for mocking objects for PHP web development. This framework’s unique feature is that it has an inbuilt web browser that allows users to navigate to different web pages and perform testing.

Steps To Install

Step 1: Setting up the SimpleTest framework requires the latest version of PHP to be installed in the system.

Step 2: Once it is done you may setup the framework by downloading from their official website.

Advantages Of Using SimpleTest Framework

  1. SimpleTest is very useful for unit testing.
  2. It provides an extensive support for forms, SSL, frames, proxies, and basic authentication.
  3. It has an inbuilt web browser, which helps in testing web applications.
  4. Using this PHP testing framework, tests can be executed through the browser as well as the command line.
  5. It can also be used in collaboration with PHPUnit, another one of the best PHP testing frameworks we discussed above.
  6. SimpleTest is highly flexible as it allows result or output customization.

8. Phpspec

Phpspec is another Behavior Driven Development (BDD) framework that is used to write and execute tests written in PHP. It is well-known for driving the design according to the specifications provided by the developer.

A BDD framework contains specifications written in the English language, which helps in understanding the tests. It describes the behavior of the application using Gherkin language.

Steps To Install

Step 1: Before installing Phpspec, you need to install PHP 5.6 or 7 in your system.

Step 2: Once done, you can install using Composer, which automatically manages all the project dependencies. While installing Composer, make sure that the autoload settings of the composer are correct. Phpspec won’t be able to detect classes unless the settings are correctly specified.

Step 3: Once you have finished installing Composer, you can install Phpspec using the following command-

composer require –dev Phpspec/Phpspec
Enter fullscreen mode Exit fullscreen mode

After everything is done, you will notice that all your dependencies have been successfully installed in the vendor folder. And the executable file will be found in vendor/bin/phpspec.

Advantages Of Using Phpspec Framework

  1. Phpspec automatically generates skeletons for the classes and the methods.
  2. It also has a mocking framework.
  3. It provides 14 in-built matchers to verify the results and describe how our tests should behave. Some of the commonly used matchers are comparison matchers, approximately matchers, type matchers, and identity matchers.
  4. This framework can be extended by configuring plugins like the DataProvider plugin, framework integration plugin, code generation plugin, and matchers plugin.

Did you know? CSS touch-action property controls filtering of gesture events, providing developers with a declarative mechanism to selectively disable touch scrolling (in one or both axes) or double-tap-zooming.

9. Peridot

Peridot is another one of the widely used PHP testing frameworks, which is a type of BDD framework. Some developers even claim that it is one of the best PHP frameworks available for a BDD approach. The tests are written in describe-it syntax in Peridot, which makes it simpler to read, understand, and maintain. It is considered one of the fastest among the PHP framework as it loads and runs the test suites quickly and runs them concurrently.

Below is a simple code for writing tests in Peridot using describe-it syntax

describe('Welcome page', function() {
    it(must have a welcome message, function() {
        $this-&gt;getPage('http://myTestWebsite.com');
        $message = $this-&gt;findElementById(welcmeMessage);
        assert($message-&gt;getText() === "Hello", "should be Hello");
    });
});
Enter fullscreen mode Exit fullscreen mode

Steps To Install

Step 1: You can easily install this using Composer which will manage our project dependencies.

Step 2: It can also be installed manually by downloading the PHAR.

Advantages Of Using Peridot Framework

  1. Peridot uses the familiar describe-it syntax to create a clear and readable testing language.
  2. It can be extended with various types of plugins, which can be used for customizing our test framework.
  3. It can be integrated with WebDriver and databases for executing the tests.
  4. Peridot is lightweight and is faster when compared to other PHP frameworks like Phpspec and PHPUnit.
  5. It also helps in executing the integration tests at high speed.
  6. It has many reporters like Peridot List reporter, concurrency reporter, and code coverage reporter, each of which are uniquely designed for specific purposes.

10. Kahlan

Kahlan is a unit and behavior-driven PHP framework that uses a describe-it syntax similar to peridot. Without any PHP extensions, we can directly stub or monkey patch our code. It is also an open source framework, which makes it highly preferable for testing purposes.

So, what is a describe-it syntax?

Kahlan uses describe-it syntax for easy maintenance and understanding of the tests. The “describe” keyword usually contains the method specs. The “it” keyword” contains the code to be tested, which should be kept short and clear.

Below is a simple code which provides a clear idea of the describe-it syntax

describe("myFunction", function() {
    describe("::match()", function() {
        it("passes if true === true", function() {
            expect(true)-&gt; myFunction (true);
        });
    });
});
Enter fullscreen mode Exit fullscreen mode

Steps To Install

Step 1: Installation of Kahlan requires PHP of version more than 5.5 to be installed in the system. If code coverage analysis is required, phpdbg or xdebug is also required. Xdebug is a PHP extension which could be used for debugging purposes.

Step 2: As a PHP framework it is always recommended to install using Composer as it helps in managing the project dependencies.

You can also manually set up the framework by referring to the official website of Kahlan.

Advantages Of Using Kahlan Framework

  1. It provides a lot of matchers that can be used for testing various scenarios.
  2. It provides stub options which can be used to resolve the dependencies.
  3. It has built in reporters and exporters.
  4. It is easily extendable and can be customized as per our requirements.
  5. It allows us to use setUp and tearDown methods like beforeAll, beforeEach, afterEach, and afterAll.
  6. Kahlan has inbuilt code coverage exporters, which can be used to get detailed information on the code coverage.

Wrapping Up!

In this blog, we have walked you through the importance of PHP frameworks, different types of PHP frameworks, steps to set up, and the advantages of using the framework. Depending on the requirements, you can choose the best PHP frameworks. These PHP testing frameworks can be used for both PHP web development as well as testing purposes. Thus, implementing the correct framework would help you to perform testing effectively.

We hope you now have a clear idea of selecting a suitable PHP framework for your project. And now, we would like to hear from you – have you implemented any other PHP framework that has proved useful?

If you found this blog valuable, we’d be grateful if you could share it on your LinkedIn or Twitter.

Top comments (0)