smart-gamma/pact-behat-extension

适用于 pact.io 的 Smart Gamma Behat 扩展

v1.0.0 2019-06-27 13:16 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Mutation Status

该扩展允许使用 pact.io 合同测试框架以 BDD 风格与 Behat 一起使用

安装

composer require --dev smart-gamma/pact-behat-extension

配置

添加到 behat.yml / behat.yml.dist

default:
    suites:
        default:
            contexts:
                - SmartGamma\Behat\PactExtension\Context\PactContext
            
extensions:
    SmartGamma\Behat\PactExtension\Extension:
        common:
            PACT_CONSUMER_NAME: consumer_name
            PACT_CORS: false
            PACT_BROKER_URI: https://pact.youdomain.com
            PACT_OUTPUT_DIR: var/pact/
            PACT_BROKER_HTTP_AUTH_USER: user
            PACT_BROKER_HTTP_AUTH_PASS: pass
            PACT_MOCK_SERVER_HEALTH_CHECK_TIMEOUT: 10
        providers:
            - provider1_name: localhost:9090
            # in case of you have to communicate with more providers from the consumer 
            #- provider2_name localhost:8889

如果您的 pact 代理没有通过 HTTP 身份验证保护,请删除 PACT_BROKER_HTTP_AUTH_USER 和 PACT_BROKER_HTTP_AUTH_PASS

消费者版本

您应该将消费者版本定义为

common:
    PACT_CONSUMER_VERSION: 1.0.0

但是,如果您使用 Symfony 框架,您可以跳过此步骤,并在 Kernel const 中定义版本

App\Kernel::PACT_CONSUMER_VERSION

使用方法

  • 带有 @pact 标签的 March 场景

      @pact
      Scenario: My cool Pact contract test scenario
    
  • 启动 Pact 模拟服务器

      Background:
      Given "provider1 name" API is available
    
  • 定义 Pact 交互

      Given "provider1 name" request 'GET' to '/api/some/1' should return response with 200 and body:
        | parameter    | value                     |
        | id           | 5af55347c9764a6a01684228  |
        | field1       | 35373808218O868           |
        | blocked      | false                     |
        | createdAt    | 2018-05-11T11:00:00+00:00 |
    
  • 定义用于复杂请求(POST body)的 Pact 交互

     Given "provider1 name" request 'POST' to '/api/device/5af55347c9764a6a01684228/first-frame' with parameters:
       | parameter    | value                     |
       | id           | 5af55347c9764a6a01684228  |
       | imei         | 35373808218O868           |
     And request above to "provider1 name" should return response with 200 and body:
       | parameter    | value                     | 
       | typeName     | SOME                      | 
       | activated    | true                      | 
       | blocked      | false                     | 
    
  • 定义嵌套结构

      Given "<device>" object should have follow structure:
        | parameter | value                    |
        | id        | 5af55347c9764a6a01684228 |
        | imei      | 35373808218O868          |
        | iccid     | 89883O3000000277040      |
      Given "device registry" request 'GET' to '/api/devices' should return response with 200 and body:
        | parameter | value    | match    |
        | count     | 2        | integer  |
        | devices   | <device> | eachLike |
    
  • 执行您的消费者场景步骤

      When I send a 'GET' request to '/api/entry' with parameters:
        | key      | value           |
        | imei     | 35373808218O868 |
        | aux_data | demo[]          |
      Then the response status code should be 200   
    

匹配器

您可以使用匹配器根据 Postel 法则定义响应

    Given "provider1 name" request 'GET' to '/api/some/1' should return response with 200 and body:
      | parameter    | value                     | match           |
      | id           | 5af55347c9764a6a01684228  | like            |
      | field1       | 35373808218O868           |                 | 
      | blocked      | false                     | boolean         |
      | createdAt    | 2018-05-11T11:00:00+00:00 | dateTimeISO8601 |
  • like - 将定义类型匹配
  • 空值 - 将使用确切值
  • 布尔值 - 将检查布尔类型
  • dateTimeISO8601 - 将与日期格式匹配
  • eachLike - 将与定义的结构匹配