實作測試環境:
- PHP 7.0.8
- Behat 3.1
- PHPUnit 5.4
建立 Composer 專案
composer init
它會用問答方式, 請依自己的需求填入相關內容
安裝測試的套件: behat 及 phpunit
composer require behat/behat
composer require phpunit/phpunit
初始 Behat 測試
./vendor/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 hooks here
Behat 會自動建立 features/ 資料夾, 並且產生 boostrap/FeatureContext.php
如此已經完成了最基本的測試環境
接下來在 features/ 資料夾新增 *.feature 功能描述檔案
範例: Calculator.feature
Feature: Calculator
In order to sum up numbers
As a user
I should get the result from it
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
執行 Behat 測試
./vendor/bin/behat
執行結果:
Feature: Calculator
In order to sum up numbers
As a user
I should get the result from it
Scenario: Add two numbers # features\Calculator.feature:6
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
1 scenario (1 undefined)
4 steps (4 undefined)
0m0.58s (5.72Mb)
--- FeatureContext has missing steps. Define them with these snippets:
/**
* @Given I have entered :arg1 into the calculator
*/
public function iHaveEnteredIntoTheCalculator($arg1)
{
throw new PendingException();
}
/**
* @When I press add
*/
public function iPressAdd()
{
throw new PendingException();
}
/**
* @Then the result should be :arg1 on the screen
*/
public function theResultShouldBeOnTheScreen($arg1)
{
throw new PendingException();
}
Behat 自動產生出缺少的步驟程式, 把它複製貼上至 FeatureContext.php 檔裡, 即可繼續開發
接下來的步驟皆是執行 ./vendor/bin/behat , 一邊補齊程式, 直到所有的功能全都符合需求為止
當所有的功能都做完, 全部的文字就會顯示綠色~~ 也就是我們測試說的 綠燈!
測試的步驟, 請參照 GitHub 上的 demo 範例, 自己做過一遍就會有感覺!!
沒有留言:
張貼留言