Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test logiciel : Passé, Présent, Futur (Forum PHP)

Test logiciel : Passé, Présent, Futur (Forum PHP)

Le test logiciel est devenu une importante préoccupation au cours de ces dernières années. Tout le monde souhaite écrire des tests et des outils se sont développés pour aider à cela. Victoire ? Pas tout à fait.
Cette présentation fait l'état du test en PHP en présentant les outils d'hier et d'aujourd'hui et se termine par une ouverture sur les nouvelles techniques de tests. Mêlant théorie et pragmatisme, vous serez également sensibilisés à l'écriture de tests pour vos applications PHP.

Online slides: http://slides.williamdurand.fr/sofware-testing-past-present-future/
Sources: https://github.com/willdurand-slides/sofware-testing-past-present-future

William Durand

October 23, 2014
Tweet

More Decks by William Durand

Other Decks in Programming

Transcript

  1. Software Testing is the process of executing a program or

    system with the intent of finding errors. However, "testing shows the presence, not the absence of bugs" (Dijkstra).
  2. Mockery class MockeryTest extends \PHPUnit_Framework_TestCase { public function testGetsAverageTemperatureFromThreeServiceReadings() {

    $service = \Mockery::mock('service'); $service ->shouldReceive('readTemp') ->times(3) ->andReturn(10, 12, 14); $temperature = new Temperature($service); $this->assertEquals(12, $temperature->average()); } } ® padraic/mockery
  3. Prophecy $prophet = new Prophecy\Prophet(); $prophecy = $prophet->prophesize(); $prophecy->willExtend('stdClass'); $dummy

    = $prophecy->reveal(); // fake $prophecy->read(Argument::type('string')); $fake = $prophecy->reveal(); // stub $prophecy->read('123')->willReturn('value'); $stub = $prophecy->reveal(); // mock $prophecy->read()->shouldBeCalled(); // spy $prophecy->read()->shouldHaveBeenCalled(); ® phpspec/prophecy
  4. Example class VfsStreamTest extends \PHPUnit_Framework_TestCase { protected function setUp() {

    $this->cacheDir = vfsStream::setup('cache'); $this->repository = new YamlUserRepository( vfsStream::url('cache/users.yml') ); } public testAddSerializesUserIntoYaml() { $this->repository->add(new User('John', 'Doe')); $this->assertEquals( '- { first_name: "John", last_name: "Doe" }', $this->cacheDir->getChild('users.yml')->getContent() ); } }
  5. TestDox class EmailParserTest extends \PHPUnit_Framework_TestCase { public function testReadsSimpleBody() {

    ... } public function testRecognizesDateStringAboveQuote() { ... } public function testDoesNotModifyInputString() { ... } public function testDealsWithMultilineReplyHeaders() { ... } } y $ phpunit --testdox EmailReplyParser\Tests\Parser\EmailParser [x] Reads simple body [x] Recognizes date string above quote [x] Does not modify input string [x] Deals with multiline reply headers F , Dan North (2006) Introducing BDD
  6. Behat Story BDD since . 2010 Feature: ls In order

    to see the directory structure As a UNIX user I need to be able to list the current directory's contents Scenario: List 2 files in a directory Given I am in a directory "test" And I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should get: """ bar foo """ ® Behat/Behat
  7. phpspec Spec BDD since , yep! 2007 class MarkdownSpec extends

    ObjectBehavior { function it_converts_plain_text_to_html_paragraphs() { $this->toHtml("Hi, there")->shouldReturn("<p>Hi, there</p>"); } } ® phpspec/phpspec
  8. PHP•VCR class PhpVcrTest extends \PHPUnit_Framework_TestCase { /** * @vcr unittest_annotation_test

    */ public function testInterceptsWithAnnotations() { // Requests are intercepted and stored into: // `tests/fixtures/unittest_annotation_test` $content = file_get_contents('http://google.com'); $this->assertEquals('some content', $content); // VCR is automatically turned on and off } } ® php‑vcr/php‑vcr
  9. Mink $driver = new \Behat\Mink\Driver\GoutteDriver(); $session = new \Behat\Mink\Session($driver); $session->start();

    $session->visit('http://williamdurand.fr'); echo "Status code: ". $session->getStatusCode() . "\n"; echo "Current URL: ". $session->getCurrentUrl() . "\n"; $page = $session->getPage(); $anchorEl = $page->find('css', 'h3 a.title'); ® Behat/Mink
  10. Code Generation public interface Login { public void e_Init(); public

    void v_ClientNotRunning(); public void e_StartClient(); public void v_LoginPrompted(); public void e_ValidPremiumCredentials(); public void v_Browse(); } ¿ GraphWalker
  11. Interaction With The SUT public class SimpleTest extends ExecutionContext implements

    Login { @Override public void e_Init() { ... } ... @Test public void runSmokeTest() { ... } @Test public void runFunctionalTest() { ... } } ¿ Java/JUnit, Robotium, Selenium, Appium, etc.
  12. Praspel The missing specification language for PHP. /** * @requires

    t : array([to integer()],boundinteger(5,10)) and * i : integer(); * @ensures \result : boolean(); * @throwable FooException */ public function find($t, $i) { // ... } ® hoaproject/Praspel
  13. Recap' Congrats for understanding the need for testing Testing is

    more than using existing tools Test automation is part of your future, start now!