douxuexiao1748
2014-10-22 10:28
浏览 76
已采纳

Behat的行为测试覆盖率

I would like to approximate test coverage of behaviour using Behat in PHP.

Obviously, unlike code coverage, behavioural coverage is not precisely quantifiable, however, we can know what behaviours we are planning to accommodate for from the start. For example, we might know that a user can 1) buy a product, 2) comment on the product. For this we would want to write two BDD tests.

To begin with, we could create placeholders and use the standard @wip tag to mark each test as work in progress.

I have two related questions:

Is it possible to have Behat report on the total number of scenarios, and the total number of scenarios with a given tag (i.e. @wip)?

Is it possible for Behat to know that count, even if you use the filter "~@wip" to ignore those tests?

图片转代码服务由CSDN问答提供 功能建议

我想在PHP中使用Behat来近似测试行为的覆盖范围。

显然,与代码覆盖不同,行为覆盖不能精确量化,但是,我们可以从一开始就知道我们计划适应哪些行为。 例如,我们可能知道用户可以1)购买产品,2)对产品发表评论。 为此我们想要编写两个BDD测试。

首先,我们可以创建占位符并使用标准的@wip标记将每个测试标记为正在进行的工作。

我有两个相关的 问题:

是否可以通过Behat报告场景总数以及具有给定标签的场景总数(即@wip)? \ n

即使您使用过滤器“〜@ wip”忽略这些测试,Behat是否可以知道该计数?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsrw29618 2014-10-22 13:36
    已采纳

    You can use hooks to get information about scenarios, including their tags, however you can only do this in @beforeFeature or @beforeSuite.

    Unfortunately this means it will not report on scenarios with tags that are being filtered out.

    Here is my initial solution.

    class Context extends BehatContext {
    
        protected static $scenarioCount = 0;
    
        protected static $scenarioCountWip = 0;
    
        /**
         * Count scenarios and WIP
         * @BeforeScenario
         */
        public function countScenario(Behat\Behat\Event\ScenarioEvent $scenario)
        {
            self::$scenarioCount++;
            if(in_array('wip', $scenario->getScenario()->getTags())) {
                self::$scenarioCountWip++;
            }
        }
    
        /**
         * @AfterSuite
         */
        public static function coverageReport()
        {
            echo 'Scenarios:      '.self::$scenarioCount.PHP_EOL;
            echo ' - in progress: '.self::$scenarioCountWip.PHP_EOL;
            echo ' - coverage:    '.(100/self::$scenarioCount)*(self::$scenarioCount-self::$scenarioCountWip).PHP_EOL;
            echo PHP_EOL;
        }
    
    }
    

    The feature file:

    Feature: Test Count
    
      @wip
      Scenario: Work in progress
    
      @someTag
      Scenario: Scenarion with tag
    
      Scenario: Final scenario
    

    The result when running all tests:

    Feature: Test Count
    
      @wip
      Scenario: Work in progress
    
      @someTag
      Scenario: Scenarion with tag
    
      Scenario: Final scenario
    
    Scenarios:      3
     - in progress: 1
     - coverage:    66.666666666667
    
    3 scenarios (3 passed)
    No steps
    

    This the result whit @wip filtered out:

    Feature: Test Count
    
      @someTag
      Scenario: Scenarion with tag
    
      Scenario: Final scenario
    
    Scenarios:      2
     - in progress: 0
     - coverage:    100
    
    2 scenarios (2 passed)
    No steps
    

    I'll leave the question open in the hope someone finds a better solution.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题