設置Behat:
首先,您需要安裝Behat。最簡單的方法是使用作曲家:
<code class="“" bash>作曲家需要behat/behat </code>
這安裝了Core Behat庫。接下來,您需要在項目的根目錄中創建 behat.yml
配置文件。此文件指定您的功能文件和上下文的位置。一個基本示例:
默認值:套房:默認值:路徑:功能/上下文:功能/bootstrap/bootstrap/
此配置告訴您在“ code
code”
特徵文件用小黃瓜(一種簡單,可讀的語言)編寫。他們從用戶的角度描述了系統的行為。一個簡單的示例:
<code class="“" gherkin>功能:用戶登錄方案:成功登錄給定的我在登錄頁面上,當我輸入“ testuser”時作為用戶名和“密碼”作為密碼,我按“登錄”然後,我應該在主頁上</code>
創建上下文文件:
上下文文件包含定義您功能文件中的步驟的代碼。這些步驟使用PHP與您的應用程序進行交互並驗證預期的行為。對於上面的示例,您需要創建一個上下文文件(例如 farmaurecontext.php
),並定義步驟:
<pre class="brush:php;toolbar:false"> <code class="“" php>&lt;?php php php behat behat behat \ behat \ behat \ contect \ contect \ contect \ contect \ contect \ contect \ contect \ contect \ contect \ contect;使用behat \ gherkin \ node \ pystringNode;使用Behat \ behat \ tester \ exception \ pendingException; class tausurecontext實現上下文{ / ** * @given我在登錄頁面 * / public函數iamontheLogInpage(){//代碼到登錄頁面} / ** * @ @ @當我enter:username as as username as as username as as username and:as as as as as as procastion * / procastion * / procast步驟定義...} </code>
運行:
一旦您定義了功能文件和上下文文件,就可以從命令行中運行:
<pre class="brush:php;toolbar:false"> <pre class="brush:php;toolbar:false"> <code class="“" bash> vendor/bash> vendor/bin/bin/bin/bin/bin </code>
在PHP項目中的BDD?
Behat can be integrated with various PHP tools and frameworks:
集成通常涉及通過合作儀安裝其他範圍擴展,並在您的 behat.yml
文件中配置它們。
在這些情況下,Behat有助於定義清晰的接受標準,自動化測試,並確保應用程序滿足業務需求。從用戶的角度來看,重點仍然放在系統的行為上,使得更容易在團隊之間進行驗證和驗證功能。
以上是如何在PHP中使用Behat進行行為驅動的發展(BDD)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!