Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Add reverse and random scenario execution order #1645

Merged
merged 6 commits into from
Jun 8, 2019
Merged

[Core] Add reverse and random scenario execution order #1645

merged 6 commits into from
Jun 8, 2019

Conversation

grasshopper7
Copy link
Contributor

@grasshopper7 grasshopper7 commented May 27, 2019

Summary

Allows scenarios to be executed in random or reverse order. Custom execution orders can also be created.

Details

All scenarios can be run in random order.
java <classpath> cucumber.api.cli.Main --order random -g <glue path> <feature file path>
The number of scenarios to be run in random can also be mentioned.
java <classpath> cucumber.api.cli.Main --order random --count <number> -g <glue> <feature file>

All scenarios can be run in reverse order.
java <classpath> cucumber.api.cli.Main --order reverse -g <glue path> <feature file path>

Custom orders can be created by implementing the PickleOrder interface in the cucumber.runtime.order package.

Motivation and Context

Scenarios can be executed in random order. Aids in finding out if their is any dependency between scenarios.

How Has This Been Tested?

Tests for RuntimeOptions are added in RuntimeOptionsTest for testing options in the command. OrderTypeFactoryTest tests the OrderTypeFactory class. RandomOrderTypeTest tests the RandomOrderType class. No test for ReverseOrderType as it will test the reverse method of Collections class.

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • [x ] New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • [x ] I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@coveralls
Copy link

coveralls commented May 27, 2019

Coverage Status

Coverage increased (+0.006%) to 85.962% when pulling 6c19a59 on grasshopper7:scenario-execution-order into 4a9835f on cucumber:master.

Copy link
Contributor

@mpkorstanje mpkorstanje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heya, looks pretty neat!

I'd say that my major objection is that different concerns are all united in the implementation of OrderType. I think it would be better to separate the CLI parsing from the implementation separate the implementation of --count and --order.

@@ -180,6 +184,10 @@ private void parse(List<String> args) {
parsedJunitOptions.addAll(asList(arg.substring("--junit,".length()).split(",")));
} else if (arg.equals("--wip") || arg.equals("-w")) {
wip = true;
} else if (arg.equals("--order")) {
orderTypeData.put(OrderType.TYPE_NAME, args.remove(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names of the CLI commands are specific to the runtime options parser. It is not necessary for the OrderType to know what it is named on the CLI. It is okay to inline this string here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change it.

@@ -214,6 +222,10 @@ private void parse(List<String> args) {
junitOptions.clear();
junitOptions.addAll(parsedJunitOptions);
}
if(orderTypeData.containsKey(OrderType.TYPE_NAME)) {
orderType = OrderTypeFactory.getOrderType(orderTypeData);
orderType.checkVariableValues(orderTypeData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact syntax of CLI commands is also specific to the runtime options parser. Checking if the input is valid should be done here.

Additionally any time when --count is used it's argument should be a number and should non-negative. Tying the validation to the order type makes this more complex then needed.

I think it might be better to make --count and --order independent variables and simply ignore --count when not relevant.

Copy link
Contributor Author

@grasshopper7 grasshopper7 May 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --count is used only for the RandomOrderType thus the check was specific to that type. That required the test for number etc to be done later.

Idea of making --count applicable for all types, to be used if required, does simplifies things. If so then the orderTypeData map is also redundant and can be removed. Also no need of checkVariableValues method.

Later on if a future OrderType requires an additional variable from CLI it needs to be handled accordingly.


public abstract class OrderType {

public static final String NONE_ORDER_TYPE = "None";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type names should be lower case or case insensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change it.


import gherkin.events.PickleEvent;

public abstract class OrderType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this class can be turned into an interface. The default implementation for orderPickleEvents is only used by none order.


import cucumber.runtime.CucumberException;

public class OrderTypeFactory {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mapping belongs in RuntimeOptions. Additionally I think the different orders, could be turned into an enum that implements/extends the OrderType interface/class.

import cucumber.runtime.CucumberException;
import gherkin.events.PickleEvent;

public class RandomOrderType extends OrderType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an Enum that implements/extends OrderType.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will convert into enum. No need of Factory.

@Override
public List<PickleEvent> orderPickleEvents(List<PickleEvent> pickleEvents) {
Collections.shuffle(pickleEvents);
return pickleEvents.subList(0, ((count > pickleEvents.size() || count == 0) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nifty but way to hard to follow.

I think this class is doing too much. It's both ordering and selecting pickles. It should only order pickles. By making --order and --count independent of each other you can remove this complexity all together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my bad. Though this logic needs to remain, as need to make sure that the count is less or equal to the size of the incoming PickleEvents to avoid exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries!

}
}
}

final List<Future<?>> executingPickles = new ArrayList<>();
for(final PickleEvent pickleEvent : runtimeOptions.getOrderType().orderPickleEvents(filteredEvents)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the order is injected into the Runtime via the builder. This allows us and other implementations to create Runtime instances without passing in CLI based RuntimeOptions.

This will in the long run let us separate the parsing of CLI options from executing pickles.

Copy link
Contributor

@mpkorstanje mpkorstanje May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example:

        List<PickleEvent> allPickles = new ArrayList<>();
        for (CucumberFeature feature : features) {
            allPickles.addAll(feature.getPickles());
        }

        List<PickleEvent> orderedPickles = pickleOrder.order(allPickles);
        List<PickleEvent> filteredPickles = pickleFilter.filter(orderedPickles);

        final List<Future<?>> executingPickles = new ArrayList<>();
        for (PickleEvent filtredPickle : filteredPickles) {
            executingPickles.add(executor.submit(new Runnable() {
                @Override
                public void run() {
                    runnerSupplier.get().runPickle(pickleEvent);
                }
            }));
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To implement the above the existing iteration for the filters will need to be moved to the Filters class. Are you suggesting above that the pickles need to be ordered first and then filtered?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It'd be much easier to implement this with a Java8 Stream but we don't have those at the moment. So I don't mind too much where the iteration ends up.

@grasshopper7
Copy link
Contributor Author

Implemented OrderType as an enumeration implementing OrderPickleEvents interface.

Separated --order and --count CLI options to work independently. The --order logic is handled by OrderType enum. While the --count is handled by the limitPickleEvents method in Filters class.

Updated the test classes accordingly.

@mpkorstanje
Copy link
Contributor

Looks good! I'll need a bit more time to give this a proper once over.

Copy link
Contributor

@mpkorstanje mpkorstanje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, just a few nit picks.

Suppose a test run fails, how would you reproduce this test run? The random order will be different on the next execution. Perhaps we can have the RANDOM option print out the seed use to shuffle the pickles? Then you could read it back again using --order random:<seed>.

If you can move the last bit of option parsing into RuntimeOptions we don't need to add that now though. It can be added later.


public Filters(FilterOptions filterOPtions) {
public Filters(FilterOptions filterOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers.


public Runtime(final Plugins plugins,
final RuntimeOptions runtimeOptions,
final EventBus bus,
final Filters filters,
final RunnerSupplier runnerSupplier,
final FeatureSupplier featureSupplier,
final ExecutorService executor) {
final ExecutorService executor,
final OrderType orderType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use OrderPickleEvents here?


public static OrderType getOrderType(String name) {
for (OrderType orderType : OrderType.values()) {
if (name.equalsIgnoreCase(orderType.name())) {
Copy link
Contributor

@mpkorstanje mpkorstanje Jun 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we parse the different order names in runtime options too. This would avoid having to support both lower and uppercase options in future versions.

@mpkorstanje mpkorstanje changed the title Custom scenario execution order [Core] Add reverse and random scenario execution order Jun 8, 2019
@mpkorstanje mpkorstanje merged commit b89e538 into cucumber:master Jun 8, 2019
@lock
Copy link

lock bot commented Jun 24, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants