unit testing in wordpress
TRANSCRIPT
“A unit test is an automated piece of code that invokes a unit of work in the system and then
checks a single assumption about the behavior of that unit of work.”
PHPUnit
CLI tool that runs unit tests & reports their results.
Created by Sebastian Bergmann
Easy to install
Unit Tests Requirements
Each test should be able to run on its own (isolated)
Easy to read
Quick to execute
Self explaining function names
Unit Tests Requirements
Test classes should inherit from WP_UnitTestCase (which inherits from PHPUnit_Framework_TestCase)
Test functions should start w/ test (test_my_test_name)
–William C. Wake
“This test case fits a pattern I call ‘Arrange, Act, Assert’: it sets up data, calls a method
under test, and then verifies that it worked as expected.”
Assertion
assertEqual() assertTrue()assertNotNull()assertContains()assertGreaterThan()
https://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions
function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); // Act $u_query = new WP_User_Query( array( 'role' => 'subscriber' ) ); // Assert $this->assertEquals( 2, count( $u_query->get_results() ) );}
function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); // Act $u_query = new WP_User_Query( array( 'role' => 'subscriber' ) ); // Assert $this->assertEquals( 2, count( $u_query->get_results() ) ); }
function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); // Act $u_query = new WP_User_Query( array( 'role' => 'subscriber' ) ); // Assert $this->assertEquals( 2, count( $u_query->get_results() ) ); }
function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); // Act $u_query = new WP_User_Query( array( 'role' => 'subscriber' ) ); // Assert $this->assertEquals( 2, count( $u_query->get_results() ) );}