Skip to content

Instantly share code, notes, and snippets.

@zsoldosp
Created November 10, 2014 13:15
Show Gist options
  • Save zsoldosp/dd779b5e71a3a76f5fbe to your computer and use it in GitHub Desktop.
Save zsoldosp/dd779b5e71a3a76f5fbe to your computer and use it in GitHub Desktop.
Feature: Showing off behave
Scenario: Run a simple test
Given we have behave installed
When we implement 5 tests
Then behave will test them for us!
from behave import given, when, then, step
@given('we have behave installed')
def step_impl(context):
pass
@when('we implement {number:d} tests')
def step_impl(context, number): # -- NOTE: number is converted into integer
assert number > 1 or number == 0
context.tests_count = number
@then('behave will test them for us!')
def step_impl(context):
assert context.failed is False
assert context.tests_count >= 0
from behave.configuration import Configuration
from behave.runner import Runner
from behave import step_registry
import os
import shutil
import tempfile
def get_config():
config = Configuration()
config.paths = [get_copied_features_dir()]
config.format = ['pretty']
return config
def get_copied_features_dir():
src_features_dir = os.path.join(os.path.dirname(__file__), 'features')
actual_features_dir = os.path.join(tempfile.mkdtemp(), 'data')
shutil.copytree(src_features_dir, actual_features_dir)
return actual_features_dir
def get_feature_summary(runner):
assert 1 == len(runner.config.reporters), runner.config.reporters
summary_reporter = runner.config.reporters[0]
return summary_reporter.feature_summary
def main():
first_config = get_config()
first_runner = Runner(first_config)
first_runner.run()
first_feature_summary = get_feature_summary(first_runner)
assert 1 == first_feature_summary['passed'], first_feature_summary
second_config = get_config()
second_runner = Runner(second_config)
try:
second_runner.run()
assert False, 'assumption is that this will fail'
except step_registry.AmbiguousStep as e:
assert "@given('we have behave installed') has already been defined" in str(e)
# the below workaround doesn't work sadly :(
step_registry.registry = step_registry.StepRegistry()
assert step_registry.registry.steps == \
{'given': [], 'when': [], 'then': [], 'step': []}, step_registry.registry.steps
second_runner.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment