Tuesday, February 10, 2015

Introducing Pepper

Pepper is a purely Java-based tool I created for automating User Acceptance Tests. I created Pepper because I felt companies could benefit from having a tool that non-technical people could utilize when specifying the features that they want their application to have. Unfortunately the Java frameworks that had this ability - primarily JBehave and Cucumber-JVM - are, in my opinion, a little bloated. I also didn't like the amount of setup code JBehave requires in order to run a simple feature file. If you'll recall from my post "Getting Started With JBehave" you needed to create a subclass similar to the following.
package features.addition;
 
import static org.jbehave.core.reporters.Format.CONSOLE;
 
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryLoader;
import org.jbehave.core.junit.JUnitStory;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
 
public class AdditionStory extends JUnitStory {
 @Override
 public Configuration configuration( ) {
  Configuration configuration = new MostUsefulConfiguration( );
 
  // Where to find the stories
  StoryLoader storyLoader;
  storyLoader = new LoadFromClasspath(this.getClass( ));
 
  configuration.useStoryLoader(storyLoader);
 
  StoryReporterBuilder storyReporterBuilder;
  storyReporterBuilder = new StoryReporterBuilder( );
 
  storyReporterBuilder.withDefaultFormats( );
  storyReporterBuilder.withFormats(CONSOLE);
 
  // CONSOLE reporting
  configuration.useStoryReporterBuilder(storyReporterBuilder);
 
  return configuration;
 }
 
 @Override
 public InjectableStepsFactory stepsFactory( ) {
  return new InstanceStepsFactory(configuration( ), new AdditionSteps( ));
 }
}
I picked the name Pepper for several reasons. 1) It's short and easy to remember. 2) Peppers, like Cucumbers, are a fruit. 3) Peppers come in many different colors including red, green, and yellow which are the standard colors for failing, passing, and pending tests.

I created Pepper using the Test-Driven Development methodolgy. Whenever I wanted Pepper to have a new feature I created a failing test for it first.

Note: Pepper is still in the early stages of development.

To use Pepper you need to create a features folder that will hold all of your feature files. You can place this folder anywhere in your project directory. You'll also need to create a subclass of StepDefinition.

Suppose you had the following feature file.
Scenario:
  Given I want to do Addition
  And my first operand is 6
  And my second operand is 2
  When I execute the Operation
  Then I should get 8 as my result
If this file had been saved as addition.feature, Pepper will look for the StepDefinition subclass.
import org.pepper.core.StepDefinition;

public class Addition extends StepDefinition {
}
Likewise, if the file had been saved as calculator_addition.feature, Pepper will look for the StepDefinition subclass.
import org.pepper.core.StepDefinition;

public class CalculatorAddition extends StepDefinition {
}
In other words, the name of the feature file is the same as the StepDefinition subclass only snake cased. There is a way to change this, though. Suppose your StepDefinition subclass was named FooBar but your feature file was named addition.feature. Here's how that would look:
import org.pepper.core.StepDefinition;

public class FooBar extends StepDefinition {
  public FooBar() {
    setFeatureName("addition");
  }
}
You can name the subclass FooBarStepDefinition if you like, if it helps as a visual reminder that the class holds the step definitions for a feature file. Pepper will ignore the StepDefinition part when determing the name of the feature file.

You can even change the extension that's used. That way you don't have to associate the .feature extension with a text editor if you don't want to. Let's say the feature file was named foo_bar.txt Here's how that would look:
import org.pepper.core.StepDefinition;

public class FooBar extends StepDefinition {
  public FooBar() {
    setFeatureExtension(".txt");
  }
}
Notice how much simpler Pepper is to JBehave. All the necessary configuration is handled by the StepDefinition class. To execute the test all you need to do is run the StepDefinition through JUnit.

No comments:

Post a Comment