Davranis Odakli Gelistirmeye Giris, Behat ve Mink

Preview:

DESCRIPTION

Davranis odakli gelistirmeye giris ve BDD testlerinin Behat ve Mink kullanilarak gelistirilmesi. BDD testlerinin cevik yazilim gelistirme metodolojilerindeki yeri.

Citation preview

Behat ve mink İle Davraniş odaklı

Gelİştİrme

Mustafa İLERİMayıs'14 PHP - IST

Mustafa İlerİ @mustafailerihttp://tr.linkedin.com/in/mustafailerimi@mustafaileri.comwww.mustafaileri.comhttps://github.com/mustafailerihttp://www.slideshare.net/mustafaileri

Behavior Driven

Development

Davranıs odaklı gelistirme, test odaklı gelistirmeye dayanarak ortaya çıkmıs bir yaılım gelistime sürecidir.

Dan North tarafından ortaya atıldı.

Amaç : Senaryoların önceden belirlenerek bunlara göre gelistirme yapmak.

NASIL ORTAYA ÇIKTI ?

TDD gibi çevik yöntemler kullanan Dan North, hep aynı sorunlar ve yanlıs anlamalarla karsılasıyor.

Gelistiriciler tarafında problem olan ve cevap bekleyen sorular:

1. Nereden başlamalıyım?2. Neyi test etmeli veya neyi test etmemeliyim?3. Testleri nasıl adlandırmalıyım?...

public class CustomerLookupTest extends TestCase { testFindsCustomerById() { ... } testFailsForDuplicateCustomers() { ... } ...}

CustomerLookup- finds customer by id- fails for duplicate customers- ...

TDD Testİ BDD Testİ

Kurgulanan İlk BDD TESTİ

Herşey Chris Stevenson'ın yadığı, yanıltıcı derecede basit jUnit testini daha anlasılır daha kısa hale getirmek ile basladı.

Test Odaklı Gelİştİrme

Birim Testler:Her bir birim için yapması gereken isi doğrulayan testler olmalı.

Entegrasyon Testleri:Parçalar birbirleri ile düzgün çalısabiliyor mu?...Genelde gelistirici tarafından gelistirme asamasındakoda yönelik testler yaılır.

BDD VS TDD

BDD VS TDDDavranış Odaklı GELİŞTİRME

Fonksiyonel Testler:Uyulama üzerinde yer alan bilesenler doğru isi yapıyor mu?

Kullanıcı KabulTestleri:Uygulama ve bilesenler belirlenen kriterlere göre uygun ve düzgün çalısıyor mu?...Genelde is sahibi ve analistler tarafından anali sırasında davranısa yönelik testler yaılır.

BDD VS TDD

BDD testlerİ yazılıM ürünlerİnİzİn son kullanıcı ve kullanıcı kabul

testlerİ İçİn HARCANAN zamanı önemlİ ölçüde azaltır.

BDD testlerİ yazılıM ürünlerİnİzİn son kullanıcı ve kullanıcı kabul

testlerİ İçİn HARCANAN zamanı önemlİ ölçüde azaltır.

BDD TESTLERİ Yenİ ÖzellİKlerİN analİZlerİNİN ve Yazılımın

sorumluluklarının daha net BİR şekİLDE GELİŞTİRİCİYE BİldİrİlmesİNİ SAĞLAR

BDD TESTLERİ Yenİ ÖzellİKlerİN analİZlerİNİN ve Yazılımın

sorumluluklarının daha net BİR şekİLDE GELİŞTİRİCİYE BİldİrİlmesİNİ SAĞLAR.

ÇALIŞAN UYGULAMALAR GELİŞTİRİRSİNİZ VE GELİŞTİRDİĞİNİZ UYGULAMALARIN SÜRDÜRÜLEBİLİRLİĞİNDENEMİN OLURSUNUZ.

ÇALIŞAN UYGULAMALAR GELİŞTİRİRSİNİZ VE GELİŞTİRDİĞİNİZ UYGULAMALARIN SÜRDÜRÜLEBİLİRLİĞİNDENEMİN OLURSUNUZ.

DÖKÜMANTASYONGELİŞTİRİLEN UYGULAMANIN DÖKÜMANTASYONUNUN SÜREKLİ GÜNCEL TUTULMASINI SAĞLAR.Testler normal İnsanların okuyabileceğİ şekİldedİr.

BDD TERMİNOLOJİLERİ

Story: Olusan veya olusması muhtemel durumu bildirir.Scenario: Olusan veya olusması muhtemel durumlar için senaryoları bildirir.Role: Verilen senaryoyu hangi rolle gerçeklestireceğimii belirtir.

ScrumHızlı Bakış

Yaılım Mühendisliği'nde bir uygulama gelistirme yöntemidir. Bu gelistirme yönteminin temel özelliği gözlemci, gelistirmeci ve tekrara dayalı olmasıdır.

Scrum Ken Schwaber ve Jef Sutherland tarafından 1990'ların başında gelistirildi ve temel fkirleri Schwaber (Schwaber 2004) tarafından ortaya konmustur.

SCRUM' A HIZLI BAKIŞ

Scrum MasterÜrün SahibiTakım* Kullanıcı* Yönetici

ROLLER toplantilarSprint Planlama ToplantılarıGünlük ScrumSprint DeğerlendirmesiRetropektif

Hikayeler belirlenir, davranıssal testler ile yaılım ekibine iletilir.

Davranıssal testler çalıstırılarak,Yayına alınmaya haır hikayeler belirlenir

As a <role>,I want <something>So that <benefit>

ProductOwner

DevelopmentTeam

ProductBacklog

SprintBacklog

ProductH

ikay

eleri

ve d

avra

nıss

al te

stler

i olu

sturu

r

Takım tarafından belirleniyor.

Gelis

tirm

eyi y

apar

Testleri çalıstırarak ürünün uygulnluğunu test eder

1

2

3

4

GHERKINGHERKINHerkes tarafından anlasılabilen ve yaılabilen test ayırıstırıcısı.

Feature: Some terse yet descriptive text of what is desiredTextual description of the business value of this featureBusiness rules that govern the scope of the featureAny additional information that will make the feature easier to understand

Scenario: Some determinable business situationGiven some preconditionAnd some other preconditionWhen some action by the actorAnd some other actionAnd yet another actionThen some testable outcome is achievedAnd something else we can check happens too

Scenario: A different situation...

Cucumber(Ruby)

jbehave(java)

nbehave(c#)

specflow(c#)

freshen(python)

lettuce(python)

BEHAT(PHP)

İs gelistirmenin beklentilerini test etmeye yarayan bir PHP çatısıdır.Konstantin Kudryashov tarafından gelistirildi.En güncel sürümü 3.0

BEHAT Nedİr?

{ "require": { "behat/behat": "2.4.*@stable" },

"config": { "bin-dir": "bin/" }}

KURULUM kullanımbin/behat –initvim features/your_first.feature-------------------------------------------

Feature: Your first feature In order to start using Behat As a manager or developer I need to try

Scenario: Successfully describing scenario Given there is something When I do something Then I should see something-------------------------------------------bin/behat

Test Projesi Olusturmak

Behat'ı yükledikten sonra test diini olusturulup test projesi için gerekli kod alt yapısı olusturulur.

cd ls_projectapp/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 hooks here

Özellik Tanımlama(Feature)

Behat'ta istenilen özellik önce tanımlanır ve belirtilir sonrasında bu tanımlamaya göre implemente edilir.

# features/ls.featureFeature: ls In order to see the directory structure As a UNIX user I need to be able to list the current directory's contents

Senaryo Tanımlama(Scenario)

Bir sonraki adım, belirtilen özelliklerin senaryoları belirlenir.

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

Step Defnitions

Diyelim ki

Eğer kiO zaman

Adım Tanımları(Step Defnitons)

Senaryoda bulunan beklentileri anlatan anlamli cümleler ve bunların yapacağı isin tanımı.

1 scenario (1 undefined)5 steps (5 undefined)0m0.02s (9.55Mb)--- FeatureContext has missing steps. Define them with these snippets: /** * @Given I am in a directory :arg1 */ public function iAmInADirectory($arg1) { throw new PendingException(); }

/** * @Given I have a file named :arg1 */ public function iHaveAFileNamed($arg1) { throw new PendingException(); }

/** * @When I run :arg1 */ public function iRun($arg1) { throw new PendingException(); }

/** * @Then I should get: */ public function iShouldGet(PyStringNode $string) { throw new PendingException(); }

<?php

use Behat\Behat\Context\BehatContext;use Behat\Gherkin\Node\PyStringNode;

class FeatureContext extends BehatContext{ private $output;

/** @Given /^I am in a directory "([^"]*)"$/ */ public function iAmInADirectory($dir) { if (!file_exists($dir)) { mkdir($dir); } chdir($dir); }

/** @Given /^I have a file named "([^"]*)"$/ */ public function iHaveAFileNamed($file) { touch($file); }

/** @When /^I run "([^"]*)"$/ */ public function iRun($command) { exec($command, $output); $this->output = trim(implode("\n", $output)); }

/** @Then /^I should get:$/ */ public function iShouldGet(PyStringNode $string) { if ((string) $string !== $this->output) { throw new Exception( "Actual output is:\n" . $this->output ); } }}

MINKMINKPHP 5.3+ üzerinde çalısan web

uygulamalrı için kullanıcı kabul test çatısıdır.

Kurulum ve Hizli Konfİgürasyon{ "require": { "behat/behat": "2.4.*@stable", "behat/mink": "1.4.*@stable", "behat/mink-extension": "*", "behat/mink-goutte-driver": "*", "behat/mink-selenium2-driver": "*" }, "minimum-stability": "dev", "config": { "bin-dir": "bin/" }}

use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext{

......

Mink Hazır Adım tanımlamalarıGiven /^(?:|I )am on (?:|the )homepage$/ When /^(?:|I )go to (?:|the )homepage$/Given /^(?:|I )am on "(?P<page>[^"]+)"$/ When /^(?:|I )go to "(?P<page>[^"]+)"$/ When /^(?:|I )reload the page$/ When /^(?:|I )move backward one page$/ When /^(?:|I )move forward one page$/ When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/ When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/ When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/ When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ When /^(?:|I )fill in the following:$/ When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/ When /^(?:|I )additionally select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/ When /^(?:|I )check "(?P<option>(?:[^"]|\\")*)"$/ When /^(?:|I )uncheck "(?P<option>(?:[^"]|\\")*)"$/ When /^(?:|I )attach the file "(?P[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/ Then /^(?:|I )should be on "(?P<page>[^"]+)"$/ Then /^(?:|I )should be on (?:|the )homepage$/ Then /^the (?i)url(?-i) should match (?P<pattern>"([^"]|\\")*")$/ Then /^the response status code should be (?P<code>\d+)$/ Then /^the response status code should not be (?P<code>\d+)$/ Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/ Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)"$/ Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|\\")*")$/

Mink Hazır Adım tanımlamaları Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|\\")*")$/ Then /^the response should contain "(?P<text>(?:[^"]|\\")*)"$/ Then /^the response should not contain "(?P<text>(?:[^"]|\\")*)"$/ Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/ Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|\\")*)"$/ Then /^the "(?P<element>[^"]*)" element should not contain "(?P<value>(?:[^"]|\\")*)"$/ Then /^(?:|I )should see an? "(?P<element>[^"]*)" element$/ Then /^(?:|I )should not see an? "(?P<element>[^"]*)" element$/ Then /^the "(?P<field>(?:[^"]|\\")*)" field should contain "(?P<value>(?:[^"]|\\")*)"$/ Then /^the "(?P<field>(?:[^"]|\\")*)" field should not contain "(?P<value>(?:[^"]|\\")*)"$/ Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should be checked$/ Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" (?:is|should be) checked$/ Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should not be checked$/ Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" should (?:be unchecked|not be checked)$/ Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" is (?:unchecked|not checked)$/ Then /^(?:|I )should see (?P<num>\d+) "(?P<element>[^"]*)" elements?$/ Then /^print current URL$/ Then /^print last response$/ Then /^show last response$/

Örnek UygulamaFeature: php-ist 2014 etkinlik bilgilerini kontrol eder Bu test etkinligin tarih yer ve konusmaci bilgilerini kontrol eder.

Background: Given I am on homepage

Scenario: Etkinligin "3rd May 2014" tarihinde olup olmadigini kontrol edelim. Then I should see "3rd May 2014"

Scenario: Konusmacilari test edelim. Then I follow "Speakers" Then I should see "Mehmet Karcı" Then I should see "Osman Üngür" Then I should see "Hugo Hamon" Then I should see "Mustafa İleri" Then I should see "Lemi Orhan Ergin" Then I should see "Ramazan Korkmaz" Then I should see "Hugo Hamon" Then I should see "Alvaro Videla" Then I should see "Suat İmamoğlu" Then I should not see "Fatih Gürsoy"

@javascript Scenario: Google sayfa testi Then I search on google with "php ist" Then I should see "php-ist PHP Conference 2014"

@javascript Scenario: Mekan kisminda google maps haritasini kontrol edelim Then check maps on venue

Örnek Uygulama<?phpuse Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext{ /** * @Then /^I search on google with "([^"]*)"$/ */ public function iSearchOnGoogleWith($arg1) { $this->getSession()->visit('http://www.google.com.tr'); $page = $this->getSession()->getPage(); $page->find('css', 'input[type=text]')->setValue($arg1); $page->find('css', 'button')->click(); $this->getSession()->wait(500); }

/** * @Then /^check maps on venue$/ */ public function checkMapsOnVenue() { $page = $this->getSession()->getPage(); $page->findLink('Venue')->click(); $this->assertElementOnPage(

'html body div div#schedule.section section#venue.section div.inner div.content div#map_canvas div.gm-style');

}

}

Örnek Uygulamalar

https://github.com/phpistorg/behat-testleri

https://github.com/phpistorg/behat-mink-testleri

Jıra EklentİSİJira eklentisi, BDD testlerinin uygun formatta yaılarak bu testlerin gelistiriciye is olarak gelmesini sağlar.

Eğer testler basarılı olursa is cozulerek kapatılır.Aksi durumda basarısız olma nedeni isin içine yaılır.

http://etensions.behat.org/jira-etension/

Kaynakça

http://dannorth.net/introducing-bdd/http://en.wikipedia.org/wiki/Behavior-driven_developmenthttp://behat.org/http://www.slideshare.net/mhelmich/t3con13?qid=bf6b3e7d-560d-4a03-8f60-20683c7cc2cb&v=qf1&b=&from_search=3http://www.slideshare.net/davidcontavalli1/zf2-bdd?qid=bf6b3e7d-560d-4a03-8f60-20683c7cc2cb&v=qf1&b=&from_search=1http://www.slideshare.net/nyccamp/behat-by-seth-cohnhttp://www.slideshare.net/headrevision/web-acceptance-testing-with-behat?qid=6021a166-eac0-435d-bf66-ee08d4c7bdef&v=qf1&b=&from_search=7http://selimober.com/blog/2013/07/19/bdd-ye-giris/http://www.mountaingoatsofware.com/blog/the-forgotten-layer-of-the-test-automation-pyramidhttp://watirmelon.com/2011/06/10/yet-another-sofware-testing-pyramid/http://martinfowler.com/bliki/TestPyramid.htmlhttp://mink.behat.org/http://www.mustafaileri.com/pot/60579550214/behat-ve-mink-ile-bdd-testleri-eski-ya

Recommended