On Github dlh01 / wcto2014
David Herrera
See also: QUnit
Next: "Setting up Vagrant for Unit Testing" with Paul Bearne
$ wp scaffold plugin my-plugin
$ ls my-plugin > bin my-plugin.php phpunit.xml readme.txt tests
$ wp scaffold unit-tests my-existing-plugin
$ cd my-plugin/ $ phpunit
<?php // tests/test-sample.php class SampleTest extends WP_UnitTestCase { function testSample() { // replace this with some actual testing code $this->assertTrue( true ); } }
<?php // tests/test-sample.php function testSample() { // replace this with some actual testing code $this->assertTrue( true ); }
<?php // tests/test-sample.php function testSample() { $foo = true; $this->assertTrue( $foo ); }
assertFalse()
assertEquals()
assertInternalType()
assertArrayHasKey() assertCount() assertEmpty() assertFileExists() assertGreaterThan() assertLessThan() assertObjectHasAttribute()
And many more: phpunit.de/manual
<?php class SampleTest extends WP_UnitTestCase { function test_is_true() { $foo = true; $this->assertTrue( $foo ); } function test_is_null() { $foo = null; $this->assertNull( $foo ); } }
<?php class SampleTest extends WP_UnitTestCase { function test_is_true() { /* ... */ } function test_is_null() { /* ... */ } function test_names() { $name = 'Wordpress'; $this->assertEquals( 'WordPress', $name ); } }
<?php // my-plugin.php function say_hello( $name ) { return sprintf( __( 'Hello %s!', 'my-plugin' ), $name ); }
<?php // tests/test-sample.php function test_say_hello() { $expected = 'Hello, David!'; $actual = say_hello( 'David' ); $this->assertEquals( $expected, $actual ); }
<?php // my-plugin.php function say_hello( $name ) { - return sprintf( __( 'Hello %s!', 'my-plugin' ), $name ); + return sprintf( __( 'Hello, %s!', 'my-plugin' ), $name ); }
<?php function rock_wins( $opponent ) { if ( 'scissors' == strtolower( $opponent ) ) { return true; } else { return false; } }
<?php class Test_RPS extends WP_UnitTestCase { }
<?php class Test_RPS extends WP_UnitTestCase { function test_against_scissors() { $actual = rock_wins( 'scissors' ); $this->assertTrue( $actual ); } }
<?php class Test_RPS extends WP_UnitTestCase { function test_against_scissors() { $actual = rock_wins( 'scissors' ); $this->assertTrue( $actual ); } function test_against_paper() { $actual = rock_wins( 'paper' ); $this->assertFalse( $actual ); } }
<?php class Test_RPS extends WP_UnitTestCase { function test_against_scissors() { $actual = rock_wins( 'scissors' ); $this->assertTrue( $actual ); } function test_against_paper() { $actual = rock_wins( 'paper' ); $this->assertFalse( $actual ); } function test_against_banana() { $actual = rock_wins( 'banana' ); $this->assertTrue( $actual ); } }
<?php function rock_wins( $opponent ) { $valid = array( 'rock', 'paper', 'scissors' ); $opponent = strtolower( $opponent ); if ( ! in_array( $opponent, $valid ) ) { return true; } return 'scissors' == $opponent; }
<?php function rock_wins( $opponent ) { if ( ! is_legal_throw( $opponent ) ) { return true; } // ... } function is_legal_throw( $opponent ) { // ... }
<?php function test_is_legal_throw() { $actual = is_legal_throw( 'scissors' ); $this->assertTrue( $actual ); $actual = is_legal_throw( 'rock' ); $this->assertTrue( $actual ); $actual = is_legal_throw( 'banana' ); $this->assertFalse( $actual ); $actual = is_legal_throw( array( 'rock', 'paper', 'scissors' ) ); $this->assertFalse( $actual ); // ... }
function test_bad_option_names() { foreach ( array( '', '0', ' ', 0, false, null ) as $empty ) { $this->assertFalse( get_option( $empty ) ); $this->assertFalse( add_option( $empty, '' ) ); $this->assertFalse( update_option( $empty, '' ) ); $this->assertFalse( delete_option( $empty ) ); } }
function test_returns_false_if_given_an_invalid_email_address() { $data = array( "khaaaaaaaaaaaaaaan!", 'http://bob.example.com/', "sif i'd give u it, spamer!1", "com.exampleNOSPAMbob", "bob@your mom" ); foreach ($data as $datum) { $this->assertFalse(is_email($datum), $datum); } }
function test_demonstrations() { $link = get_post_type_archive_link( 'my-post-type' ); $this->go_to( $link ); }
function test_demonstrations() { $response = wp_update_post( array( 'ID' => 'oops' ), true ); $this->assertWPError( $response ); }
function test_demonstrations() { $post_ID_A = $this->factory->post->create(); $post_ID_B = $this->factory->post->create( array( 'post_title' => 'Hello, Toronto!', 'post_date' => '2014-11-16 09:30:00', ) ); }
function test_demonstrations() { $term_ID_A = $this->factory->term->create(); $term_ID_B = $this->factory->term->create( array( 'name' => 'Term B', 'taxonomy' => 'category', ) ); }
function test_demonstrations() { $user_ID_A = $this->factory->user->create(); $user_ID_B = $this->factory->user->create( array( 'role' => 'subscriber', 'display_name' => 'Aldo Leopold' ) ); }
function test_demonstrations() { $haystack = get_echo( 'the_content' ); $this->assertContains( 'needle', $haystack ); }