-
I have a client that connects to a server over a serial link, i.e. responses are asynchronous and can appear after a delay. I already have working client and server code that uses asyncio. Now I'd like to replace the client with a Cucumber / behave harness, also using asyncio to communicate with the server. Can you show me a minimal behave example, similar to what's shown in https://github.com/behave/behave/blob/main/README.rst, but using behave-asyncio instead? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Have you like at the docs or the features/step.async_steps.feature ? SEE ALSO: |
Beta Was this translation helpful? Give feedback.
-
I saw the set.async_steps.feature file, but as a relative newcomer to advanced features syntax, the it wasn't clear that it was a template for the python code. Staring harder shows that it does in fact have a recipe. However, the "New and Noteworthy" link provides both the context and un-obfuscated code examples I was hoping to find -- thank you! |
Beta Was this translation helpful? Give feedback.
-
The example got me halfway there, but I'm stuck on how to test the return value of an async function. A simple example to demonstrate my ignorance (but I'm sure it's something rather simple): file tstat.py:import asyncio
class Tstat:
def __init__(self):
self._temperature = 0
async def set_temperature(self, t):
self._temperature = t
await asyncio.sleep(1.0) # simulate server call
async def get_temperature(self):
await asyncio.sleep(1.0) # simulate server call
return self._temperature file features/tstat.feature:Feature: testing tstat
Scenario: set and get
Given the temperature is set to 25.0
Then the temperature should be 25.0 file: features/steps/tstat_behavior.pyfrom behave import step
from behave.api.async_step import async_run_until_complete
from tstat import Tstat
import asyncio
t = Tstat()
@given(u'the temperature is set to {temperature}')
@async_run_until_complete
async def step_impl(context, temperature):
await t.set_temperature(temperature)
@then(u'the temperature should be {temperature}')
@async_run_until_complete
async def step_impl(context, temperature):
returned_temperature = t.get_temperature()
assert(returned_temperature == temperature) resultsFeature: testing tstat # features/tstat.feature:1
Scenario: set and get # features/tstat.feature:3
Given the temperature is set to 25.0 # features/steps/tstat_behavior.py:8 1.001s
Then the temperature should be 25.0 # features/steps/tstat_behavior.py:13 0.008s
Traceback (most recent call last):
File "/home/r/.../behave/model.py", line 1329, in run
match.run(runner.context)
File "/home/.../behave/matchers.py", line 98, in run
self.func(context, *args, **kwargs)
File "/home/.../behave/api/async_step.py", line 157, in wrapped_decorator
return step_decorator(astep_func, context, *args, **kwargs)
File "/home/.../behave/api/async_step.py", line 124, in step_decorator
loop.run_until_complete(astep_func(context, *args, **kwargs))
File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "features/steps/tstat_behavior.py", line 18, in step_impl
assert(returned_temperature == temperature)
AssertionError AnalysisI'm pretty sure the problem is in this statement: returned_temperature = t.get_temperature() ... because What is the correct syntax for what I'm trying to accomplish? |
Beta Was this translation helpful? Give feedback.
-
Doh! I figured it out, and as presaged, the solution was rather simple: In @then(u'the temperature should be {temperature}')
@async_run_until_complete
async def step_impl(context, temperature):
returned_temperature = await t.get_temperature()
assert(returned_temperature == temperature) ... or more concisely: @then(u'the temperature should be {temperature}')
@async_run_until_complete
async def step_impl(context, temperature):
assert(await t.get_temperature() == temperature) It now returns this: Feature: testing tstat # features/tstat.feature:1
Scenario: set and get # features/tstat.feature:3
Given the temperature is set to 25.0 # features/steps/_behavior.py:8 1.001s
Then the temperature should be 25.0 # features/steps/tstat_behavior.py:13 1.001s All good! |
Beta Was this translation helpful? Give feedback.
Have you like at the docs or the features/step.async_steps.feature ?
SEE ALSO: