Skip to content

Commit

Permalink
Use runtime constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jenisys committed Jul 13, 2019
1 parent 9a438eb commit 29ad299
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/async_step/features/async_dispatch.feature
@@ -1,6 +1,8 @@
@use.with_python.version=3.4
@use.with_python.version=3.5
@use.with_python.version=3.6
@use.with_python.version=3.7
@use.with_python.version=3.8
Feature:
Scenario:
Given I dispatch an async-call with param "Alice"
Expand Down
2 changes: 2 additions & 0 deletions examples/async_step/features/async_run.feature
@@ -1,6 +1,8 @@
@use.with_python.version=3.4
@use.with_python.version=3.5
@use.with_python.version=3.6
@use.with_python.version=3.7
@use.with_python.version=3.8
Feature:
Scenario:
Given an async-step waits 0.3 seconds
13 changes: 13 additions & 0 deletions examples/async_step/features/environment.py
@@ -1,8 +1,18 @@
# -*- coding: UTF-8 -*-

from behave.tag_matcher import ActiveTagMatcher, setup_active_tag_values
from behave.api.runtime_constraint import require_min_python_version
import sys

# -----------------------------------------------------------------------------
# REQUIRE: python >= 3.4
# -----------------------------------------------------------------------------
require_min_python_version("3.4")


# -----------------------------------------------------------------------------
# SUPPORT: Active-tags
# -----------------------------------------------------------------------------
# -- MATCHES ANY TAGS: @use.with_{category}={value}
# NOTE: active_tag_value_provider provides category values for active tags.
python_version = "%s.%s" % sys.version_info[:2]
Expand All @@ -11,17 +21,20 @@
}
active_tag_matcher = ActiveTagMatcher(active_tag_value_provider)


# -----------------------------------------------------------------------------
# HOOKS:
# -----------------------------------------------------------------------------
def before_all(context):
# -- SETUP ACTIVE-TAG MATCHER (with userdata):
setup_active_tag_values(active_tag_value_provider, context.config.userdata)


def before_feature(context, feature):
if active_tag_matcher.should_exclude_with(feature.tags):
feature.skip(reason=active_tag_matcher.exclude_reason)


def before_scenario(context, scenario):
if active_tag_matcher.should_exclude_with(scenario.effective_tags):
scenario.skip(reason=active_tag_matcher.exclude_reason)
Expand Down
@@ -1,8 +1,12 @@
# -- REQUIRES: Python >= 3.4
# -- REQUIRES: Python >= 3.4 and Python < 3.8
# HINT: Decorator @asyncio.coroutine is prohibited in python 3.8
# USE: Async generator/coroutine instead.

from behave import step
from behave.api.async_step import async_run_until_complete
import asyncio

# -- USABLE FOR: "3.4" <= python_version < "3.8"
@step('an async-step waits {duration:f} seconds')
@async_run_until_complete
@asyncio.coroutine
Expand Down
@@ -1,10 +1,11 @@
# -- REQUIRES: Python >= 3.5

from behave import step
from behave.api.async_step import async_run_until_complete
import asyncio

@step('an async-step waits {duration:f} seconds')
@async_run_until_complete
async def step_async_step_waits_seconds_py35(context, duration):
"""Simple example of a coroutine as async-step (in Python 3.5)"""
"""Simple example of a coroutine as async-step (in Python 3.5 or newer)"""
await asyncio.sleep(duration)
29 changes: 23 additions & 6 deletions examples/async_step/features/steps/async_dispatch_steps.py
@@ -1,21 +1,38 @@
# -*- coding: UTF-8 -*-
# REQUIRES: Python >= 3.5
# REQUIRES: Python >= 3.4/3.5
import sys
from behave import given, then, step
from behave.api.async_step import use_or_create_async_context, AsyncContext
from behave.api.async_step import use_or_create_async_context
from hamcrest import assert_that, equal_to, empty
import asyncio

@asyncio.coroutine
def async_func(param):
yield from asyncio.sleep(0.2)
return str(param).upper()

# ---------------------------------------------------------------------------
# ASYNC EXAMPLE FUNCTION:
# ---------------------------------------------------------------------------
python_version = "%s.%s" % sys.version_info[:2]
if python_version >= "3.5":
async def async_func(param):
await asyncio.sleep(0.2)
return str(param).upper()
else:
# -- HINT: Decorator @asyncio.coroutine is prohibited in python 3.8
@asyncio.coroutine
def async_func(param):
yield from asyncio.sleep(0.2)
return str(param).upper()


# ---------------------------------------------------------------------------
# STEPS:
# ---------------------------------------------------------------------------
@given('I dispatch an async-call with param "{param}"')
def step_dispatch_async_call(context, param):
async_context = use_or_create_async_context(context, "async_context1")
task = async_context.loop.create_task(async_func(param))
async_context.tasks.append(task)


@then('the collected result of the async-calls is "{expected}"')
def step_collected_async_call_result_is(context, expected):
async_context = context.async_context1
Expand Down
12 changes: 12 additions & 0 deletions examples/async_step/features/steps/async_steps.py
@@ -0,0 +1,12 @@
# -*- coding: UTF-8 -*-
# REQUIRES: Python >= 3.4/3.5
"""Python import-barrier for python2 or python < 3.4."""

from __future__ import absolute_import
import sys

python_version = "%s.%s" % sys.version_info[:2]
if python_version >= "3.5":
import _async_steps35
elif python_version == "3.4":
import _async_steps34

0 comments on commit 29ad299

Please sign in to comment.