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
//features/bootstrap/FeatureContext.php use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
class FeatureContext implements SnippetAcceptingContext{
/** * Initializes context. */ public function __construct() { }
}
// features/bootstrap/FeatureContext.php
<?php
use Behat\Behat\Context\Context;
use Domain\Price;use Domain\PriceComparator;
use Infrastructure\NBPPriceConverter;
/*** 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 运行所有的测试。你应该得到以下结果:
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
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 允许你为你的测试定义多个上下文。这意味着你可以将步骤代码分割成多个类,并从不同的角度去测试你的场景。
你可以例如:为 web 上下文编写代码,它将使用你的应用程序 HTTP 控制器运行你的测试步骤。你还可以创建“domain”上下文,它将只使用 PHP API 调用来运行你的业务逻辑。通过这种方式,你可以单独地测试业务逻辑集成,从端到端应用程序测试。
关于如何在 Behat 建立许多上下文的更多信息,请参考http://behat.org/en/latest/userguide/context.html的文档。