行为测试的模块化结构
Modular structure for behave tests
现在我有以下测试功能目录:
Tests/
--BehaveTest1/
----BehaveTest1.feature
----steps/
------test_steps.py
--BehaveTest2/
----BehaveTest2.feature
----steps/
------test_steps.py
由于BehaveTest1 和BehaveTest2 的测试步骤是通用的,所以我想实现一个通用模块,以便两个测试用例在需要时都可以调用。目前,我在 Tests/ 文件夹中创建了一个 common/ 目录并通过以下方式导入它(在每个测试功能的 test_steps.py 文件中):
import sys, os
sys.path.append('../common')
import common
但是我不想弄乱路径所以我想知道是否有更好的方法来使用行为测试功能的结构来做到这一点?
实际上没有其他方法像你这样做的方法:
您想从某个位置导入代码。这意味着您需要让 python 知道这个位置。这是通过 PYTHONPATH 或 sys.path.append().
完成的
Behave 是(据我所知)只能在功能文件所在的 "steps" 目录中找到代码。如果你有额外的代码,你必须设置 sys.path.
在 python > 3.3 中会更容易一些,因为 "namespace" 包 (pep420) 可以调用
:$ behave Tests/BehaveTest1/BehaveTest1.feature
在您的测试目录的父文件夹中。那么你将不得不做
import Tests.common
在您的步骤文件中。
这是因为测试、BehaveTests1 和 BehaveTests2 将成为一个 python 包。
没必要乱用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
Tests
目录中 __init__.py
的存在是关键。 它是一个空文件,但没有它,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
和 @then
会自动放入 Behave 从 steps/
子目录加载的文件中,而不是从您使用 import
加载的任何其他模块加载的文件中。 =25=]
然后我可以 运行 使用伪特征文件调用 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
对我来说最灵活的方法是在我的 steps 文件夹中创建一个 类 的文件夹:
features/test.feature
test_steps/
test_steps/classes
test_environment.py
可能有这样的结构,如果我 运行 这个命令,alice.features 和 bob.features 的步骤将 运行:"behave"或 "behave --tags @"
DIRECTORY STRUCTURE:
+-- features/
+-- steps/ (optional, common steps)
+-- alice.features/
| +-- steps/ (specific steps for alice sub features, can use common steps)
| +-- *.feature
+-- bob.features/
+-- steps/
+-- *.feature
+-- environment.py
现在我有以下测试功能目录:
Tests/
--BehaveTest1/
----BehaveTest1.feature
----steps/
------test_steps.py
--BehaveTest2/
----BehaveTest2.feature
----steps/
------test_steps.py
由于BehaveTest1 和BehaveTest2 的测试步骤是通用的,所以我想实现一个通用模块,以便两个测试用例在需要时都可以调用。目前,我在 Tests/ 文件夹中创建了一个 common/ 目录并通过以下方式导入它(在每个测试功能的 test_steps.py 文件中):
import sys, os
sys.path.append('../common')
import common
但是我不想弄乱路径所以我想知道是否有更好的方法来使用行为测试功能的结构来做到这一点?
实际上没有其他方法像你这样做的方法:
您想从某个位置导入代码。这意味着您需要让 python 知道这个位置。这是通过 PYTHONPATH 或 sys.path.append().
完成的Behave 是(据我所知)只能在功能文件所在的 "steps" 目录中找到代码。如果你有额外的代码,你必须设置 sys.path.
在 python > 3.3 中会更容易一些,因为 "namespace" 包 (pep420) 可以调用
:$ behave Tests/BehaveTest1/BehaveTest1.feature
在您的测试目录的父文件夹中。那么你将不得不做
import Tests.common
在您的步骤文件中。
这是因为测试、BehaveTests1 和 BehaveTests2 将成为一个 python 包。
没必要乱用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
Tests
目录中 __init__.py
的存在是关键。 它是一个空文件,但没有它,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
和 @then
会自动放入 Behave 从 steps/
子目录加载的文件中,而不是从您使用 import
加载的任何其他模块加载的文件中。 =25=]
然后我可以 运行 使用伪特征文件调用 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
对我来说最灵活的方法是在我的 steps 文件夹中创建一个 类 的文件夹:
features/test.feature
test_steps/
test_steps/classes
test_environment.py
可能有这样的结构,如果我 运行 这个命令,alice.features 和 bob.features 的步骤将 运行:"behave"或 "behave --tags @"
DIRECTORY STRUCTURE:
+-- features/
+-- steps/ (optional, common steps)
+-- alice.features/
| +-- steps/ (specific steps for alice sub features, can use common steps)
| +-- *.feature
+-- bob.features/
+-- steps/
+-- *.feature
+-- environment.py