zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Is it correct that a .feature file always maps to a step file?

IT to is File that Feature step maps
2023-09-11 14:14:16 时间

Is it correct that a .feature file always maps to a step file?

Not necessarily. A .feature file in Cucumber or SpecFlow describes a feature or user story in the application under test. The steps in the .feature file are implemented in step definition files, which contain the code that will be executed for each step in the scenario.

So, in general, a .feature file maps to one or more step definition files. However, you can also have multiple .feature files that map to the same step definition file, especially when the steps are reused across multiple scenarios.

In summary, there is a one-to-many relationship between .feature files and step definition files, with each .feature file potentially mapping to multiple step definition files.

 

How can I find a scenario in which step file if they are mapped to multi step files?

In SpecFlow, each scenario in a .feature file maps to one or more steps in a step definition file. The mapping between a scenario and its step definitions is achieved through the use of regular expressions. The step definitions match the steps in the scenario by using the expressions defined in the scenario's Given/When/Then statements.

To find which step file a scenario maps to, you can look at the regular expressions defined in the step definitions. If a step definition matches a step in a scenario, then that scenario maps to that step definition. You can also use the "Go to Definition" feature in your development environment to quickly navigate to the step definition from the scenario. This feature is usually available by right-clicking on a step in the .feature file and selecting "Go to Definition" or a similar option.

 

Is it possible that multi feature files share the same scenario?

Yes, it is possible for multiple .feature files to share the same scenario. This could occur if you have multiple .feature files that are related to different parts of your application, but they have similar functionality that can be described by the same scenario. In this case, you can copy and paste the scenario across multiple .feature files, or you can create a shared repository of scenarios that can be reused across multiple .feature files.

In either case, you would still need to implement the steps for the scenario in a step definition file, so each .feature file that uses the scenario will map to the same step definition file.

 
 

Does it mean that the scenario in different feature could not be same name, otherwise they shared the same scenario?

It is possible to have multiple feature files with the same scenario name, but it is not recommended as it may cause confusion and make it difficult to understand which scenario belongs to which feature. It is better to give each scenario a unique name, even across different feature files, to clearly identify each scenario. Additionally, it can make it easier to maintain and update the test cases in the future.

 

What's the construction of a scenario in Gherkin?

In Gherkin, a scenario has a specific construction and follows a specific structure. The general structure of a scenario in Gherkin is as follows:

Scenario: Some description of the scenario
  Given some preconditions
  And some other preconditions
  When some action is taken
  Then some expected outcome should occur
  And some other expected outcome should occur

 The Scenario: line is followed by a brief description of what the scenario is testing. The next lines (Given, When, and Then) are the steps that make up the scenario. Given steps set up the initial conditions for the scenario, When steps describe the action that is taken, and Then steps specify the expected outcome of the action. The And keyword is used to add additional steps to the Given, When, or Then sections.

Each of these steps is written in a simple, declarative sentence structure, and is intended to be easy to read and understand, even for non-technical stakeholders.

 

How can I share the given across multi scenarios through the Background?

In Gherkin, the Background keyword can be used to define steps that are common to all scenarios in a feature file. This can be used to set up preconditions for the scenarios, and to reduce duplication in the scenario steps.

To share Given steps across multiple scenarios using the Background, you can define the Given steps that you want to share in the Background section, as follows:

Feature: My feature

  Background:
    Given I am logged in as a registered user

  Scenario: My first scenario
    Given I do something
    When I do something else
    Then something should happen

  Scenario: My second scenario
    Given I do something different
    When I do something else again
    Then something else should happen

In the above example, the Given I am logged in as a registered user step is common to both the My first scenario and My second scenario scenarios, and is defined in the Background section. This means that the step will be executed before each scenario, and any subsequent steps in the scenarios will be executed in the context of a logged-in user.

 

list all keyword in Gherkin with index, and explain them

Sure, here's a list of all the keywords in Gherkin:

  1. Feature: Indicates the feature being described
  2. Background: Describes the preconditions for the scenarios in a feature
  3. Scenario: Describes a single scenario
  4. Scenario Outline: Describes a scenario template that can be used for multiple scenarios with different inputs
  5. Examples: Provides inputs to a scenario outline
  6. Given: Describes the preconditions for a scenario
  7. When: Describes the action being taken in a scenario
  8. Then: Describes the expected outcome of a scenario
  9. And: Used to combine multiple Given, When, or Then steps in a scenario
  10. But: Indicates a negative variation of a Given, When, or Then step

In general, Gherkin is a language used for writing feature files in a way that's easy to understand for both technical and non-technical stakeholders. The keywords listed above are used to structure the feature files in a standardized way, so that everyone involved in the project can easily read and understand them. Here's a brief explanation of what each keyword is used for:

  1. Feature: This keyword is used to introduce the feature being described. It should be followed by a short description of the feature.

  2. Background: This keyword is used to describe the preconditions for the scenarios in a feature. It's essentially a set of Given steps that are common to all the scenarios in the feature.

  3. Scenario: This keyword is used to describe a single scenario. It should be followed by a short description of the scenario.

  4. Scenario Outline: This keyword is used to describe a scenario template that can be used for multiple scenarios with different inputs. It should be followed by a short description of the scenario, and then an Examples section that provides inputs to the scenario.

  5. Examples: This keyword is used to provide inputs to a Scenario Outline. It should be followed by a table of inputs.

  6. Given: This keyword is used to describe the preconditions for a scenario. It should be followed by a short description of the precondition.

  7. When: This keyword is used to describe the action being taken in a scenario. It should be followed by a short description of the action.

  8. Then: This keyword is used to describe the expected outcome of a scenario. It should be followed by a short description of the expected outcome.

  9. And: This keyword is used to combine multiple Given, When, or Then steps in a scenario. It should be used after the first Given, When, or Then step.

  10. But: This keyword is used to indicate a negative variation of a Given, When, or Then step. It should be used after the first Given, When, or Then step.

 

 https://cucumber.io/docs/gherkin/reference/#keywords

Keywords

Each line that isn’t a blank line has to start with a Gherkin keyword, followed by any text you like. The only exceptions are the free-form descriptions placed underneath Example/Scenario, Background, Scenario Outline and Rule lines.

The primary keywords are:

There are a few secondary keywords as well:

  • """ (Doc Strings)
  • | (Data Tables)
  • @ (Tags)
  • # (Comments)

Localisation

Gherkin is localised for many spoken languages; each has their own localised equivalent of these keywords.