WordPress – Install PHPUnit For WordPress

Tram Ho

As the title I mentioned, this article will guide you how to integrate PHPUnit for WordPress .

You can visit your original article to see more details:


The reason we need to set up Unit Test for WordPress :

  • Check the accuracy of the Plugin or Function that we write through Testing.
  • It is possible to apply TDD (Test Driven Development) to be able to write Function better.
  • Being able to write Unit Test will give you a higher salary: laughing:: laughing:: laughing :.

Note: This article is written based on MacOS so it will work well on Linux, but other systems I do not know.

Here are a few steps to install PHPUnit for WordPress :

Install PHPUnit

To be able to use PHPUnit , we first need to install it first. There are many options but here I choose to install them using Wget through some commands as follows:

Install WP-CLI

WP-CLI (WordPress Command-line Interface) is a combination of commands used to interact with WordPress. In addition WP-CLI has a lot of commands that you cannot do in the Admin Dashboard

For example:

Install WP-CLI through Homebrew with the command below or you can refer to here for some other installation methods.

alt text Once installed, it should look like this

Install PHPUnit for WordPress

After completing both steps above, next we will configure PHPUnit for WordPress via WP-CLI with the command below:

In the above command, you just need to replace tên-plugin -name with the name of your Plugin folder.

Note: remember to run the above command at the floder location of the Plugin.

alt text After running it will create the same folder as above

Next is to configure a separate database to test your Plugin. You guys run the command below right at your Plugin directory.

alt text it will look like this

Write the first Unit Test

First, you need to remove the code below in the file phpunit.xml.dist . The yellow line below.

alt text

Next I will try to write a function to test in the test-sample.php file with the following content:

Then run the phpunit command and enjoy the results as shown below:

alt text

The next article is over. Any questions please leave a comment below.

Video tutorials will update this Wednesday.

See you again in an upcoming article, I will basically teach how to write Unit Test on WordPress . ahihi 😆😆😆

Share the news now

Source : Viblo