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:
    Past, Present, Future
    William Durand ‑ October 23rd, 2014

    View full-size slide

  2. 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).

    View full-size slide

  3. Past
    /pɑːst/

    View full-size slide

  4. Testing was...
    Expensive ($$$)
    Time consuming
    Complicated

    View full-size slide

  5. Tools, tools, tools

    View full-size slide

  6. PHPUnit
    by Sebastian Bergmann ©
    ® sebastianbergmann/phpunit
    (Alternative: )
    atoum/atoum

    View full-size slide

  7. Extend It!
    ® etsy/phpunit‑extensions

    View full-size slide

  8. 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

    View full-size slide

  9. 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

    View full-size slide

  10. More?
    Wanna use PHPUnit with Prophecy and Atoum asserters?
    ® hautelook/frankenstein

    View full-size slide

  11. vfsStream
    Mocking file system since 2007.
    ® mikey179/vfsStream

    View full-size slide

  12. 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()
    );
    }
    }

    View full-size slide

  13. Tests As Documentation

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide

  16. phpspec
    Spec BDD since , yep!
    2007
    class MarkdownSpec extends ObjectBehavior
    {
    function it_converts_plain_text_to_html_paragraphs()
    {
    $this->toHtml("Hi, there")->shouldReturn("Hi, there");
    }
    }
    ® phpspec/phpspec

    View full-size slide

  17. Hooray!?
    Not really.

    View full-size slide

  18. Present
    /ˈpreznt/

    View full-size slide

  19. Testing is...
    Slow
    Manual
    But, trending (hype?)

    View full-size slide

  20. Testing, In Parallel
    ® (Ruby tool!)
    ®
    ®
    grosser/parallel
    brianium/paratest
    liuggio/fastest

    View full-size slide

  21. Continuous Integration

    View full-size slide

  22. Record & Replay

    View full-size slide

  23. 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

    View full-size slide

  24. Gor
    HTTP traffic replication tool, written in Go.
    ® buger/gor

    View full-size slide

  25. It is not only about
    testing your code.

    View full-size slide

  26. But rather, testing
    everything.

    View full-size slide

  27. Security Testing
    , automatic SQL injection tool
    , security scanner
    sqlmap
    Zapr
    SensioLabs Security Checker

    View full-size slide

  28. Web Acceptance Testing

    View full-size slide

  29. 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

    View full-size slide

  30. ® jlipps/sausage

    View full-size slide

  31. Fuzzy Testing

    View full-size slide

  32. gremlins.js
    ® marmelab/gremlins.js

    View full-size slide

  33. But also
    Infrastructure Testing
    Smoke Testing ( )
    Robustness Testing
    Actually,  Testing
    smoke.sh

    View full-size slide

  34. Future
    /ˈfjuːtʃəʳ/

    View full-size slide

  35. Testing will be...
    Automated!
    More formal

    View full-size slide

  36. Disclaimer
    The following techniques are not really "new"
    from a research point of view.

    View full-size slide

  37. Model‑based Testing

    View full-size slide

  38. 1 Model w N Implementations

    View full-size slide

  39. Model
    ¿ yEd Graph Editor

    View full-size slide

  40. 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

    View full-size slide

  41. 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.

    View full-size slide

  42. Sikuli
    y
    F http://www.sikuli.org/

    View full-size slide

  43. Automated Model
    Generation
    Story of my life.

    View full-size slide

  44. Contract‑based Testing

    View full-size slide

  45. 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

    View full-size slide

  46. Recap'
    Congrats for understanding the need for testing
    Testing is more than using existing tools
    Test automation is part of your future, start now!

    View full-size slide

  47. Thank You.
    Questions?
    è
    ¾
    ®
    ¬
    joind.in/11953
    williamdurand.fr
    github.com/willdurand
    twitter.com/couac

    View full-size slide