Specflow - State between “scenarios”

情到浓时终转凉″ 提交于 2019-12-12 14:27:32

问题


Using Specflow, I'm writing a suite of scenarios that model monthly payrolls, validating the calculated payments for each month and finally the yearend figures.

The result of each month is cumulative, so each subsequent scenario is dependent on the previous month’s additions and deductions. The payment calculations are written to a database via a third party tool, so creating and destroying test data between scenarios is expensive.

From my experience with testing, I know it's not always possible to ensure the execution sequence of the tests. I can control the sequence of execution with some scenario naming conventions, but can't guarantee that a remote test runner is going to run tests in alphabetical order.

Options I've considered:

  • Run the entire year through a single scenario including lots of given, when, then assertions. This results in a single huge scenario that is difficult to read.
  • Create a concatenation "Given" for each scenario. "Given: All payments to month X have been made". This creates lots of database traffic as each scenario will need to create and destroy test data.

Is there a better way to store state between scenarios and ensure scenarios are executed in the desired sequence?


回答1:


Relying on the order of execution of the scenarios is an anti-pattern and should be avoided. Test runners usually don't provide any mechanism to control the order of execution for the same reason. It would be also against the concept of executable specification: the scenario should be understandable (and executable) on its own.

In your case the Given part should prepare the data for the calculation in question, the When should calculate and the Then should check the result of that single calculation.

To reduce the execution time maybe you could try to select "important" scenarios in a way that they test different aspects. Probably it is not necessary to test each month 1-11. You could have one test for the first monthly payroll, one for the 2th month, one to close an entire year, one to start a new year, etc.

It is also a common technique that the Given must not necessarily be done the same way as the "real application" would do (from scratch). Sometimes you can do shortcuts in the test to ensure the prerequisites in a faster and easier way. E.g. you could specify the sums of the previous month if that's all your scenario needs to calculate the next month (instead of letting the app calculate everything from scratch). Of course you have to know what you are doing and you have to consider the risk involved with faking some aspects of the application.



来源:https://stackoverflow.com/questions/7158810/specflow-state-between-scenarios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!