Gherkin - real or representative scenarios? - cucumber

Lets say that my app works with some books with real titles "The Old Man and the Sea", "War and Peace", etc., when creating scenarios, should I use real title like:
Given I have a book "War and Peace" persisted
When ...
or should I do something like:
Given I have a book "Book1" persisted
When ...
Option 2 is more generic, but artificial example. And If I use first option, person who is reading the test has to have domain knowledge, and he will also have some presumptions about the scenario as soon as he reads the title of the book.
Also, is there some simpler way for me to create data table without repeating data (in this case page where I have always to repeat 1,1,2,2,2,2...)? example:
When we receive book with following content:
| Page | Line | Text |
| 1 | 1 | a |
| 1 | 2 | b |
| 2 | 1 | a |
| 2 | 2 | b |
is this standard way to do it:
When we receive a book
And page 1 has content
| Line | Text |
| 1 | a |
| 2 | b |
And page 2 has content
| Line | Text |
| 1 | a |
| 2 | b |

First of all start with the name of the scenario, this name should be meaningful and should be like a summary about what is about the test.
Once you have the name then the other steps should describe a business flow that of course should contain domain language, because for example if i don't know nothing about healthcare, banking etc then why would I understand a test about a specific domain subject?, the scenarios are for a specific group of people (the ones that are working in the specific domain).
One of the BDD role is to help in understanding better the specifications and the application on all levels (technical to non-technical, but on the same business domain), to improve communication.
Now for your specific issue.
Given I have a book "War and Peace" persisted does not offer to much info since the title of the book says nothing about the test data; is a new book that just was added/created?, is a type of book technical/poetry or just some book?
What was useful for me is use a name for the the data that says something about the data used in the test.
If you don't have different types of books you can use any name, else a more complete name would be more useful.
As for the table, that represents a data set and you need to tell what to check and where; depending by case you could group some checks, if you can read all data at once or not, or if you need to specify the texts/pages.
One option would be to hide the data set and say something like:
Given I have a book "War and Peace" persisted
Then the book contains the expected content for "War and Peace"
in the first step "War and Peace" - gets/creates a specific book that is identified by this title
in the second step "War and Peace" - identifies a set of data for the expected result using the same name since is the expected for that specific data set, this set of data can be list/array/map ... depending of what programming language you are using.
Don't think to much to the details, just define the scenario in human readable language using outside-in approach, then see if you can refine it and after start the implementation.
Always use a description for the feature and a meaningful title for each scenario

Related

How can I display multiple line scenario text in extend reports?

In my feature file, using the same scenario I am checking more than one requirements. I have written the scenario like below:
Scenario: My first requirement ID
My second requirement ID
My third requirement ID
Etc
After execution, the extend report shows only the result as
Scenario: My first requirement ID
How can I get all the three I D,s in extent report.
NOTE:Each of my scenario title is lengthy.
Can you explain your scenario text a little bit more? According to the documentation, the scenario should describe in human terms what we expect the software to do. It is quite unusual to include expected data in that scenario text. Are you using the ID from an enum? If that is the case, it would be better to spell out the enum in human readable terms. Scenario: UserType is Administrator for example. Another option would be to use a Scenario Outline, something like
Scenario Outline: My generic requirement statement
Given Id <whateverId> is provided
When I do <activity>
Then I expect to see <result>
Examples:
| whateverId | activity | result |
| 12 | firstMethod | MyResult |
| 20 | secondActivity | anotherResult |
| 42 | thirdExample | thirdResult |
The variable names provided in the outline in angle brackets become the column headers in the examples grid. Just be sure to indent the grid below the Examples: line and also include the pipe | on both the left and right boundaries of the grid. Hopefully that helps.

Cucumber: How to execute the entire list of Scenario Outline along with example for a different set of attributes

I have a situation where I need to run the scenario outline along with all the datatable for different set of value. I am looking for an datatable inside another datatable. That I need to run my entire list of examples of a scenario outline repeatedly for the given list of products.
Note: I am trying to avoid write different scenario for each product.
I have given some example and my problem statement for better understanding as below
Scenario Outline : Check the behaviour of all the products
Given the POST retrieveProductdetails api url with valid authorization
When POST api is applied for the <"Products">
Then verify the behaviour of all the <"Properties"> and its <"result">
Examples:
|Properties |result|
|Appearance | Successful|
|reading | Successful|
|writing |Successful|
|memo |Successful|
|Singing |Successful|
|Help |Successful|
|Adancefeature |Successful|
|Antiquefeatuer |Succesful|
|AI nature |Successful|
|Interaction |Successful|
Note : I have around 20 Products to be validated and for each and every product i need validate all the 10 properties as mentioned .
If I start to write a an third Variable like as below , I will end up in writing 200 lines/examples (20 *10 = 200 ). And similar to the above scenario i have around 25 to 30 details which needed to be validate for all 20 products . The maintenance will be very difficult. Is there any better option for this ?
Examples:
|Properties |result |Products|
|Appearance | Successful |Alexa|
List of Products
|Products|
|Alexa|
|firetv|
|GoogleHome|
|Chromecast|
|SmartHub|
|SmartTV|
|AmazonVideo|
|AmazonPhoto|
|Echo|
|Echo Dot|
|Echo Show|
|Ring|
.
.
.
.
|SmartHome|
You are making this very difficult on yourself for a couple of reasons.
You are not describing the behavior of the system. I'm not sure what your application is supposed to do but it seems that you have a particular type of product and all instances of that type of product should have certain flags set.
However you didn't write this down, rather you appear to be retrieving all products of that type from a database and checking if these have the right flags set. So I have to infer the behavior of the system from your scenario. This should be the other way around.
You are trying to programming in Gherkin. Steps in Gherkin are not steps in a test script. They do not have to describe the exact operation needed to get some result. When you use Gherkin to describe the behavior of a system it shouldn't matter if you talk to the system in a unit tests, via http or a browser.
However by describing the exact operations you are painting yourself in a corner. It means that you can't effectively generalize without using programming language constructs like loops. If you step away from describing exact operations and rather try to describe what the system does you can use a much bigger vocabulary.
You appear to be testing against fixed data. Your data appears to have been put into the system already. You are merely checking if it comes out alright. This is not a good test because it assumes the system is in a particular state rather then creating the system in that state or verifying it is.
So to fix your feature file you might want to something like this:
Scenario: All smart home products are in the category of AI powered spy-devices
Given the smart home product "<Product>"
When I inspect this smart home product
Then it has all the properties of an AI powered spy-device:
| Appearance |
| reading |
| writing |
| memo |
| Singing |
| Help |
| Adancefeature |
| Antiquefeatuer |
| AI nature |
| Interaction |
Examples:
| Product |
| Alexa |
| firetv |
| GoogleHome |
| Chromecast |
| SmartHub |
| SmartTV |
| AmazonVideo |
| AmazonPhoto |
| Echo |
| Echo Dot |
| Echo Show |
| Ring |
.
.
.
.
| SmartHome |
While in the Given step you'd normally create the product, in your case you'll have to fetch the catalogue of products and verify that the catalogue contains the product. In the When step you'd probably fetch the details for the product. Finally in the Then step you'd verify if all properties have been set when looking at the details.
edit:
If you actually want to check if all the data has been entered into the system correctly you could also do something like this:
Scenario: All smart home products are in the category of AI powered spy-devices
Given the smart home product "<Product>"
When I inspect this smart home product
Then it has all the properties:
| Appearance | <Apperance> |
| reading | <Reading> |
| writing | <....> |
| memo | |
| Singing |
| Help |
| Adancefeature |
| Antiquefeatuer |
| AI nature |
| Interaction |
Examples:
| Product | Apperance | Reading | ....
| Alexa | Yes | No
| firetv | No | Yes
| GoogleHome | Yes | No
.
.
.
.
| SmartHome | No | Yes | ....
But I would suggest not using Cucumber for this. In that case you'd be better of putting your data into an excel file and using JUnit5s parameterized test.

How much data should be given in a cucumber feature file?

I'm trying to write some Gherkin feature files in order to do BDD acceptance testing using SpecFlow. The system I'm trying to test consists of multiple RESTful APIs - system has a microservice architecture. In a scenario, I need to be certain that some records already exist in the database prior to going with the actual scenario, so I've included a Background section with a given part. The problem I'm having is that each of those records that need to exist are created through APIs that require lots of data in their schema contact and the team requires that I specify each and every fields and their respective values in a record in a gherkin table. The result is something like this:
| PassportExpireDate|PassportNumber|PassportCountry |Firstname|Lastname|LocalFirstname|LocalLastname | Birthday | NationalNumber | NationalityCountryId | PassengerType | Gender |PartyId | SourceTravelerId | CellNumber | Price|
This is the header of one of my tables which is going to be used to create a Traveler record in the database before starting the actual test by specification. However, as you can see this table has too much fields and therefore is too long too fit on the screen and thus very hard to read and maintain. secondly it's tightly coupled to the DTO schema. I argued that we shouldn't put this much detail on our specificatons, trying to include only vital high-level data (e.g. given we have an existing traveler named "James Peterson") but the team and the CTO insisted that these details should be present on the feature file. In my next attempt, I broke the tables into multiple tables (e.g. personal data, order data, passport data, etc.).
But I'm still confused and I think I'm still not doing the wrie thing. What's your recommendation? Do we have any rule of thumb or best practices for this?
Can you transpose the filed and values in the data table as below.
|field |values |
| PassportExpireDate |[] |
| PassportNumber |[] |
| PassportCountry |[] |
| Firstname |[] |
| Lastname |[] |
| LocalFirstname |[] |
| LocalLastname |[] |
| Birthday |[] |
| NationalNumber |[] |
| NationalityCountryId |[] |
| PassengerType |[] |
| Gender |[] |
| PartyId |[] |
| SourceTravelerId |[] |
| CellNumber |[] |
| Price |[] |
And in the step def implement the logic to get the values from the values array.
Specflow supports external data binding for such cases. You can use Excel binding to keep your feature file fit.
Scenario Outline: Add Traveler
Given ...
When ....
Then ....
#source:TravelerRecordsExamples.xlsx
Examples:
| PassportExpireDate|PassportNumber|PassportCountry |Firstname|Lastname|LocalFirstname|LocalLastname | Birthday | NationalNumber | NationalityCountryId | PassengerType | Gender |PartyId | SourceTravelerId | CellNumber | Price|
TLDR None
Don't put definitions and data in a Gherkin tables, its incredibly counter productive and error prone. Instead use something else to specify the fields (ideally the source code of the api) and name each thing.
Then use simple Givens to create you things.
Now in your case you seem to be creating travelers. The behavior your Gherkin is documenting is the creation of travelers. HOW travelers are created and what their characteristics are have no place in this description of the behavior.
So your background steps become something like
Given there are foo travelers
an the implementation is something like
Given 'there are foo travelers' do
create_foo_travelers
end
and now you have translated you problem from
from
How do I do something incredibly stupid and difficult in Gherkin
to
How do I write some code to create travelers
This is the approach you should take to writing all scenarios when Cuking. The scenario should only document the WHAT and WHY of the behavior. Any details about HOW the behavior is implemented have no place in the scenario.
The true power of cuking is using natural language, naming and abstraction to make you cukes simple. Use these skills to delegate the complexity of HOW to more appropriate tools.

Identifying and comparing syntactic structure of questio-sentence

I am getting question from user and trying to understand syntactically.
My goal is to identify the exact question sentence from user entered question. Like
Obama is president of USA, who is his wife?
So I am able to apply anaphora resolution and get his pointing to Obama and can convert above sentence to
Obama is president of USA, who is Obama wife?
but how can I syntactically identify exact question sentence i.e. Who is obama wife? from above entire question
I am trying with pylinkgrammar which give 54 linkage for above sentence, like
linkparser>
Linkage 54, cost vector = (UNUSED=0 DIS= 8.05 LEN=24)
+------------------------------Xp------------------------------+
+---------------------->WV---------------------->+ |
+-------------------Xx-------------------+-->WV->+---SIs---+ |
+----Wd---+--Ss--+--Oum--+---Mp--+-Js+ +Wq+--Q-+ +Ds**c+ |
| | | | | | | | | | | |
LEFT-WALL Obama[!] is.v president.t of USA.l , who is.v his wife.n ?
What I want to do it defining pattern for different question type like W5H1, conjunction based question etc.
But I dont find how to write rule for these pattern, any suggestion and reference would be much appreciable?
You can try to extract different possible sub-questions (hypotheses) from your original text and test for textual entailment between your text and hypotheses. Check out http://hltfbk.github.io/Excitement-Open-Platform/#Recognizing_Textual_Entailment

Is it possible to use 2 different examples table in Cucumber/Cuke4Duke

Is it possible to somehow construct a Scenario which uses two different Example tables in different steps? Something like this:
Given I log in
When I view a page
Then I should see <goodText>
Examples:
|goodText|
|abc|
And I should not see <badText>
Examples:
|badText|
|xyz|
The scenario above doesn't work, also in reality there would be more rows to each table.
It looks like you're confusing tables with scenario examples. You can mix them, but from your example I'm not sure what you're trying to achieve. Why not just write:
Given I log in
When I view a page
Then I should see "abc"
But I should not see "xyz"
or if you wanted to check for multiple strings:
Given I log in
When I view a page
Then I should see the following text:
| abc |
| def |
But I should not see the following text:
| xyz |
| uvw |
You say that in reality there would be many more rows to the table; but of course a table can also have many columns.
Would this not work for you?
Given I log in
When I view a page
Then I should see <goodText>
But I should not see <badText>
Examples:
|goodText| badText |
|abc | xyz |

Resources