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

BDD i Behat

BDD i Behat

DaFED#32
Speaker: Saša Stamenković

BDD metodologija se fokusira na ponašanje aplikacije, a uz pomoć Behat-а moguće je izvršenje korisničkih scenarija u vidu funkcionalnih testova. Upoznaćemo se sa osnovnim konceptima ovakvog načina razvoja softvera i naučiti kako da pišemo scenarije i iste pretvorimo u funkcionalne testove, kao i kako da koristimo Behat i Mink u konkretnim PHP projektima.

DaFED

May 06, 2015
Tweet

More Decks by DaFED

Other Decks in Programming

Transcript

  1. Hajde da svi koristimo istu Hajde da svi koristimo istu

    terminologiju terminologiju Menadžeri Programeri Biznis analitičari Vlasnici projekta Testeri ...
  2. ...za planiranje, ...za planiranje, implementaciju i testiranje implementaciju i testiranje

    funkcionalnosti funkcionalnosti Menadžeri Programeri Biznis analitičari Vlasnici projekta Testeri ...
  3. ...sa fokusom na ponašanje ...sa fokusom na ponašanje Menadžeri Programeri

    Biznis analitičari Vlasnici projekta Testeri ...
  4. StoryBDD nam pomaže da razvojni tim StoryBDD nam pomaže da

    razvojni tim razume biznis logiku na istom nivou kao razume biznis logiku na istom nivou kao klijent. klijent. I čak podiže nivo znanja klijenta o I čak podiže nivo znanja klijenta o sopstvenom biznisu sopstvenom biznisu. .
  5. Primoravajući vas da Primoravajući vas da odgovorite na tri odgovorite

    na tri jednostavna pitanja. jednostavna pitanja.
  6. Gherkin Gherkin Struktuirani jezik kojim se Struktuirani jezik kojim se

    opisuje funkcionalnost. opisuje funkcionalnost.
  7. Feature: News admin panel In order to maintain a list

    of news As a site administrator I need to be able to edit news
  8. Feature: News admin panel In order to maintain a list

    of news As a site administrator I need to be able to edit news
  9. Feature: (naziv funkcionalnosti) In order to (upotrebna vrednost) As a

    (osoba koja koristi) I need (opis funkcionalnosti)
  10. Feature: News admin panel In order to maintain a list

    of news As a site administrator I need to be able to edit news Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learn BDD" And I press "Save" Then I should see "A new article was added"
  11. Feature: News admin panel In order to maintain a list

    of news As a site administrator I need to be able to edit news Scenario: Add new article Given I am on the "/admin/news" page When I click "New Article" And I fill in "Title" with "Learn BDD" And I press "Save" Then I should see "A new article was added"
  12. Feature: News admin panel In order to maintain a list

    of news As a site administrator I need to be able to edit news Scenario: (naziv scenarija) Given (podesi stanje sistema) When (akcija osobe/uloge) And (se može dodati za više akcija...) And (...Given/When/Then linije) Then (proveri stanje sistema)
  13. Gherkin Gherkin Konzinstentan način da se Konzinstentan način da se

    opišu funkcionalnosti i opišu funkcionalnosti i scenariji korišćenja istih. scenariji korišćenja istih.
  14. Zamislite da možemo da Zamislite da možemo da izvršavamo Gherkin

    rečenice izvršavamo Gherkin rečenice kao funkcionalne testove. kao funkcionalne testove.
  15. Behat Behat Mapira svaki korak Mapira svaki korak (Gherkin rečenicu)

    u PHP (Gherkin rečenicu) u PHP poziv. poziv.
  16. Konzolna aplikacija Konzolna aplikacija $ ./bin/behat --help Usage: behat [-s|--suite="..."]

    [-f|--format="..."] [-o|--out="..."] [--format-settings="..."] [--init] [ Arguments: paths Optional path(s) to execute. Could be: - a Symfony2 bundle path (@BundleName/) - a dir (features/) - a feature (*.feature) - a scenario at specific line (*.feature:10). - all scenarios at or after a specific line (*.feature:10-*). - all scenarios at a line within a specific range (*.feature:10-20). - a scenarios list file (*.scenarios). Options: --suite (-s) Only execute a specific suite. --format (-f) How to format tests output. pretty is default. Available formats are: - progress: Prints one character per step. - pretty: Prints the feature as is. You can use multiple formats at the same time. (multiple values allowed) --out (-o) Write format output to a file/directory instead of STDOUT (output_path). You can also provide different
  17. Inicijalizacija Inicijalizacija $ php bin/behat --init +d features - place

    your *.feature files here +d features/bootstrap - place your context classes here +f features/bootstrap/FeatureContext.php - place your definitions, transformations and hook
  18. Behatu je potrebno: Behatu je potrebno: *.feature fajlovi za izvšenje.

    FeatureContext.php koji sadrži metode za izvršenje. Opcioni behat.yml konfiguracioni fajl.
  19. Feature: ls In order to see the directory structure As

    a UNIX user I need to be able to list directory's contents features/ls.feature
  20. Scenario Scenario Ako imamo dva fajla u Ako imamo dva

    fajla u direktorijumu, i pokrenemo direktorijumu, i pokrenemo ls komandu - trebalo bi da ls komandu - trebalo bi da ih vidimo u listi. ih vidimo u listi.
  21. Scenario: List 2 files in a directory Given I have

    a file named "foo" And I have a file named "bar" When I run "ls" Then I should see "foo" in the output And I should see "bar" in the output features/ls.feature
  22. $ php bin/behat $ php bin/behat $ php bin/behat Feature:

    ls In order to see the directory structure As a UNIX user I need to be able to list directory's contents Scenario: List 2 files in a directory # features/ls.feature:6 Given I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should see "foo" in the output And I should see "bar" in the output 1 scenario (1 undefined) 5 steps (5 undefined) 0m0.01s (10.10Mb) --- FeatureContext has missing steps. Define them with these snippets: /** * @Given I have a file named :arg1 */
  23. Implementiraj generisane Implementiraj generisane metode metode /** * @Given I

    have a file named :file */ public function iHaveAFileNamed($file) { touch($file); } /** * @When I run :command */ public function iRun($command) { $this->output = shell_exec($command); }
  24. $ php bin/behat $ php bin/behat $ php bin/behat Feature:

    ls In order to see the directory structure As a UNIX user I need to be able to list directory's contents Scenario: List 2 files in a directory # features/ls.feature:6 Given I have a file named "foo" # FeatureContext::iHaveAFileNamed() And I have a file named "bar" # FeatureContext::iHaveAFileNamed() When I run "ls" # FeatureContext::iRun() Then I should see "foo" in the output # FeatureContext::iShouldSeeInTheOutput() And I should see "bar" in the output # FeatureContext::iShouldSeeInTheOutput() 1 scenario (1 passed) 5 steps (5 passed) 0m0.02s (10.22Mb)
  25. Možemo da Možemo da mapiramo svaki mapiramo svaki korak u

    PHP poziv korak u PHP poziv sa Behatom. sa Behatom.
  26. Mink Mink Mink je Mink je open sors open sors

    pregledač pregledač kontroler/emulator za kontroler/emulator za web web aplikacije. aplikacije.
  27. Korišćenje Mink-a Korišćenje Mink-a use Behat\Mink\Session; use Behat\Mink\Driver\GoutteDriver; $session =

    new Session(new GoutteDriver()); $session->visit($startUrl); $session->getPage()->findLink('Downloads')->click(); echo 'Status: '.$session->getPage()->getStatusCode(); echo 'Content: '.$session->getPage()->getContent();
  28. Ažuriranje zavisnosti Ažuriranje zavisnosti { "require": { "behat/behat": "~3.0", "behat/mink-extension":

    "~2.0@dev", "behat/mink-goutte-driver": "~1.0", "behat/mink-selenium2-driver": "~1.1" } }
  29. Mink unutar FeatureContext Mink unutar FeatureContext klase klase namespace Hypebeast\Bundle\WebBundle\Behat;

    use Behat\MinkExtension\Context\RawMinkContext; class FeatureContext extends RawMinkContext { public function doSomething() { $this->getSession()->visit('https://github.com/'); } }
  30. FeatureContext extends MinkContext FeatureContext extends MinkContext namespace Hypebeast\Bundle\WebBundle\Behat; use Behat\MinkExtension\Context\RawMinkContext;

    class FeatureContext extends MinkContext { public function doSomething() { $this->visit('/foo'); $this->selectOption('Country', 'Poland'); $this->pressButton('Save'); $this->assertPageContainsText('Country changed.'); } }
  31. Nakon Nakon dodavanja MinkContext dodavanja MinkContext $ php bin/behat -dl

    default | Given /^(?:|I )am on (?:|the )homepage$/ default | When /^(?:|I )go to (?:|the )homepage$/ default | Given /^(?:|I )am on "(?P<page>[^"]+)"$/ default | When /^(?:|I )go to "(?P<page>[^"]+)"$/ default | When /^(?:|I )reload the page$/ default | When /^(?:|I )move backward one page$/ default | When /^(?:|I )move forward one page$/ default | When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/ default | When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/ default | When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" default | When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/ default | When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$ default | When /^(?:|I )fill in the following:$/ default | When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*) default | When /^(?:|I )additionally select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>( default | When /^(?:|I )check "(?P<option>(?:[^"]|\\")*)"$/ default | When /^(?:|I )uncheck "(?P<option>(?:[^"]|\\")*)"$/ default | When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/ default | Then /^(?:|I )should be on "(?P<page>[^"]+)"$/ default | Then /^(?:|I )should be on (?:|the )homepage$/ default | Then /^the (?i)url(?-i) should match (?P<pattern>"(?:[^"]|\\")*")$/
  32. Što znači Što znači Možemo pisati funkcionalne Možemo pisati funkcionalne

    testove bez potrebe za testove bez potrebe za pisanjem i jedne linije PHP pisanjem i jedne linije PHP koda! koda!
  33. Feature: Search In order to find articles As a website

    visitor I need to be able to search for articles Scenario: Searching for an article that exists Given I am on the homepage When I fill in "search" with "BDD" And I press "go" And I follow "Behavior-driven development" Then I should see "History" And I should see "Principles of BDD" And I should see "Story versus specification"