Created by Tim Klever
Contact: tim@timisawesome.com - @timklever
Two Parts:
test1
/test/
noun
verb
Production code is
CONSTANTLY
being tested
Manual Testing is:
But it does work!
Types of Automated Tests
Take away my people, but leave my factories, and soon grass will grow on the factory floors.
Take away my factories, but leave my people, and soon we will have a new and better factory.- Andrew Carnegie
Tests Facilitate
Communication
about Software
Feature: Serve coffee
In order to earn money
Customers should be able to
buy coffee at all times
Scenario: Buy last coffee
Given there are 1 coffees left in the machine
And I have deposited 1 dollar
When I press the coffee button
Then I should be served a coffee
behat/behat
PHP implementation of Cucumber
Executes a series of "steps" defined by the keywords Given, When & Then
phpunit/phpunit
Arrange - Act - Assert
Test Functionality in isolation in order to eliminate outside variables
class SomeTest extends PHPUnit_Framework_TestCase()
{
public function testCanCount() {
// Arrange
$counter = new Counter();
// Act
$counter->add(5);
// Assert
$this->assertEquals(5, $counter->getCount());
}
}
When we are writing a test in which we cannot (or chose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn't have to behave exactly like the real DOC; it merely has to provide the same API as the real one so that the SUT thinks it is the real one
- Gerard Meszaros
class SomeTest extends PHPUnit_Framework_TestCase()
{
public function testCanDouble() {
// Arrange
$fetcher = $this->getMock('SomeFetchingInterface');
$fetcher->expects($this->any())
->method('getNumber')
->will($this->returnValue(5));
$doubler = new Doubler($fetcher);
// Act
$output = $doubler->double();
// Assert
$this->assertEquals(10, $output);
}
}
Unit Testing Best Practices
How comprehensively are we testing our code?
Line Coverage is a filthy liar!
Test our Test's Testing
humbug/humbug
Attempts to simulate branch coverage
Not all mutations are harmful
A method to describe the ACTUAL behavior of EXISTING software
sebastianbergmann/de-legacy-fy
Uses XDebug execution trace data to test existing untested software
public function testContactInformationForSpeaker()
{
$this->assertTimKlever($this->speaker);
$this->assertEquals("tim@timisawesome.com",
$this->speaker->getEmailAddress()
);
$this->assertEquals("@timklever",
$this->speaker->getTwitterUsername()
);
}