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 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 Slide

  3. Past
    /pɑːst/

    View Slide

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

    View Slide

  5. Tools, tools, tools

    View Slide

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

    View Slide

  7. Extend It!
    ® etsy/phpunit‑extensions

    View 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 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 Slide

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

    View Slide

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

    View 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 Slide

  13. Tests As Documentation

    View 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 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 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 Slide

  17. Hooray!?
    Not really.

    View Slide

  18. Present
    /ˈpreznt/

    View Slide

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

    View Slide

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

    View Slide

  21. Continuous Integration

    View Slide

  22. Record & Replay

    View 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 Slide

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

    View Slide

  25. It is not only about
    testing your code.

    View Slide

  26. But rather, testing
    everything.

    View Slide

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

    View Slide

  28. Web Acceptance Testing

    View 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 Slide

  30. ® jlipps/sausage

    View Slide

  31. Fuzzy Testing

    View Slide

  32. gremlins.js
    ® marmelab/gremlins.js

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  37. Model‑based Testing

    View Slide

  38. View Slide

  39. 1 Model w N Implementations

    View Slide

  40. Model
    ¿ yEd Graph Editor

    View Slide

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

  42. 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 Slide

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

    View Slide

  44. Results

    View Slide

  45. Automated Model
    Generation
    Story of my life.

    View Slide

  46. Contract‑based Testing

    View Slide

  47. 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 Slide

  48. 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 Slide

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

    View Slide