behave-skill について
PythonのBDDフレームワークBehaveでブラウザーテストを作成するスキルです。Gherkinの機能ファイル、ステップ定義、フック、タグ、レポートに加え、CI/CDとデバッグの資料を扱います。
主な機能
- 01Gherkinの機能ファイルとシナリオ
- 02Pythonのステップ定義
- 03環境フックとタグ
- 04JSONレポートの生成
- 05CI/CDとデバッグのガイド
活用シーン
- 01ログインフローのBDDシナリオ作成
- 02再利用可能なPythonステップの整理
- 03タグによるシナリオの選択実行
- 04JSONテストレポートの生成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill behave-skillインストール後、Claude Code で/behave-skillを入力して読み込まれたか確認します。
同梱ファイルreference/advanced-patterns.mdreference/playbook.md
詳細
Behave BDD スキル
コアパターン
フィーチャーファイル(features/login.feature)
Feature: User Login
As a registered user
I want to log into the application
Background:
Given I am on the login page
Scenario: Successful login
When I enter "[email protected]" as email
And I enter "password123" as password
And I click login
Then I should see the dashboard
And the welcome message should say "Welcome"
Scenario: Invalid credentials
When I enter "[email protected]" as email
And I enter "wrong" as password
And I click login
Then I should see error "Invalid credentials"
Scenario Outline: Login with various users
When I enter "<email>" as email
And I enter "<password>" as password
And I click login
Then I should see "<result>"
Examples:
| email | password | result |
| [email protected] | admin123 | Dashboard |
| [email protected] | wrong | Error |
ステップ定義(features/steps/login_steps.py)
from behave import given, when, then
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
@given('I am on the login page')
def step_on_login(context):
context.browser.get(context.base_url + '/login')
@when('I enter "{text}" as email')
def step_enter_email(context, text):
el = context.browser.find_element(By.ID, 'email')
el.clear()
el.send_keys(text)
@when('I enter "{text}" as password')
def step_enter_password(context, text):
el = context.browser.find_element(By.ID, 'password')
el.clear()
el.send_keys(text)
@when('I click login')
def step_click_login(context):
context.browser.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click()
@then('I should see the dashboard')
def step_see_dashboard(context):
WebDriverWait(context.browser, 10).until(
EC.url_contains('/dashboard')
)
assert '/dashboard' in context.browser.current_url
@then('I should see error "{msg}"')
def step_see_error(context, msg):
error = WebDriverWait(context.browser, 5).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.error'))
)
assert msg in error.text
環境フック(features/environment.py)
from selenium import webdriver
def before_all(context):
context.base_url = 'http://localhost:3000'
def before_scenario(context, scenario):
context.browser = webdriver.Chrome()
context.browser.implicitly_wait(10)
def after_scenario(context, scenario):
if scenario.status == 'failed':
context.browser.save_screenshot(f'screenshots/{scenario.name}.png')
context.browser.quit()
タグ
@smoke
Feature: Login
@critical
Scenario: ...
behave --tags=@smoke
behave --tags="@smoke and not @slow"
セットアップ:pip install behave selenium
実行:behave または behave features/login.feature
TestMu AI でのクラウド実行
環境変数を設定します:LT_USERNAME、LT_ACCESS_KEY
# environment.py
from selenium import webdriver
import os
def before_scenario(context, scenario):
lt_options = {
"user": os.environ["LT_USERNAME"],
"accessKey": os.environ["LT_ACCESS_KEY"],
"build": "Behave Build",
"name": scenario.name,
"platformName": "Windows 11",
"video": True,
"console": True,
"network": True,
}
options = webdriver.ChromeOptions()
options.set_capability("LT:Options", lt_options)
context.driver = webdriver.Remote(
command_executor=f"https://{os.environ['LT_USERNAME']}:{os.environ['LT_ACCESS_KEY']}@hub.lambdatest.com/wd/hub",
options=options,
)
レポート:behave --format json -o report.json
応用パターン
本番環境向けのパターンについては、reference/playbook.md を参照してください。
| セクション | 内容 |
|---|---|
| §1 プロジェクトのセットアップ | behave.ini、プロジェクト構成、依存関係 |
| §2 フィーチャーファイル | Scenario Outline、データテーブル、Background を使った Gherkin |
| §3 ステップ定義 | 型の登録、API ステップ、PyHamcrest を使った共通ステップ |
| §4 環境フック | before_all/scenario/feature、失敗時のスクリーンショット、DB の分離 |
| §5 ページオブジェクト | 待機処理を含む BasePage、LoginPage、再利用可能なコンポーネント |
| §6 フィクスチャとテストデータ | DatabaseHelper、トランザクションのロールバック、JSON データローダー |
| §7 LambdaTest の統合 | リモートブラウザーの作成、クラウド機能 |
| §8 CI/CD の統合 | Postgres、Selenium、Allure レポートを使った GitHub Actions |
| §9 デバッグ表 | よくある問題 12 件とその原因、修正方法 |
| §10 ベストプラクティス | BDD テストのチェックリスト 14 項目 |
コメント
Markdown 対応