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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度