Creating Rowtests with SpecFlow - specflow

I am trying to create row tests using SpecFlow and the Microsoft built-in Test Framework, something along these lines:
Scenario Outline: Test Calculator
Given I have entered <x> into the calculator
And I have entered <y> into the calculator
When I press add
Then the result should be <result> on the screen
Examples:
| x | y | result|
| 1 | 2 | 3|
| 2 | 2 | 4|
The problem I am facing is that given any step in the Scenario Outline a separate step method is auto-generated for each value from the Examples table. I would like to be able to implement for each step a generic method receiving input values as parameters but it just does not seem to work.

In the end it looks like it works as expected, what I was missing were quotes around input parameters placeholders:
Scenario Outline: Test Calculator
Given I have entered "<x>" into the calculator
And I have entered "<y>" into the calculator
When I press add
Then the result should be "<result>" on the screen
Examples:
| x | y | result|
| 1 | 2 | 3|
| 2 | 2 | 4|

I had this same problem in VS 2012. I think it may be a bug with SpecFlow, because when I change the Scenario Outline to only be a Scenario, it generates everything correctly. All the documentation says you should not have to surround the placeholders in quotes.
In short, my solution is to change it to a Scenario to generate the steps. But don't forget, you have to change it back to a Scenario Outline to compile. This is what is working for me.

Related

What is the proper way to "store" data with PowerShell

I have to handle quite a lot of results and store them. I'd like to create something like a tab which I could in the end export as a CSV.
I was using a PSObject with quite a lot of members. I wonder if there is a better way to handle this? If you can show me how to create it and input data it would be nice.
I should create a tab witch look like this:
Site 1 | Tester | result Test 1 | Result test 2 |.... |Result test x
Sitename1 | tester1| Fail | Succes | fail| etc...
Sitename1 | tester1| Succes | Fail | fail| etc...
PS: It has to be working with PowerShell v2.0.

Repeating steps with different values in BDD test case

I am new to BDD specflow.
I have to write a scenario wherein after I capture an image, i have to select a value for each defined attribute for that image from a selection list
For Eg:
|Body Part |Location |Group |
| Leg | Left | Skin |
| Hand | Upper | Burn |
| Arm | Right | Ulcer |
I need a way in which i can select a different value for each attribute, every time.
Thanks in advance!
You are looking for Scenario Outline;
Scenario outlines allow us to more concisely express these examples through the use of a template with placeholders, using Scenario Outline, Examples with tables and < > delimited parameters.
Specflow takes each line in the Example table and create from the line a scenario to execute.

Is it possible to parameterise the NUnit test case display name when using ``Ticked method names``?

I am testing out F# and using NUnit as my test library; I have discovered the use of double-back ticks to allow arbitrary method naming to make my method names even more human readable.
I was wondering, whether rightly or wrongly, if it is possible to parameterise the method names when using NUnit's TestCaseAttribute to change the method name, for example:
[<TestCase("1", 1)>]
[<TestCase("2", 2)>]
let ``Should return #expected when "#input" is supplied`` input expected =
...
This might not be exactly what you need, but if you want to go beyond unit testing, then TickSpec (a BDD framework using F#) has a nice feature where it lets you write parameterized scenarios based on back-tick methods that contain regular expressions as place holders.
For example, in Phil Trelford's blog post, he uses this to define tic-tac-toe scenario:
Scenario: Winning positions
Given a board layout:
| 1 | 2 | 3 |
| O | O | X |
| O | | |
| X | | X |
When a player marks X at <row> <col>
Then X wins
Examples:
| row | col |
| middle | right |
| middle | middle |
| bottom | middle |
The method that implements the When clause of the scenario is defined in F# using something like this:
let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)``
(mark:string,row:Row,col:Col) =
let y = int row
let x = int col
Debug.Assert(System.String.IsNullOrEmpty(layout.[y].[x]))
layout.[y].[x] <- mark
This is a neat thing, but it might be an overkill if you just want to write a simple parameterized unit test - BDD is useful if you want to produce human readable specifications of different scenarios (and there are actually other people reading them!)
This is not possible.
The basic issue is that for every input and expected you need to create a unique function. You would then need to pick the correct function to call (or your stacktrace wouldn't make sense). As a result this is not possible.
Having said that if you hacked around with something like eval (which must exist inside fsi), it might be possible to create something like this, but it would be very slow.

Scenario Outline: Placeholders with a restricted number of possible values

I am relatively new to BDD and I have a question regarding scenario outlines. When looking at samples over the internet I have the feeling that the placeholders can take any values. The number of elements in their domain is not restricted. Here is one example:
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
The placeholder <start> for example can be any number so the number of values is infinite.
In my specs I have to deal with contracts which can have one of four states (planned, ongoing, paused, and closed). My specs say that I can edit planned contracts but I am not allowed to edit contracts which have one of the remaining three states.
I think I would write a scenario named "Updating a planned contract" and one scenario outline where the status of a contract is a placeholder.
Scenario: Update a planned contract
Given the list of contracts as follows
| name | status | some value |
| c1 | planned | 123 |
And I have edited contract c1 as follows
| field | value |
| name | c1 |
| some value | 456 |
When I save contract c1
Then the list of contracts should be as follows
| name | status | some value |
| c1 | planned | 456 |
Scenario Outline: Update contract
Given there is a <status> contract
And I have edited that contract
When I save that contract
Then I an error 'only planned contracts are allowed to change' should be displayed
Examples:
| status |
| ongoing |
| paused |
| closed |
Is that the right way? One expicit scenario and one parameterized? Or should I write the scenario outline as explicit scenarios for each possibility? I am not sure because the status of a contract is restricted by four possible values as opposed to the examples on the internet.
One thing I find that helps is to remember that Gherkin is just a syntax for Specification by Example. You are trying to provide the examples that make most sense to you in the business domains language.
As such, what you are proposing is perfectly valid. You have one example of a scenario that uses tables to define what happens when a planned contract is edited, and another set of examples that produce errors when contracts in other states. You could also do it explicitly by expanding the outline for each state. Both are valid, and you can always refactor your feature sepcifications as you would the codebase.
What you are aiming to do here however is to provide a grammar, a framework, a language, call it what you will, that you can use to have conversations with your business analysts. You want to be able to pull out this document and say "This is how the system works now, how do we change this to make it support your new feature?".
Personally, I'm avoiding tabular and outline forms in my features right now as I want to make it look as friendly as possible to all I show it to, and as yet, my features are still easy to describe.

Parse issues when trying to use "Examples" section in Cucumber feature

No luck in googling on this error message
features/manage_hand_evaluator.feature: Parse error at features/manage_hand_evaluator.feature:21. Found examples when expecting one of: comment, py_string, row, scenario, scenario_outline, step, tag. (Current state: step). (Gherkin::Parser::ParseError)
Here's the setup I have for Examples section (there are no other Scenarios at this time, just this one after the "Feature:" section)
...
Scenario: Evaluating for current straights
Given I am a player with <hand>
When the board is <board>
Then the current possible straights should be <possibles>
Examples:
| board | hand | possibles |
| A23 | 45 | A2345 |
| 3456 | 23 | A2345,23456,34567,45678 |
| 789T | A2 | 56789,6789T,789TJ,89TJQ |
| 45678 | 23 | 23456,34567,45678,56789,6789T |
I also have step definitions set up already for those "Given, When, Then" lines (and tests passes fine when I replace , , with some text and comment out the "Examples" section). So it seems step definitions are set up properly, just that there is some kind of parsing issue with the contents I have in .feature file and I cannot figure out what I am doing wrong.
Relevant gems installed:
Gherkin (2.1.5) (tried 2.2.0 but it breaks with my version of Cucumber)
Cucumber (0.8.5)
Cucumber-Rails (0.3.2)
Rails (2.3.8)
Replace
Scenario:
with
Scenario Outline:
When you have tests that extends to having examples, you should always use Scenario Outline. Scenario is for those kind of tests with not more than one data to be inputted to test.

Resources