Join Shashi Shekhar for an in-depth discussion in this video Feature file, part of Cucumber Essential Training.
- [Instructor] Wouldn't it be nice if all stakeholders, developers, QA analysts, product owner could refer to the same artifact when they implement or verify requirements? It should be like a document written in simple business language that is understandable to technical, business and quality assurance professionals. Cucumber offers an elegant solution to this problem, it's called a Feature File. You can write concrete implementations of your business scenarios in a business-like language called Gherkin DSL or Gherkin domain-specific language in a feature file. Instead of focusing too much in theory, let's write what is known as a feature file in Eclipse. Let's go ahead and define a feature file here, so I'm going to add a new package, call it Feature Files or Features, within my Features package, I'm going to define a new file and I'm going to call it MenuManagement.feature. And once this file is created, you will notice that Eclipse has added this nice comment header on top where I can specify a bunch of information, also, at the bottom, it has created a sample scenario for me and also, what is known as a Scenario Outline, which we are going to discuss later in this course. Let me go ahead and delete all this and, I need to delete this too, and let's go ahead and define a feature. So the feature we are going to call is Menu Management, within the feature, I'm going to define a scenario, which will be called Add a menu item. Within the scenario, I'm going to define the steps and the steps will be defined as Given I have a menu item with name "Cucumber Sandwich" and price $20 When I add that menu item Then Menu Item with name "Cucumber Sandwich" should be added. Let's save this file. Notice that Eclipse has highlighted the terms Given, When and Then, those are Gherkin keywords. Given represents a precondition, When represents an action and Then represents a result. Two additional Gherkin keywords that can be used here are And and But that represent additional preconditions, actions and results, for example, I could write something like this here, And I have another menu item with name, this is an additional precondition or this is added to the Given section, I'm going to delete that for now. So, I have my scenario defined and I am aware of those five keywords that we can use, note that Feature and Scenario are also keywords and within a feature file, you will have multiple scenarios, within each scenario, you're going to have multiple steps. This is an oversimplified example of how a supervisor or admin user at a restaurant location would add a feature to add an item to the restaurant's menu, but it represents a concrete example of a software feature written in simple business language. So, how does a feature file help? Feature files document common understanding of business requirements. You write your business scenarios in business language that non-programmers can understand. You define your domain objects like Menu and Menu Item in a feature file that helps your developers understand what type of domain objects need to be implemented. Also, as your system evolves, feature files become the ultimate source of truth for all stakeholders, developers, testers and business analysts. I'll go ahead and minimize this file to bring up the package explorer. Two key things to note here are, first of all, the name of the feature file does not matter, but the extension has to be .feature. Secondly, the order of these terms does not matter, I could have defined the scenario as Given, Given and Given, it does not matter to Eclipse or Cucumber, because these steps will be executed in the same order, but it does reduce the readability of your test scenarios, so the recommended path is to use Given, When and Then. You could also change the order of these keywords, you could start with a Then, that will not cause any issues with Cucumber, but that will make your scenario less readable. The last thing I am going to do is to run this feature file from Eclipse, all I need to do is right-click here, Run As, Run Configurations, select the Cucumber Feature, double-click and I have my feature file selected here, click Run, click OK and you will see in the console output that it has given you some suggestions. What it's telling you is that you have these methods that are available to you defined in your feature file, but you do not have the step definitions implemented for these three. So the three steps in our feature file are not implemented, but Cucumber plugin has given us very nice method steps to be implemented in Step Definitions file for each of the feature scenario steps.
- How Cucumber can be used for BDD, TDD, and ATDD
- Installing and configuring Cucumber
- Mapping user stories to features
- Adding hooks
- Generating reports
- Adding a data table
- Adding Selenium dependencies
- Applying techniques to make BDD maintainable