BDDフレームワーク:behave学習記録


変換元:https://www.cnblogs.com/helenMemery/p/6429492.html
1 behaveのインストール
pythonをインストールしたらpip install behaveコマンドを使用してbehaveをインストールします
-----behaveの公式サイト:http://pythonhosted.org/behave/
2簡単な例
次のファイルを新規作成します.ファイル構造は次のとおりです.
                  firstCase
                  firstCase/wordcheck.feature
                  firstCase/steps
                  firstCase/steps/wordcheck.py
wordcheck.Featureの内容は以下の通りです.
Feature: word check
  Scenario: Check letters
    Given I have a letter y
    When I input letter y
   
Then the inputed letter is Equal with y

wordcheck.pyの内容は次のとおりです.
__author__ = 'helen'
from behave import *

@Given('I have a letter')
def step_impl(context):
    pass

@When('I input letter {letter}')
def step_impl(context,letter):
    context.letter = letter

@Then('the inputed letter is Equal with {TargetLetter}')
def step_impl(context,TargetLetter):
    assert context.letter is context.TargetLetter

この2つのファイルを完了すると、最初のcaseが完了し、次にcmdコマンドで実行します.
初めて実行した結果、次のようになりました.TargetLetterパラメータはありません.
  BDD框架:behave学习记录_第1张图片
チェックしたpy,@Thenの部分がTargetLetterに値を付けるのを忘れたことに気づき,赤い枠の部分を加えた.
  BDD框架:behave学习记录_第2张图片
2回目の運転は、次の図のように実行されます.
  BDD框架:behave学习记录_第3张图片
 
こうして、簡単な例が完成したのですが、簡単なのではないでしょうか(*^^*)ヒヒ…
 
3 featuresフォルダ
私が書いた例の「firstCase」フォルダは、今言っているfeaturesフォルダに相当します.
簡単に説明します.义齿pyファイル:
1)         .featureファイルはテストフィールドを作成するために使用され、さまざまなシーンとデータを書くことができ、中国語をサポートすることができます.
2)         .pyファイルは、あなたが書いたテストシーンとデータに基づいてテストを実行します.
3)一番いいです.ファイルと.pyファイルは一つ一つ対応します.
 
公式サイトのチュートリアルで説明したように、簡単なプロジェクトファイルの構造は以下の通りです.
features/

features/everything.feature
features/steps/
features/steps/steps.py
複雑な点の構造は次のとおりです.
features/
features/signup.feature
features/login.feature
features/account_details.feature
features/environment.py
features/steps/
features/steps/website.py
features/steps/utils.py

4         .featureファイルの紹介
ひとつfeatureファイルには、次のように複数のScenarioを書くことができます.
Feature: comparison
  Scenario: comparison two words
    Given I have two words
    When input a and a
   
Then two words Equal

  Scenario: comparison two numbers
    Given I have two numbers
    When I input number1 3 and number2 2
   
Then the first number big then the last number

以下、これらのキーワードについて簡単な解析を行います.
1)Feature:機能名
2)Scenario:シーン名
3)Given:テストの前提条件を与える;
4)when:私たちのテスト手順に相当します.
5)Then:期待結果を与える
次のように「And」または「But」を追加できます.
Feature: word check
  Scenario: Check letters
    Given I have a letter
    When I input letter y
     
But I just want to show the key word "But"
    Then the inputed letter is Equal with y
     
And continue to next case

でも気をつけてねpyには、次の図のように、対応するステップの処理方法を書き込みます.「When I just want to show the key word“But”」と「Then continue to next case」に対応する方法を追加します.
  BDD框架:behave学习记录_第4张图片
 
4.1  Scenario Outlines
同じシーンをテストする場合、さまざまなデータを入力して異なる結果出力を検証する必要がある場合が多く、Scenario Outlinesで実現できます.次の図のように、シーンを検証するために大文字と小文字を入力します.
Feature: word check
  Scenario Outline: Check letters
    Given I have a letter
    When I input letter
   
Then the inputed letter is Equal with

  
Examples: Lowercase letters
    |keyword|targetword|
    |
a      |a         |
    |
b      |b         |

    Examples:
Capital letters
    |keyword|targetword|
    |
F      |F         |
    |
D      |D         |

実行結果は次のとおりです.
  BDD框架:behave学习记录_第5张图片
 
4.2シーンにコメントを追加
シーンでは、「」「」を使用して、Contextの「.text」属性値になるコメント情報を出力できます.次の図のように、GivenWhenThenにコメントを追加します.
Feature: word check
  Scenario : show notes
    Given I want to show some notes
    """
    This is Given Notes
    """
    When just want to show some notes
    """
    This is When notes
    """
    Then there is a word "Then" in the attribute ".text"
    """
    This is Then notes
    """

そしてpyファイルでは最後に「Then」にContextが存在するか否かを判断する.textでは、次のコードがあります.
__author__ = 'helen'
from behave import *

@Given('I want to show some notes')
def step_impl(context):
    pass

@When('just want to show some notes')
def step_impl(context):
    pass

@Then('there is a word "Then" in the attribute ".text"')
def step_impl(context):
    assert context.text.find("Then")

最後に実行が通過し、実行結果は以下の通りで、結果にコメントが出力されます.
  BDD框架:behave学习记录_第6张图片
 
4.3 tableの使用
上のcontextとtextと同様に、シーン内の1つのテーブルをcontextとすることができる.Tableプロパティの値は、次の図のようにGivenにテーブルを追加します.
Feature: show the table
  Scenario: some scenario
  Given a set of specific users
     | name      | department  |
     |
Barry     | Beer Cans   |
     |
Pudey     | Silly Walks |
     |
Two-Lumps | Silly Walks |

 When
we count the number of people in each department
 Then we will find two people in "Silly Walks"
  But we will find one person in "Beer Cans"

 
 
 
 
 
6         Environment.py
Environment.pyはfeatureフォルダの下に置く非常に重要なファイルです.featureファイルが並列に表示されます.次はEnvironmentです.pyで定義されたいくつかの方法:
before_step(context, step), after_step(context, step)
These run before and after every step.
before_scenario(context, scenario), after_scenario(context, scenario)
These run before and after each scenario is run.
before_feature(context, feature), after_feature(context, feature)
These run before and after each feature file is exercised.
before_tag(context, tag), after_tag(context, tag)
These run before and after a section tagged with the given name. They are invoked for each tag encountered in the order they’re found in the feature file. See controlling things with tags.
before_all(context), after_all(context)
These run before and after the whole shooting match.
以下に簡単な例を示します.必要に応じて方法を定義できます.
# coding:utf-8
__author__ = 'helen'
import sys
from behave import *
from selenium import webdriver

# , utf-8
def before_all(context):
    reload(sys)
    sys.setdefaultencoding('utf-8')

def before_feature(context):
    context.driver = webdriver.Firefox()

def after_feature(context):
    context.driver.quit()

 
7ラベルtagによるテスト実行の制御
ラベルは@で始まり、次の例を示します.
Feature: find a look
  @valid
  Scenario: look up a book
   Given I search for a valid book
    Then the result page will include "success"

  @invalid
  Scenario: look up an invalid book
    Given I search for a invalid book
     Then the result page will include "failure"

実行時にbehaveの後ろにtagラベルを付けるといいです.「valid」というシーンだけをテストする場合は、「behave--tags=valid」と入力し、下図のように1つのシーンをスキップして無視します.
  BDD框架:behave学习记录_第7张图片
 
いくつかの異なるラベルのシーンを実行したい場合は、「behave--tags=valid,invalid」と書くことができます.
@invalid以外のすべてのシーンを実行したい場合は、「behave--tags=-invalid」と書くことができます.
ラベルに「valid」と「invalid」の2つのラベルが含まれているシーンを実行する場合は、「behave--tags=valid--tags=invalid」と書くことができます.
もちろんtagsはpyファイルでも機能します.例えば
def before_feature(context):
    if 'browser' in feature.tags:
        context.driver = webdriver.Firefox()

def after_feature(context):
    if 'browser' in feature.tags:
        context.driver.quit()

転載先:https://www.cnblogs.com/hanfanfan/p/9646788.html