@jukebox42
class Unicorns {
private $count = 8;
public function getCount() {
return $this->count;
}
public function steal($number) {
$this->count -= $number;
}
}
class UnicornsTest extends PHPUnit_Framework_TestCase {
public function test_can_steal() {
// Setup
$unicorns = Unicorns();
// Action
$unicorns->steal(5);
// Assert
$this->assertEquals(3, $unicorns->getCount()); }
}
assertEquals($expected, $actual [, $message = '']);
$ phpunit tests/unicorns.php
PHPUnit 3.4.0 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 0.5Mb
OK (1 tests, 1 assertions)
class Unicorns() {
private $count = 8;
public function getCount() {
return $this->count;
}
public function steal($number) {
// Make sure we can't have negative unicorns
if($this->count - $number >= 0) { $this->count -= $number;
} }
}
class UnicornsTest extends PHPUnit_Framework_TestCase() {
public function test_can_steal() {
$unicorns = Unicorns();
$unicorns->steal(5);
$this->assertEquals(3, $unicorns->getCount());
}
public function test_cant_steal() {
$unicorns = Unicorns();
$unicorns->steal(1000);
$this->assertEquals(8, $unicorns->getCount());
}}
$ phpunit tests/unicorns.php
PHPUnit 3.4.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 0.5Mb
OK (2 tests, 2 assertions)
class Unicorns() {
private $count = 8;
public function getCount() {
return $this->count;
}
public function steal($number) {
$this->count -= $this->count - $number >= 0 ? $number : 0; }
}
class UnicornsTest extends PHPUnit_Framework_TestCase() {
public function test_can_steal() {
$unicorns = Unicorns();
$unicorns->steal(5);
$this->assertEquals(3, $unicorns->getCount());
}
public function test_cant_steal() {
$unicorns = Unicorns();
$unicorns->steal(1000);
$this->assertEquals(8, $unicorns->getCount());
}
}
$ phpunit tests/unicorns.php
PHPUnit 3.4.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 0.5Mb
OK (2 tests, 2 assertions)
class Unicorns() {
private $count = 8;
public function getCount() {
return $this->count;
}
public function steal($num) {
$this->count -= $this->count - $num >= 0 ? $num : $this->count;
}
}
$ phpunit tests/unicorns.php
PHPUnit 3.4.0 by Sebastian Bergmann.
.F
Time: 0 seconds, Memory: 0.5Mb
There was 1 failure:
1) UnicornsTest::test_cant_steal
Failed asserting that 0 matches expected 8.
/path/to/test/unicorns.php:11
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
$ phpunit tests/
PHPUnit 3.4.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 0.5Mb
OK (2 tests, 2 assertions)
class UnicornsTest extends PHPUnit_Framework_TestCase() {
protected function setUp() {
// Create the unicorns object for each test
$this->unicorns = Unicorns();
}
public function test_can_steal() {
$this->unicorns->steal(5);
$this->assertEquals(3, $this->unicorns->getCount());
}
public function test_cant_steal() {
$this->unicorns->steal(1000);
$this->assertEquals(8, $this->unicorns->getCount());
}
}
class UnicornsTest extends PHPUnit_Framework_TestCase() {
protected function tearDown() {
// Clean up the mess you've made
}
public function test_can_steal() {
$this->unicorns->steal(5);
$this->assertEquals(3, $this->unicorns->getCount());
}
public function test_cant_steal() {
$this->unicorns->steal(1000);
$this->assertEquals(8, $this->unicorns->getCount());
}
}
class ReactorTest extends PHPUnit_Framework_TestCase() {
public function test_not_over() {
$reactor = Reactor();
$reactor->setTemp(9000);
$this->assertLessThan(100, $reactor->getTemp());
}
}
PHPUnit 4.1.0 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) ReactorTest::test_not_over
Failed asserting that 9000 is less than 100.
/path/to/test/reactor.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
class RegExpTest extends PHPUnit_Framework_TestCase {
public function testFailure() {
$this->assertRegExp('/foo/', 'bar');
}
}
Output
PHPUnit 4.1.0 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) RegExpTest::testFailure
Failed asserting that 'bar' matches PCRE pattern "/foo/".
/path/to/test/RegExpTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
class StringEndsWithTest extends PHPUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertStringEndsWith('suffix', 'foo');
}
}
PHPUnit 4.1.0 by Sebastian Bergmann.
F
Time: 1 second, Memory: 5.00Mb
There was 1 failure:
1) StringEndsWithTest::testFailure
Failed asserting that 'foo' ends with "suffix".
/path/to/test/StringEndsWithTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
class Unicorns() {
private $count = 8;
public function getCount() {
return $this->count;
}
public function steal($num) {
if ($num > $this->count) {
throw new Exception('Not enough unicorns to steal');
}
$this->count -= $num;
}
}
class UnicornsTest extends PHPUnit_Framework_TestCase() {
/**
* @expectedException Exception
* @expectedExceptionMessage Not enough unicorns to steal
*/
public function test_cant_steal() {
$unicorns = Unicorns();
$unicorns->steal(1000);
}
}
PHPUnit 3.4.0 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 0.5Mb
OK (1 tests, 1 assertions)
class DependsTest extends PHPUnit_Framework_TestCase {
public function test_one() {
$this->assertTrue(false);
}
/**
* @depends test_one
*/
public function test_two() {
$this->assertTrue(true);
}
}
This does NOT define the order the tests will get run. This means that when you run the test, if a dependent fails, then the output will declare any test depending on it as "skipped".
class TimeTest extends PHPUnit_Framework_TestCase {
/**
* @small
*/
public function test_two() {
$result do_a_bunch_of_crazy_things();
$this->assertTrue($result);
}
}
/**
* @requires extension mysqli
*/
class DatabaseTest extends PHPUnit_Framework_TestCase {
...
}
class DatabaseTest extends PHPUnit_Framework_TestCase {
public function setUp() {
if(something_is_not_met()) {
$this->markTestSkipped('Don\'t run this test!'); }
}
}
$ phpunit --coverage-html sometest.php