我刚刚开始使用behave,一个使用Gherkin 语法的Pythonic BDD 框架。行为需要一个功能,例如:
Scenario: Calling the metadata API
Given A matching server
When I call metadata
Then metadata response is JSON
And response status code is 200
Run Code Online (Sandbox Code Playgroud)
还有一个步骤文件,例如:
...
@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
assert_equals(context.response.status_code, int(expected_status_code))
@then('metadata response is JSON')
def step_impl(context):
json.loads(context.metadata_response.data)
...
Run Code Online (Sandbox Code Playgroud)
并将它们组合成一个漂亮的测试报告:

其中一些步骤 - 例如:
metadata response is JSONresponse status code is {expected_status_code} 在我的许多项目中使用,我想将它们分组到一个通用步骤文件中,我可以导入和重用该文件。
我尝试将有用的步骤提取到单独的文件中并将其导入,但收到以下错误:
@then('response status code is {expected_status_code}')
NameError: name 'then' is not defined
Run Code Online (Sandbox Code Playgroud)
如何创建通用步骤文件并导入它?
考虑一个Behave场景:
When some magic number is generated
Then the number should be greater than 5
Run Code Online (Sandbox Code Playgroud)
所以我有一个@when函数产生(比方说)一个随机数,我需要在@then条件测试中出现这个数字.
如何将一步结果传递给另一步?
正如标题所示,我希望在场景大纲之前运行某些配置/环境设置步骤.我知道有Background这样做的场景,但Behave将场景大纲分成多个场景,从而为场景大纲中的每个输入运行背景.
这不是我想要的.由于某些原因,我无法提供我正在使用的代码,但是我将编写一个示例功能文件.
Background: Power up module and connect
Given the module is powered up
And I have a valid USB connection
Scenario Outline: Example
When I read the arduino
Then I get some <'output'>
Example: Outputs
| 'output' |
| Hi |
| No |
| Yes |
Run Code Online (Sandbox Code Playgroud)
什么会发生在这种情况下是会舞动动力循环和检查每个输出USB接口Hi,No,Yes导致三个电源周期和三个连接检查
我想要的是使用一次电源循环并检查连接一次,然后运行所有三个测试.
我该怎么做呢?
我正在使用 Pycharm 编写测试并以行为方式运行它们。我正在使用 cli 运行行为命令。为了编写功能和场景,我正在使用 Pycharm。我如何调试每个步骤?
我正在用 Python 研究 BDD。结果的验证是一个拖累,因为正在验证的结果不会在失败时打印。
比较行为输出:
AssertionError:
File "C:\Python27\lib\site-packages\behave\model.py", line 1456, in run
match.run(runner.context)
File "C:\Python27\lib\site-packages\behave\model.py", line 1903, in run
self.func(context, *args, **kwargs)
File "steps\EcuProperties.py", line 28, in step_impl
assert vin == context.driver.find_element_by_xpath("//table[@id='infoTable']/tbody/tr[4]/td[2]").text
Run Code Online (Sandbox Code Playgroud)
到 SpecFlow+NUnit 输出:
Scenario: Verify VIN in Retrieve ECU properties -> Failed on thread #0
[ERROR] String lengths are both 16. Strings differ at index 15.
Expected: "ABCDEFGH12345679"
But was: "ABCDEFGH12345678"
--------------------------^
Run Code Online (Sandbox Code Playgroud)
使用 SpecFlow 输出可以更快地找到失败原因。要获取出错的变量内容,必须手动将它们放入字符串中。
来自生菜教程:
assert world.number == expected, \
"Got %d" % world.number
Run Code Online (Sandbox Code Playgroud)
从 …
我想在behave遇到异常时显式地使该步骤失败
例如。我正在根据行为文档编写代码 -
from behave import *
@when('verify test fails.*?(?P<param_dict>.*)')
def test_logger(context, param_dict):
try:
logger.info("testing the logger. this is info message")
logger.info(1/0)
except Exception as e:
logger.error("arrived at exception: "+str(e))
fail("failed with exception: "+str(e))
Run Code Online (Sandbox Code Playgroud)
但它抛出这个错误:
NameError:未定义名称“失败”
我也尝试了其他方法,但没有任何效果,例如。context.failed = True (也没有工作)
如果我不尝试显式失败,即使它进入异常块,最终测试结果也会成为 PASS ......这很奇怪。
我有一个 Python 3.4 项目,其中包含在behave框架(版本 1.2.5)中构建的测试。当我运行测试时,我得到数百行输出,其中大部分描述了没有问题的步骤。当场景失败时,我需要滚动所有这些输出来查找失败(这很容易注意到,因为它是红色的,而通过的步骤是绿色的,但我仍然需要查找它)。
有没有办法behave只显示失败场景的输出?理想情况下,我将获得所有失败场景的输出,以及通过/失败/跳过的功能/场景/步骤数量末尾的摘要。如果它打印出所有内容但将所有失败放在底部,我也会感到满意。
我已经behave --help浏览过这个网站,但没有找到任何相关内容。然而,我肯定不是第一个对此感到恼火的人,我想有一些方法可以做到这一点。谢谢您的帮助!
编辑:该--quiet标志简化了输出,但不会删除它。例如,这个输出:
Scenario Outline: Blank key identification -- @1.3 blank checks # tests/features/tkg.feature:15
Given we have pages with the wrong checksum # tests/features/steps/tkg_tests.py:30 0.000s
When we check if the key is blank # tests/features/steps/tkg_tests.py:50 0.000s
Then it is not blank # tests/features/steps/tkg_tests.py:55 0.000s
当使用该--quiet标志运行时变为:
Scenario Outline: Blank key identification -- @1.3 blank checks
Given we have pages with the wrong checksum …
我的组织正在寻求迁移到 Amazon Quicksight 以取代我们现有的报告解决方案。作为我们搬家之前审查的一部分,我被要求研究为 Quicksight 实施创建自动化测试的选项,关键是确保在仪表板中正确呈现正确的数据,仪表板可以在各种不同的浏览器上工作,并且任何计算都是准确且一致的。
在网上我找不到太多关于 Quicksight 实现的测试方法的文档,我们当前的测试框架是 Py-Behave。是否有任何文档在网上提供测试示例,最好是行为测试的示例。
如果没有,那么 Quicksight 是否允许轻松进行自动化测试,是否可以通过 Selenium 测试等直接测试仪表板,以及是否可以轻松地直接测试任何“数据模型”以确保应用正确的计算等?
我正在尝试通过测试资源管理器在 VS Code 中运行我的行为场景。我的功能文件和步骤文件已正确绑定,因为同一个项目在 Pycharm 专业版(试用版)上运行。我没有看到我的测试资源管理器加载我的场景。我需要设置哪些配置?我是Python新手。我已经安装了以下支持行为的扩展。
下面是我的 launch.json
{
"python.testing.pytestArgs": [
"venv"
],
"python.testing.unittestEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": false,
"python.testing.unittestArgs": [
"-v",
"-s",
"./Tests",
"-p",
"*test.py"
]
}
Run Code Online (Sandbox Code Playgroud)
我想定义一个基于 PyTest-BDD 的场景大纲,其中包含多个示例。示例片段:
Scenario Outline: front to back validation
When tester executes access view sql query <sqlCommandProp> into av dataframe
And tester adds investment quant id to av dataframe
And tester reads raw file <fileNameProp> from datalake into raw dataframe
@raw2AccessValidation
Examples:
|sqlCommandProp|fileNameProp|
|sqlCommand | fileName |
@raw2AccessValidation2
Examples:
|sqlCommandProp|fileNameProp|
|eric | shane |
Run Code Online (Sandbox Code Playgroud)
我想为每个示例都有单独的标签,因为我可能不想运行所有示例。
我已经尝试了上面的方法,发现多个示例都可以。但是,我似乎无法识别不同的标签,因此我无法指定要运行这两个(或更多)中的哪一个。
我问是因为这可以用 java/cucumber 引擎完成。想知道我是否使用 pytest-bdd 遗漏了什么,做错了什么?
谢谢
python-behave ×10
python ×9
bdd ×3
pytest-bdd ×2
gherkin ×1
lettuce ×1
pycharm ×1
pytest ×1
scenarios ×1
testing ×1