我们如何在另一个场景中使用场景大纲中的示例进行测试数据生成?

huangapple go评论58阅读模式
英文:

How can we use the examples in a scenario outline for test data generation in another scenario

问题

我已经使用了SpecFlow,这是一个用于在报名表中输入基本详细信息的报名功能。
流程如下。
报名后,患者和在报名表中填写的详细信息将添加到应用程序中。一旦报名成功,管理员可以打开已报名患者的个人资料并在个人资料中执行操作。

Enrollment.Feature

由于不知道是否能够在不同特性文件的场景中使用相同的示例中传递的变量值,所以没有尝试任何操作。请帮助。

英文:

I have used specflow and This is an enrollment feature where basic details are entered for enrollment in the enrollment form.
The flow is as below.
After enrollment, the patient and the details filled in the enrollment is added to the application. Once enrolled, admin can open the profile of the enrolled patient and perform actions in profile.

Enrollment.Feature

Didnt try anything as I have no idea if we will be able to use the same variable values passed in examples in a scenario in a different feature file's scenario

Please help

答案1

得分: 1

Your scenario Enroll patient from Client Enrollment Page explicitly lists all data in examples table and that is good, as that may be crucial for the feature it describes/tests.

我理解你希望在其他功能的测试中重用此处的内容。但是,在编写BDD场景时,我们应该始终问自己是否将所有重要信息放入场景中,而没有多余的信息。我想象对于您的应用程序其他功能的大多数场景而言,已注册患者的所有或一些详细信息可能是不相关的。然后,您可以使用一个 Given 步骤,来断言应用程序中存在 一些 患者,作为先决条件。

Given enrolled patients exist

或者

Given 10 enrolled patients exist

这些步骤可以使用一些预定义的数据集(来自外部文件、某个项目资源,甚至嵌入在代码中),并迭代创建患者。或者,它们可以只更改应用程序的底层持久存储,例如更新数据库中的表格或替换某个文件。

如果您确信正在编写场景的功能需要患者数据的更多具体信息,您可以构建一个 Given 步骤,使用数据表传递数据给步骤定义,就像这样:

Given following enrolled patients exist
| FirstName | LastName | DOB | ... |
| Sandra | Alpha | 31337 | ... |
| Marie | Beta | 12345 | ... |
| ... | ... | ... | ... |

查看 SpecFlow.Assist Helpers 的文档,其中有一些简化步骤数据表操作的方法,例如仅使用一个方法创建对象的集合:https://docs.specflow.org/projects/specflow/en/latest/Bindings/SpecFlow-Assist-Helpers.html

这种方法很好,因为您可以在许多场景/功能中重用此步骤,并根据您正在描述/测试的功能的需要仅更改数据表内容。

或者,您可以标记需要某个特定患者集的场景,然后在 BeforeScenario 钩子中使用这些标记作为条件,以在场景自身开始之前填充DB(或其他内容)具有特定患者数据集。然后,仍然可以使用类似于上述步骤的 Given 步骤,但其定义只是一个断言。有时甚至应该这样做,以确保阅读场景的人了解设置/先决条件(记住:BDD场景用于描述功能,能够执行为测试只是一种很好的附带产品)。

您可以在每个需要的场景中使用类似于上述步骤的 Given 步骤,或者将其放在功能文件的 Background 中。

相关阅读:https://specflow.org/gherkin/outlines-background-or-hooks/

英文:

Your scenario Enroll patient from Client Enrollment Page explicitly lists all data in examples table and that is good, as that may be crucial for the feature it describes/tests.

I understand your itch to reuse something from here in tests of other features. However, when writing BDD scenarios, we should always ask ourselves if we are putting all the important information in the scenario, and no more. I imagine that for most scenarios on other features of your application all or some details of enrolled patients may be irrelevant. You may then use a Given step that asserts that some patients exist in the app, as a precondition.

Given enrolled patients exist

or

Given 10 enrolled patients exist

Such steps can take some predefined data set (from an external file, or some project resource, or maybe even embedded in code) and iterate over it, creating patients one by one. Alternatively, they can just change the app's underlying persistant storage eg. UPDATE a table in DB or replace some file.

If you are sure that the feature for which you are writing scenarios requires more specificity in patients' data, you can construct a Given step that uses a data table to pass data to the step definition, like this

Given following enrolled patients exist
    | FirstName | LastName | DOB   | ... |
    | Sandra    | Alpha    | 31337 | ... |
    | Marie     | Beta     | 12345 | ... |
    | ...       | ...      | ...   | ... |

Check documentation on SpecFlow.Assist Helpers that has some nice ways to make your life with step's data tables easier, like creating collection of objects with just one method: https://docs.specflow.org/projects/specflow/en/latest/Bindings/SpecFlow-Assist-Helpers.html

This apporach is nice because you can reuse this step in many scenarios/features and just change datatable contents depending on needs of the feature you are describing/testing.

Alternatively, you can tag scenarios that require some particular set of patients and then use these tags as conditions in BeforeScenario hook, to populate the DB (or whatever) with a specific patient dataset even before the scenarios itself starts. Then a Given step similar to the ones before can still can be used, but its definition is just an assertion. Sometimes you even should do that, to ensure that someone reading the scenario understands the setup/precondition (remember: BDD scenarios are for describing the feature, the fact that they can be executed as tests is like a nice by-product).

You can use a Given step similar to the ones above in each scenario that needs it, or put it in a Background of a feature file.

Related reading: https://specflow.org/gherkin/outlines-background-or-hooks/

huangapple
  • 本文由 发表于 2023年6月8日 17:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定