1

I have a cucumber feature file 'A' that serves as setting up environment (data clean up and initialization). I want to have it executed before all other feature files can run.

It's it kind of like @before hook as in http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/. However, that does not work because my feature files 'A' contains hundreds of cucumber steps and it is not as simple as:

@Before
public void beforeScenario() {
    tomcat.start();
    tomcat.deploy("munger");
    browser = new FirefoxDriver();
}

instead it's better to be able to run 'A' as a feature file as a whole.

I've searched around but did not find a answer. I am so surprised that no one has this type of requirement before.

The closest i found is 'background'. But that means i can have only one huge feature file with the content of 'A' as 'background' at the top, and rest of my test in the same file. I really do not want to do that.

Any suggestions?

user1559625
  • 2,583
  • 5
  • 37
  • 75
  • If its an initialisation, then put it in the separate method and call that method in setup(). Or for your scenario, you want to have it as a separate feature file only? – Sakshi Singla Dec 20 '16 at 07:12

2 Answers2

0

By default, Cucumber features are run single thread in order by:

  1. Alphabetically by feature file directory
  2. Alphabetically by feature file name within directory

Scenario execution is then by order within the feature file.

So have your initialization feature in the first directory (alhpabetically) with a file name that sorts first (alphabetically) in that directory.

That being said it is generally a bad practice to require an execution order in your feature files. We run our feature files in parallel so order is meaningless. For Jenkins or TeamCity you could add a build step that executes the one feature file followed by a second build step that executes the rest of your feature files.

MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
  • It partially does. However it's not flexible, especially if I need to this 'A' multiple times (i.e. once every time before I run some other verification features) – user1559625 Dec 21 '16 at 04:59
0

I have also a project, where we have a single feature file, that contains a very long scenario called Scenario: Test data with a lot of very long scenarios, like this:

Given the system knows about the following employees
|uuid|user-key|name|nickname|
|1|0101140000|Anna|annie|
... hundreds of lines like this follow ... 

We see this long SystemKnows scenarios as quite valuable, so that our testers, Product Owner and developers have a baseline of what data are in the system. Our domain is quite complex, and we need this baseline of reference data for everyone to be able to understand the tests. (These reference data become almost like well known personas, and are a shared team metaphore)

In the beginning, we were relying on the alphabetic naming convention, to have the AAA.feature to be run first.

Later, we discovered that this setup was brittle, and decided to use the following trick, inspired by the PageObject pattern:

  • Add a background with the single line Given(~'^I set test data for all feature files$')

  • In the step definition, have a factory to create the test data, and make sure inside the factore method, that it is only created once, like testFactory.createTestData()

In this way, you have both the convenience of expressing reference setup as a scenario, that enhances team communication, but you also have a stable test setup.

Hope this is helpful! Agata

themathmagician
  • 467
  • 5
  • 16
  • This is the exact problem I am having now. But how can you write the factory to create test data that you did with a VERY LONG SCENARIO THAT MAY CONTAIN HUNDREDS OF LINES? That long scenario create all data and the reason I raise this stackflow question is to ask how to reuse this long scenario. – user1559625 Dec 21 '16 at 04:01
  • Say this long scenario looks like this: Given I create test data '1'; And I create test data '2' .. Given I create test data '1000'. Each step is like calling a java step function with different '1' '2' ... '1000'. How is it possible to achieve this with a single factory class or method?! – user1559625 Dec 21 '16 at 04:03
  • by using parameterised function?? – Sakshi Singla Dec 21 '16 at 09:35