英文:
Behave background with multiple examples
问题
我正在为在多个类之间继承的情景编写一个功能。我对behave还很陌生,所以希望我是否遗漏了一些基本的东西...
希望能够为它们所有都有一个共享的设置,并使整个功能针对示例运行会很好:
功能:汽车
背景:一旦一辆{car_type}的汽车进入装配线
示例:
|car_type|
|卡车 |
|面包车 |
场景:能够驾驶
当车上添加了车轮
那么它能够驾驶
场景:能够鸣笛
当车上添加了喇叭
那么它能够鸣笛
目前,我必须在每个场景中为背景定义设置,以便我可以定义多个示例。
有什么建议吗?
英文:
I'm writing a feature for scenarios that are inherited between multiple classes. I'm new to behave so hoping I'm missing something basic...
It would be nice to have a shared setup for all of them and have the whole feature be run against the examples:
Feature: Car
Background: Once a {car_type} of car has entered the assembly line
Examples:
|car_type|
|truck |
|van |
Scenario: Can drive
When the wheels are added to the car
Then it can drive
Scenario: Can honk
When a horn added to the car
Then it can honk
Currently instead I have to define the setup for the background in every scenario so I can define multiple examples.
Any suggestions?
答案1
得分: 1
我认为解决您的问题的最佳方法是使用基本语法:
功能:汽车
场景大纲:能够驾驶
假如有一辆 "<car_type>" 类型的汽车进入了装配线
当车轮被添加到汽车上
那么它就能够驾驶
举例:
| car_type |
| 卡车 |
| 面包车 |
场景大纲:能够鸣笛
假如有一辆 "<car_type>" 类型的汽车进入了装配线
当喇叭被添加到汽车上
那么它就能够鸣笛
举例:
| car_type |
| 卡车 |
| 面包车 |
在您的代码中,也可以仅在一个地方定义示例数组作为Python变量,并使用 context.execute_steps()
(详见https://behave.readthedocs.io/en/stable/api.html#behave.runner.Context.execute_steps),但这将导致只有一个巨大的场景,其中将同时测试所有示例,这可能比在所有场景中重复示例更糟糕。
英文:
I think the best solution for your problem is to use the basic syntax:
Feature: Car
Scenario Outline: Can drive
Given Once a "<car_type>" of car has entered the assembly line
When the wheels are added to the car
Then it can drive
Examples:
| car_type |
| truck |
| van |
Scenario Outline: Can honk
Given Once a "<car_type>" of car has entered the assembly line
When a horn added to the car
Then it can honk
Examples:
| car_type |
| truck |
| van |
It could be possible to only define the example array in one place in your code as a Python variable and use context.execute_steps()
(see more in https://behave.readthedocs.io/en/stable/api.html#behave.runner.Context.execute_steps) but you will have only one massive Scenario where will be tested all the examples at once and it would be worst solution than repeating the examples in all Scenarios.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论