Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unit testing for WordPress


Published on

WordCamp Kathmandu 2017, Unit testing for WordPress

Published in: Software
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ ◀ ◀ ◀ ◀
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Unit testing for WordPress

  1. 1. Unit Testing for WordPress
  2. 2. Harshad Mane PHP Developer WordPress Enthusiast
  3. 3. No one writes a PERFECT CODE :) Software Testing? It’s impossible to guarantee a bug-free release, but you can minimize the chances by using automated tests. There’s nothing better than ending your day with a clean conscience knowing you did your job right.
  4. 4. Test cases are executed manually without any support from tools or scripts. Manual Testing
  5. 5. Test cases are executed with the assistance of tools, scripts, and software. Automated Testing
  6. 6. Manual/Automated Testing: Pros and Cons Manual Automated Not accurate. More reliable Time Consuming Process Faster than a manual approach. It does not involve in programming task to fetch hidden information. Testers can test complicated application too. Useful in UI Testing Not always!
  7. 7. Automated software testing can increase the depth and scope of tests to help improve software quality. Why do we need automation testing? Lengthy tests that are often avoided during manual testing can be run unattended.
  8. 8. A unit test is a piece of code that exercises another piece of code. What are Unit Tests? A unit test should check that one piece of code, such as a function or method, runs properly and returns the expected results.
  9. 9. How to setup unit tests for your code. We will walk through How to write the unit tests. How to run the tests. How to write a testable code.
  10. 10. Let’s look quickly at a simple example
  11. 11. function hm_get_vat( $quantity ) { return $quantity * 0.15; }
  12. 12. A good unit test for our hm_get_vat() function would try passing in various numbers in various formats to determine if the results are always what we expect them to be.
  13. 13. A unit test is nothing more than a programmatic check to determine if a chunk of code performs the way it is supposed to.
  14. 14. public function test_hm_get_vat() { $obj = new Class; $vat = $obj->hm_get_vat(100); $this->assertEquals( 15, $vat ); }
  15. 15. Setting up the Testing Suite
  16. 16. Easiest way to setup Unit Tests is through WP-CLI WP-CLI, a command line interface for WordPress.
  17. 17. 1. Install PHPUnit phpunit#installation 2. Install WP-CLI tests/
  18. 18. 1. Go to your WordPress install root folder cd /Applications/MAMP/htdocs/phpunit/ 2. Instruct WP-CLI to create the initial unit test files wp scaffold plugin-tests <plugin-slug> This will generate all of the files needed for our unit tests.
  19. 19. wp scaffold child-theme Generate child theme based on an existing theme. wp scaffold plugin Generate starter code for a plugin. wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin. wp scaffold post-type Generate PHP code for registering a custom post type. wp scaffold taxonomy Generate PHP code for registering a custom taxonomy. wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme. wp scaffold _s Generate starter code for a theme based on _s.
  20. 20. The new folders / files created: bin/ tests/ bootstrap.php test-sample.php phpunit.xml .travis.yml These files are the foundation of our plugin’s test suite.
  21. 21. bash bin/ phpunit_test root root localhost latest Above command will create a test database and install all test dependencies.
  22. 22. The testing suite that WP-CLI sets up for us includes one sample unit test It is located in tests/test-sample.php: class SampleTest extends WP_UnitTestCase { function testSample() { // replace this with some actual testing code $this->assertTrue( true ); } }
  23. 23. Writing your First Test
  24. 24. phpunit.xml <phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" > <testsuites> <testsuite> <directory prefix="test-" suffix=".php"d>./tests/ </directory> </testsuite> </testsuites> </phpunit>
  25. 25. This tells PHPUnit where to look for the PHP file tests/bootstrap.php defines a few options, and then also tells PHPUnit where the actual unit tests live Here files which are prefixed with “test-“ are only considered Note: only methods prefixed with “test” will be considered a unit test. All other methods will be skipped.
  26. 26. Things to note down while writing tests Separate the tests into multiple files so that they are logically organized Every class contains an extension of the WP_UnitTestCase class. The name of the class does not really matter, just name it something that makes sense.
  27. 27. Assertions An assertion is one “check” within the the test to determine if a value is equal to what we expect. Tests Group of one or more assertions are Tests
  28. 28. One test with a single assertion: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017'; $this->assertEquals( 'WordCamp Kathmandu 2017', $string ); } One test with a two assertions: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017 - Awesome Event‘; $this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome Event', $string ); $this->assertNotEquals( 'WordCamp Kathmandu 2017', $string ); }
  29. 29. Q&A
  30. 30. Thank you! धन्यवाद :)