Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Last active August 29, 2015 14:16
Show Gist options
  • Save drmikecrowe/a3f8a31f2c7f2de1d078 to your computer and use it in GitHub Desktop.
Save drmikecrowe/a3f8a31f2c7f2de1d078 to your computer and use it in GitHub Desktop.
Reformat Gherkin test scripts to line up properly

Setup/Configuration

Here's how our tests are setup:

  • Tests are grouped in subdirectories
  • Each directory/filename is prefixed with a number between 100-999, typically incremented by 10. This allows us to insert new tests into the stream of sequential tests
  • Tests start at 100, but configuration setup tests start at 010.
  • Tests are tagged according to the directory, and directory/file

This is very easy to understand/use, especially when you get 1000s of steps/scenarios and complex tests in your setup.

Directory Structure

  • 100-Basic Emulator Tests/
  • /010-Website Create TEST100 Device.feature
  • /100-Emulator Test Device Registers.feature
  • ....
  • 110-Basic Med Configuration/
  • /010-Website Create TEST110 Device.feature
  • ....

What does the script do?

  • Lines up your "I" in a perfect column
  • Each Scenario:, Given, When, Then, And are right-justified to column 15
  • Remaining line is left justified at column 16
  • The Feature: top line is replaced with the directory name (the directory name defines the group of tests)
  • Tags are inserted at the top as follows:
  • @### -- The 3-digit prefix of the directory
  • @###-### -- The 3-digit prefix of the directory plus the 3-digit prefix of the individual test

Example:

@100
@100-010
Feature: 100-Basic Emulator Tests/010-Website Create TEST100 Device
 
      Scenario: Admin verifies we are running in the TEST environment
          Given I am on the / page
           Then I will see -test
...

Running

This is an example for using behave (Python based), but applies to any platform.

  • To run all tests in directory 100:
  • behave --tags=100
  • To run just test 100 in directory 100, run:
  • behave --tags=100-100
  • Say you the first file in directory 100 is a setup function you want to run before your individual test:
  • behave --tags=100-010,140
  • This effectively runs the first file of the 100's (which is your "setup" scenario), then runs all tests in 140. So, you're skipping all the tests between 100-140
  • Now, say you have a specific test in 140 that's failing:
  • behave --tags=140-160
  • Or you want to re-initialize everything before running this one test:
  • behave --tags=100-010,140-160
BEGIN {
match(ARGV[1], /([0-9]{3})(-.*)\/([0-9]{3})(-.*).feature/, parts);
cwd = parts[1] parts[2];
filename = parts[3] parts[4];
print "@" parts[1];
print "@" parts[1] "-" parts[3];
print "Feature: " cwd "/" filename
print " "
}
/^@/ { next; }
/Feature:/ { next; }
{
if ( $1 == "Scenario:" || $1 == "Given" || $1 == "When" || $1 == "Then" || $1 == "And" || $1 == "|") {
if ($1 == "|") {
match($0, $1);
y = substr($0, RSTART+RLENGTH-1);
gsub(/^[ \t]+/, "", y);
x = "";
} else {
match($0, $1);
x = $1;
y = substr($0, RSTART+RLENGTH+1);
}
printf("%15s %s\n", x, y);
started = 1;
} else {
if ( !started ) next;
print $0
}
}
#!/bin/bash
find . -name '*feature' | while read FILE; do
gawk -f cleanup.awk "$FILE" > /tmp/.tmp.save
mv /tmp/.tmp.save "$FILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment