Skip to content

Commit

Permalink
Various tweaks can cleanups, example for #497
Browse files Browse the repository at this point in the history
  • Loading branch information
jenisys committed Oct 1, 2016
1 parent 4317f57 commit a2320bf
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 2 deletions.
2 changes: 1 addition & 1 deletion behave.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
default_tags = -@not_implemented -@xfail
show_skipped = false
format = rerun
outfiles = rerun.featureset
outfiles = rerun.txt
logging_level = INFO
# logging_format = LOG.%(levelname)-8s %(name)-10s: %(message)s
# logging_format = LOG.%(levelname)-8s %(asctime)s %(name)-10s: %(message)s
26 changes: 26 additions & 0 deletions examples/env_vars/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
EXAMPLE: Use Environment Variables in Steps
=============================================================================

:RELATED TO: `issue #497`_

This directory provides a simple example how you can use environment variables
in step implementations.

::

# -- USE: -f plain --no-capture (via "behave.ini" defaults)
$ behave
Feature: Test Environment variable concept

Scenario:
USE ENVIRONMENT-VAR: LOGNAME = xxx (variant 1)
When I click on $LOGNAME ... passed
USE ENVIRONMENT-VAR: LOGNAME = xxx (variant 2)
When I use the environment variable $LOGNAME ... passed

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

.. _`issue #497`: https://github.com/behave/behave/issues/497
15 changes: 15 additions & 0 deletions examples/env_vars/behave.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# =============================================================================
# BEHAVE CONFIGURATION
# =============================================================================
# FILE: .behaverc, behave.ini
#
# SEE ALSO:
# * http://packages.python.org/behave/behave.html#configuration-files
# * https://github.com/behave/behave
# * http://pypi.python.org/pypi/behave/
# =============================================================================

[behave]
default_format = plain
stdout_capture = false
show_source = true
12 changes: 12 additions & 0 deletions examples/env_vars/behave_run.output_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Test Environment variable concept

Scenario:
USE ENVIRONMENT-VAR: LOGNAME = xxx (variant 1)
When I click on $LOGNAME ... passed
USE ENVIRONMENT-VAR: LOGNAME = xxx (variant 2)
When I use the environment variable $LOGNAME ... passed

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s
6 changes: 6 additions & 0 deletions examples/env_vars/features/env_var.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: Test Environment variable concept

Scenario:
When I click on $LOGNAME
When I use the environment variable $LOGNAME

38 changes: 38 additions & 0 deletions examples/env_vars/features/steps/env_var_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: UTF-8 -*-
# -- FILE: features/steps/my_steps.py

from __future__ import print_function
from behave import when
import os
import sys

# -- VARIANT 1:
@when(u'I click on ${environment_variable:w}')
def step_impl(context, environment_variable):
env_value = os.environ.get(environment_variable, None)
if env_value is None:
raise LookupError("Environment variable '%s' is undefined" % environment_variable)
print("USE ENVIRONMENT-VAR: %s = %s (variant 1)" % (environment_variable, env_value))


# -- VARIANT 2: Use type converter
from behave import register_type
import parse

@parse.with_pattern(r"\$\w+") # -- ONLY FOR: $WORD
def parse_environment_var(text):
assert text.startswith("$")
env_name = text[1:]
env_value = os.environ.get(env_name, None)
return (env_name, env_value)

register_type(EnvironmentVar=parse_environment_var)

@when(u'I use the environment variable {environment_variable:EnvironmentVar}')
def step_impl(context, environment_variable):
env_name, env_value = environment_variable
if env_value is None:
raise LookupError("Environment variable '%s' is undefined" % env_name)
print("USE ENVIRONMENT-VAR: %s = %s (variant 2)" \
% (env_name, env_value))

12 changes: 12 additions & 0 deletions tests/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This is the new directory root for:

* unit tests
* functional tests (that do not use feature files)

The tests here use the :pypi:`pytest` test framework for testing.

.. note::

Other tests in the collocated "../test/" directory will be eventually
cleaned and converted in (clean) pytests ;-)

2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ setenv =
[testenv:jy27]
basepython= jython
commands=
py.test {posargs:test}
py.test {posargs:test tests}
behave --format=progress {posargs:features}
behave --format=progress {posargs:tools/test-features}
behave --format=progress {posargs:issue.features}
Expand Down

0 comments on commit a2320bf

Please sign in to comment.