Unit Testing Your Plugins ![rw-book-cover](https://webdevstudios.com/wp-content/uploads/2022/11/wds-default.png) ## Metadata - Author: [[Camden Segal]] - Full Title: Unit Testing Your Plugins - Category: #articles - Summary: So you have an amazing plugin, and it’s starting to get bigger and the bug reports are piling up (or better you are are starting a new plugin and you wanna do it right). Unfortunately getting PHPUnit to properly function with WordPress is a task that requires some serious configuration. - URL: https://webdevstudios.com/2014/08/07/unit-testing-your-plugins/ So you have an amazing plugin, and it’s starting to get bigger and the bug reports are piling up (or better you are are starting a new plugin and you wanna do it right). Unfortunately getting PHPUnit to properly function with WordPress is a task that requires some serious configuration. Fortunately WP-CLI includes an automated method for getting started with tests in your plugin! Using WP-CLI’s scaffold command we can automatically generate the files necessary to start testing our plugin. The most difficult portion of this process is getting everything installed properly. If you are using [Varying Vagrant Vagrants](https://github.com/Varying-Vagrant-Vagrants/VVV) which I highly recommend with [Alison Barret’s Site Wizard](https://github.com/aliso/vvv-site-wizard) you already have everything you need installed and you can just ‘vagrant ssh’ in and skip down to the “Setup the Plugin” section. #### Installation Install PHPUnit here [http://phpunit.de/getting-started.html](https://phpunit.de/getting-started.html) Install WP-CLI here <http://wp-cli.org/> #### Setup the Plugin Once you have everything installed and working properly it’s just a matter of running a few cli commands. ``` 1. wp scaffold plugin-tests plugin-name 2. # replacing plugin-name with the folder name of your plugin 3. # then move to the plugin directory if you aren't there already 4. cd wp-content/plugins/plugin-name 5. ./bin/install-wp-tests.sh wordpress\_test mysql\_username 'mysql\_password' localhost latest 6. # Replace mysql\_username, mysql\_password, and localhost with 7. # the proper login credentials for your local database. 8. # Once those command have been run you can run 9. phpunit 10. # Which should give you a single successful test ``` #### Writing Tests The wp-cli scaffold command created a few new files in the plugin folder, the most important being the tests folder. PHPUnit will run any tests you place in the tests folder. Included is a file called **test-sample.php** in this file you can find a basic example of a WordPress PHPUnit test. ``` 1. // replace this with some actual testing code ``` In every test’s file you create, setup your test by wrapping them in classes that *extend* *WP\_UnitTestCase*. Every function in the *WP\_UnitTestCase* class that begins with *test* will run automatically. Unit testing relies on assertions to confirm that the behavior your plugin is exhibiting matches the behavior that is expected. In the example above the simple testSample asserts that true is true which should always come out positive unless something is seriously wrong. Let’s say we have a plugin that keeps track of a score for posts using post meta, let’s write a few tests to confirm the basic behavior of the plugin. ``` 1. // Create a new post 2. $post\_id = $this->factory->post->create(); 3. // Get the new posts score 4. $score = my\_plugin\_get\_post\_score( $post\_id ); 5. // Check to confirm the new post’s score is equal to zero 6. $post\_id = $this->factory->post->create(); 7. // Get the posts score after incrementing 8. $score = my\_plugin\_get\_post\_score( $post\_id ); 9. // The posts score should now be 1 10. // it started at 0 and the increment method 11. // Increment the score again, this time by 2 12. // Get the new score 13. $score = my\_plugin\_get\_post\_score( $post\_id ); 14. // Confirm that 2 more points were added to the 15. $post\_id = $this->factory->post->create(); 16. // Reset the post’s score to 0 17. $score = my\_plugin\_get\_post\_score( $post\_id ); 18. // The post’s score should be 0 because it was reset 19. } ``` These three functions test different aspects of the post-scoring plugin’s functionality. *testInitialScore* creates a new post and confirms that on create the *my\_plugin\_get\_post\_score* function returns 0. The *testIncrementScore* function confirms that the *my\_plugin\_increment\_post\_score* function properly increments the score by 1 — the default — or by a specified amount. The *testResetScore* creates a post, gives it several points, resets its score, and confirms that the *my\_plugin\_reset\_post\_score* function properly returned the post’s score to 0. With these tests in place we can easily confirm whether or not the plugin is functioning as it should, and if any changes we make to the plugin break any required functionality. One important thing to notice in the above code is the use of *$this->factory* to create objects to work with. The *WP\_UnitTestCase* class give us these methods to make it easier to test the interaction of WordPress and your plugin by giving you test data. For more information, [this is a pretty nice rundown of functionality available through the factories](http://codesymphony.co/writing-wordpress-plugin-unit-tests/#object-factories). Writing tests for complex functionality can be difficult, both in that the result of the function may be more difficult to confirm, and there is more space for edge cases that break in unexpected ways. If you are running into issues like these, consider breaking down large functions into smaller functions that only do one thing each so that they are simpler to test. In addition, you can integrate writing tests as part of your bug fixing procedure. When a new bug is reported that isn’t covered by a test, write a new test that covers it so you are protected in the future. #### Automation! Once you’ve got PHPUnit up and running and have some tasks in place you can do some really cool automation. Using a task runner like gulp or grunt, you can automatically run the tests whenever a php file is saved, or even before you are allowed to do a git commit. #### Further Reading This is just a basic introduction to using PHPUnit and WordPress. For further reading on more assert methods and use of PHPUnit check out the [official documentation](https://phpunit.de/manual/current/en/index.html)., Unit Testing Your Plugins, Camden Segal, 01gnmd0jyspech8kabv0x3vzc7.