# Gherkin Syntax and Unit Specification

I am wondering about the Gherkin syntax for some scenarios. Suppose I have following events A, B{1}, B{2}, C, D1, D2, G. Where uppercase{number} events like B1, D2 are parallel events(simultaneous). Where uppercase events like A, G are normal events. The symbol `||` means OR but `&&` means AND.

Now how I could write Unit specifications for the following scenarios.

`````` 1. Arrange Event: P Act Event: Q Assert Event: C
2. Arrange Event: P Act Event: B1, B2, B3 Assert Event: C && D
3. Arrange Event: P || X || Y Act Event: B1, B2, B3 Assert Event: C || D
4. Arrange Event: P || X && Y Act Event: B1, B2, C Assert Event: D {C only happens after B1 and B2}
5. Arrange Event: P && X Act Event: B, C||D Assert Event: E {Either C or D have to happen one after another}
6. Arrange Event: P Act Event: B, C&&D Assert Event: E {{Both C and D have to happen one after another}
7. Arrange Event: P Act Event: B1||B2, C1&&C2, E Assert Event: F {Either of the event B1 or B2 happens simultaneously, afterward both C1 and C2 have to happen simulatenously}
8. Arrange Event: P Act Event: B1||B2, C&&D, E Assert Event: F {Either of the event B1 or B2 happens simultaneously, afterward, both C and D have to happen one after another}
``````

By itself Gherkin has a concept of steps, but no concept of parallelism. When writing your scenarios, you could therefore either create a step that applies multiple simultaneous events, or choose a particular order for the events. You can use placeholders in your scenario to run the same scenario with multiple sets of events.

For example a description of this test:

• Arrange Event: P || X || Y
• Act Event: B1, B2, B3
• Assert Event: C || D

could be:

``````Scenario Outline: some meaningful name
Given that <initial_event> has happened
When I trigger event B1
And I trigger event B2
And I trigger event B3
Then event C or D happens

Examples:
| initial_event |
| P             |
| X             |
| Y             |
``````

Of course, this gets more difficult if you have multiple independent event placeholders and you want to test all combinations. Gherkin requires that you write out all combinations by hand in the examples table.

In that case, Gherkin-style “specification by example” may no longer be a good fit for your needs. Instead, it might be better to create a test in code that exercises all possible combinations.

It also seems that your tests seem very much like rule definitions for a rule engine, and not really like tests. In particular, an example cannot really have a choice of inputs – instead the example defines the inputs!

This could also indicate that these examples are not capturing the requirements correctly, in particular that you are describing these requirements on a too low level. What is it about events P, X, Y that any of these might be a suitable precondition for this scenario? Do they put a system into a particular state? Are they special cases of some more general aspect of the system? This is very domain-specific so using single-letter symbols is not helpful.

• +1. Gherkin is best used for higher level, user acceptance style tests. The OP would be better served by unit tests or just coded tests. – Greg Burghardt Jan 31 '19 at 16:08
• Does gherkin has OR keyword ? – Roshan Mehta Feb 1 '19 at 8:06
• @RoshanMehta No, Gherkin has no OR keyword. Steps are just executed one after another. All steps must succeed in order for the scenario to pass. As explained in my answer, you can use data tables to list multiple examples, and you can define steps that try multiple alternatives. – amon Feb 1 '19 at 8:17
• I see you have written then event C OR D happens. Is it okay to write such statements? – Roshan Mehta Feb 1 '19 at 9:07
• @RoshanMehta `And` is just the Gherkin keyword for another step. You can also choose a different order of events. But you do need to pick an order in order to write an example. If you can't do that, maybe Gherkin is not a good fit for your tests. Then just write the tests in code. – amon Feb 1 '19 at 20:00