PHPUnit – Database Testing

In this article, we will see how to test database operations using PHPUnit. In this example, we will create a class called GuestBook, which will have a function called addGuest(). The addGuest() will add a row to database table. Using PHPUnit, we will test, whether the addGuest() is adding new row to database table. 1. […]

PHPUnit – Test Suit with XML file

In this article, we will see how to create a test suit with an xml file. In our previous example, we have created a file system based test suit. One of the drawback of the file system based test suit is that, we don’t have any control over the order of execution of test case […]

PHPUnit – Test Suit using file system

Test suit is a collection of test cases. PHPUnit can compose a test suit by recursively traversing through a directory in the file system. In our previous example, we have saved our test case files in the directory tests. PHPUnit can execute all the test cases in the directory tests as a test suite with […]

PHPUnit – Using fixtures for tests

In a unit test, we usually want to do some common codes like object initialization, database connectivity etc. Suppose, if there exists multiple tests in a testing class, then it will be really cumbersome to write same set of codes in all the tests. In order to share common set of codes in multiple tests, […]

PHPUnit – Testing output of ‘echo’ or ‘print’

In this article, we will see how to test the output of echo or print. In our previous articles, we have created a Calculator class with functionalities like sum, subtract and division. In this example, we will add another function called multiply. The multiply function will take two arguments and prints its multiplied values using […]

PHPUnit – Testing Exceptions

In this article, we will see how to test a function that throws an exception when condition meets. In our previous examples, we have created a calculator class with functionalities addition and subtraction. In this example, we will add division and will be tested using PHPUnit to check whether the function can throw exception when […]

A simple PHPUnit TestCase example

In this article, we will see, how to create and test a simple PHP class, say Calculator, using PHPUnit. 1. Create a directory called Calculator with the given below command 2. Add Composer to the directory Calculator First download Composer installer from here and copy it to the directory Calculator. Then execute the given below […]

Setting up PHPUnit for a PHP Project

In this article we will see how to setup PHPUnit for a PHP project. There are two methods by which we can use PHPUnit in a project. Method1 : With PHAR file 1. Download PHAR file for the latest version of PHPUnit at 2. Rename the downloaded PHPUnit phar file 3. Copy the downloaded […]