をいじる必要はありsys.pathません。これは、使用している Python のバージョンに依存しません。これは、Python 2.7 または Python 3.x でも同様に機能します。
次のファイル構造があるとします。
Tests/
├── BehaveTest1
│ ├── BehaveTest1.feature
│ └── steps
│ └── test_steps.py
├── BehaveTest2
│ ├── BehaveTest2.feature
│ └── steps
│ └── test_steps.py
├── common.py
├── __init__.py
ディレクトリに が存在すること__init__.pyが重要です。Testsこれは空のファイルですが、これがないと、Python はモジュールをロードできTestsません。これは、パッケージと見なされないためです。
test_steps.py両方のディレクトリで次のようにすることができます:
import Tests.common
Tests/common.pyファイルには次のものが含まれます。
from behave import when, then
@when("foo")
def foo(context):
pass
@then("bar")
def bar(context):
pass
@when@thenBehave がサブディレクトリからロードするファイルに自動的に配置されますが、 でロードするsteps/他のモジュールからはロードされませんimport。
次に、で定義された手順を呼び出す偽の機能ファイルを使用して実行できますcommon.py。
$ behave Tests/BehaveTest*
Feature: BehaveTest1 # Tests/BehaveTest1/BehaveTest1.feature:1
Scenario: foo # Tests/BehaveTest1/BehaveTest1.feature:3
When foo # Tests/common.py:3 0.000s
Then bar # Tests/common.py:7 0.000s
Feature: BehaveTest2 # Tests/BehaveTest2/BehaveTest2.feature:1
Scenario: foo # Tests/BehaveTest2/BehaveTest2.feature:3
When foo # Tests/common.py:3 0.000s
Then bar # Tests/common.py:7 0.000s
2 features passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
4 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s