Skip to content

Instantly share code, notes, and snippets.

@schuster-rainer
Last active January 28, 2016 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schuster-rainer/8e7bf8d24829df9cff7a to your computer and use it in GitHub Desktop.
Save schuster-rainer/8e7bf8d24829df9cff7a to your computer and use it in GitHub Desktop.
Execute command from behave acceptance test
Feature: Tracing
Scenario Outline: Generating a two column CSV
When I execute: my_fictional_command input1.xml input2.xml --header=COL1;COL2
Then the generated csv with <COL1> should map to <COL2>
Examples: COL1 maps to COL2
| REQ_ID| RELATED_ID |
| COL1| COL2 |
| A | 1 |
| B | 2 |
| C | 3 |
from behave import *
import subprocess
from subprocess import CalledProcessError, PIPE
@when(u'I execute: {command}')
def step_impl(context, command):
params = command.split()
try:
p = subprocess.Popen(params, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
context.stdout_capture.write(out)
context.stdout_capture.write(err)
except CalledProcessError as e:
context.exception = e
@then(u'the generated csv with {col1} should map to {col2}')
def step_impl(context, col1, col2):
# example for a two column csv file
row_expression = re.compile("(.+);(.+)*")
valid = False
current_position = context.stdout_capture.tell()
context.stdout_capture.seek(0)
lines = [line for line in context.stdout_capture]
context.stdout_capture.seek(current_position)
for line in lines:
if line.find(req_id) > -1:
result = row_expression.match(line)
assert result.group(1) == col1
col2_values = result.group(2).split(" ")
assert col2 in col2_values
valid = True
break
assert valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment