I have 14 features in total. One of them is for cleaning (teardown), so I want that to run in the end.
But when I run my suite, it actually runs in the middle and hence breaks the suite.
How can we run the features in a specific order?
If it's truly a one time execution at the end you could let JUnit take care of this step by adding @AfterClass
The below example shows where you can add your before and after code.
@RunWith(Karate.class)
@CucumberOptions(features = "classpath:features")
public class TestRunner {
@BeforeClass
public static void beforeClass() {
System.out.println("BEFORE");
}
@AfterClass
public static void afterClass() {
System.out.println("AFTER");
}
}
Currently the ordering of feature execution is down to the underlying Cucumber implementation.
Karate currently uses cucumbers MultiLoader to load the features from the file system or classpath. The cucumber version is 1.2.5 as of karate release 0.8.1, and the ordering is determined by the Java ClassLoader.getResources https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResources(java.lang.String)
You're going to have to worry about your directory structure and the naming of files within the folders.
(On a side note, a cleaner way would be for each feature to be completely independent from other features).
If you really want to force the order of feature execution, a "Karate way" would be to execute just one feature, and have this feature call the features you want after each other i.e:
Specify the Runner to only execute your orchestrator feature:
@RunWith(Karate.class)
@CucumberOptions(features = "classpath:features/orchestrator.feature")
public class TestRunner {
@BeforeClass
public static void beforeClass() {
System.out.println("BEFORE");
}
@AfterClass
public static void afterClass() {
System.out.println("AFTER");
}
}
Define the test orchestrator that will call the other features in order:
Feature: Test orchestration feature
Scenario: Run all of the tests in order
* call read('first.feature')
* call read('second.feature')
* call read('third.feature')
Here are sample features that are called - first:
Feature: First feature
Scenario: First
* print "first"
second:
Feature: Second feature
Scenario: Second
* print "Second"
and third:
Feature: Third feature
Scenario: Third
* print "Third"