简体   繁体   中英

How to structure Cucumber step definitions

I'm learning BDD and I'm using Cucumber to do the features.

My question is: How do I structure the step definition into files?

For instance, imagine the following Scenario:

Scenario 1: Account has sufficient funds
Given the account balance is \$100
And the card is valid
And the machine contains enough money
When the Account Holder requests \$20
Then the ATM should dispense \$20
And the account balance should be \$80
And the card should be returned

How would I translate it into step definitions?

Should I create many step classes AccountSteps , CardSteps , ATMSteps , ATMServiceSteps (to hold methods such as withdraw and getBalance ? Should I split it by feature, or something?

I saw many topics explaining how to create them (which I know now), but how would be the best to structure these steps for scalability and avoid lots of repetition?

Thanks in advance

One way of structuring the steps can be by concept in your domain. Say one step class per concept as you are suggesting.

Personally, I would not start there. I would probably start with one class for steps. I would break that class apart when it got too big. And initially hope that it doesn't get too big.

Keeping all steps in the same class has the benefit that any shared state between steps is easy. Splitting the steps into many classes might mean that you need to solve the problem of sharing state between step classes. This is a solved problem, you use dependency injection to share a common class. But it is more complicated compared not to have to share any steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM