淺談如何提高PHP代碼質(zhì)量之端到端集成測(cè)試
在這里,我們可以使用為行為驅(qū)動(dòng)開(kāi)發(fā)構(gòu)建的工具——官方 PHP 的 Cucumber 實(shí)現(xiàn)——Behat。我們可以通過(guò)運(yùn)行以下代碼來(lái)安裝它:
$ php composer.phar require --dev behat/behat
增加一個(gè)目標(biāo)到 build.xml(在本文的第一部分中描述了 Phing 設(shè)置)
<target name='behat'><exec executable='bin/behat' passthru='true' checkreturn='true' /></target>…<target name='run' depends='phpcs,phpcpd,phan,phpspec,behat' />
然后,你應(yīng)該為文件 features/price.feature 的測(cè)試創(chuàng)建一個(gè)規(guī)范。
Feature: Price Comparison In order to compare prices As a customer I need to break the currency barrier Scenario: Compare EUR and PLN Given I use nbp.pl comparator When I compare “100EUR” and “100PLN” Then It should return some result
這個(gè)測(cè)試場(chǎng)景非常容易閱讀,并且應(yīng)該給你一個(gè)關(guān)于該特性應(yīng)該如何工作的良好印象。不幸的是,計(jì)算機(jī)通常并不真正理解人類語(yǔ)言,所以現(xiàn)在是為每一步編寫代碼的時(shí)候了。
你可以通過(guò)運(yùn)行 ./bin/behat-init 來(lái)生成它的代碼模板。它應(yīng)該會(huì)創(chuàng)建一個(gè)這樣的類:
//features/bootstrap/FeatureContext.php use BehatBehatContextSnippetAcceptingContext;use BehatGherkinNodePyStringNode;use BehatGherkinNodeTableNode;class FeatureContext implements SnippetAcceptingContext{/** * Initializes context. */ public function __construct() { }}
然后你可以執(zhí)行:
$ bin/behat --dry-run --append-snippets
Behat 將自動(dòng)為場(chǎng)景中定義的每個(gè)步驟創(chuàng)建函數(shù)。現(xiàn)在你可以通過(guò)填充函數(shù)的主體來(lái)開(kāi)始實(shí)現(xiàn)真正的檢查:
// features/bootstrap/FeatureContext.php<?phpuse BehatBehatContextContext;use DomainPrice;use DomainPriceComparator;use InfrastructureNBPPriceConverter;/*** Defines application features from the specific context.*/class FeatureContext implements Context{/** @var PriceComparator */ private $priceComparator;/** @var int */ private $result; /** * Initializes context. * * Every scenario gets its own context instance.* You can also pass arbitrary arguments to the* context constructor through behat.yml. */public function __construct() { } /** * @Given I use nbp.pl comparator */ public function iUseNbpPlComparator() {$this->priceComparator = new PriceComparator(new NBPPriceConverter());} /** * @When I compare :price1 and :price2 */public function iCompareAnd($price1, $price2) {preg_match(’/(d+)([A-Z]+)/’, $price1, $match1); preg_match(’/(d+)([A-Z]+)/’, $price2, $match2);$price1 = new Price($match1[1], $match1[2]);$price2 = new Price($match2[1], $match2[2]);$this->result = $this->priceComparator->compare($price1, $price2); } /** * @Then It should return some result */ public function itShouldReturnSomeResult() {if (!is_int($this->result)) {throw new DomainException(’Returned value is not integer’);} }}
最后,使用 ./bin/phing 運(yùn)行所有的測(cè)試。你應(yīng)該得到以下結(jié)果:
Buildfile: /home/maciej/workspace/php-testing/build.xmlMyProject > phpcs: MyProject > phpcpd: phpcpd 4.0.0 by Sebastian Bergmann.0.00% duplicated lines out of 103 total lines of code. Time: 17 ms, Memory: 4.00MB MyProject > phan: MyProject > phpspec: / skipped: 0% / pending: 0% / passed: 100% / failed: 0% / broken: 0% / 3 examples2 specs3 examples (3 passed)15ms MyProject > behat: Feature: Price Comparison In order to compare prices As a customer I need to break the currency barrier Scenario: Compare EUR and PLN # features/price.feature:6 Given I use nbp.pl comparator# FeatureContext::iUseNbpPlComparator() When I compare '100EUR' and '100PLN' # FeatureContext::iCompareAnd() Then It should return some result # FeatureContext::itShouldReturnSomeResult()1 scenario (1 passed)3 steps (3 passed)0m0.01s (9.13Mb) MyProject > run: BUILD FINISHED Total time: 1.1000 second
正如你所看到的,Behat 準(zhǔn)備了一份很好的報(bào)告,說(shuō)明我們的應(yīng)用程序做了什么,結(jié)果是什么。下一次,當(dāng)項(xiàng)目經(jīng)理詢問(wèn)你在測(cè)試中涉及到哪些場(chǎng)景時(shí),你可以給他一個(gè) Behat 輸出!
1、測(cè)試的結(jié)構(gòu)每個(gè)測(cè)試都包括:
對(duì)該場(chǎng)景的一些準(zhǔn)備,用“Given”部分表示 “When”部分所涵蓋的一些動(dòng)作 一些檢查被標(biāo)記為“Then”部分每個(gè)部分都可以包含多個(gè)與“And”關(guān)鍵字連接的步驟:
Scenario: Compare EUR and PLN Given nbp.pl comparator is available And I use nbp.pl comparator When I compare '100EUR' and '100PLN' And I save the result Then It should return some result And the first amount should be greater
2、上下文Behat 允許你為你的測(cè)試定義多個(gè)上下文。這意味著你可以將步驟代碼分割成多個(gè)類,并從不同的角度去測(cè)試你的場(chǎng)景。
你可以例如:為 web 上下文編寫代碼,它將使用你的應(yīng)用程序 HTTP 控制器運(yùn)行你的測(cè)試步驟。你還可以創(chuàng)建“domain”上下文,它將只使用 PHP API 調(diào)用來(lái)運(yùn)行你的業(yè)務(wù)邏輯。通過(guò)這種方式,你可以單獨(dú)地測(cè)試業(yè)務(wù)邏輯集成,從端到端應(yīng)用程序測(cè)試。
關(guān)于如何在 Behat 建立許多上下文的更多信息,請(qǐng)參考http://behat.org/en/latest/userguide/context.html的文檔。
3、如何使用Behat正如一開(kāi)始所提到的,你可以使用 Behat 進(jìn)行集成測(cè)試。通常情況下,你的代碼依賴于一些外部的第三方系統(tǒng)。當(dāng)我們?cè)诘?2 部分中編寫單元測(cè)試時(shí),我們總是假設(shè)外部依賴關(guān)系像預(yù)期的那樣工作。使用 Behat,你可以編寫測(cè)試場(chǎng)景,它將自動(dòng)運(yùn)行你的代碼,并檢查它是否正確地使用真實(shí)場(chǎng)景的服務(wù)。
最重要的是,Behat 對(duì)于測(cè)試系統(tǒng)使用的復(fù)雜的端到端場(chǎng)景非常有用。它允許你隱藏在一個(gè)可讀性的名字后面運(yùn)行測(cè)試步驟所需的復(fù)雜代碼,并編寫一個(gè)人人都能理解的場(chǎng)景。
總結(jié)從以上的文章中,你已經(jīng)學(xué)習(xí)了如何在你的項(xiàng)目中設(shè)置六個(gè)有用的工具:
PHing 用于運(yùn)行你的構(gòu)建 PHPCS 用于自動(dòng)檢查代碼格式 PHPCPD 用于檢測(cè)重復(fù)代碼的 Phan 用于高級(jí)靜態(tài)代碼分析 PHPSpec 用于單元測(cè)試 Behat 用于端到端和集成測(cè)試現(xiàn)在,你可以向 git 提交鉤子添加 ./bin/phing,并設(shè)置持續(xù)集成來(lái)運(yùn)行每個(gè)提交的測(cè)試。
是不是突然之間,沒(méi)有什么能阻止你寫出高質(zhì)量的 PHP 代碼!
以上就是淺談如何提高PHP代碼質(zhì)量之端到端集成測(cè)試的詳細(xì)內(nèi)容,更多關(guān)于如何提高PHP代碼質(zhì)量之端到端集成測(cè)試的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序2. XML入門精解之結(jié)構(gòu)與語(yǔ)法3. XML解析錯(cuò)誤:未組織好 的解決辦法4. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)5. 利用CSS3新特性創(chuàng)建透明邊框三角6. XML入門的常見(jiàn)問(wèn)題(一)7. HTML5 Canvas繪制圖形從入門到精通8. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera9. HTML DOM setInterval和clearInterval方法案例詳解10. XML入門的常見(jiàn)問(wèn)題(二)
