Debian Codegraph @

tests/api/_test_async_step35.py

raw
1 # -*- coding: UTF-8 -*-
2 """
3 Unit tests for :mod:`behave.api.async_test` for Python 3.5 (or newer).
4 """
5
6 # -- IMPORTS:
7 from __future__ import absolute_import, print_function
8 import sys
9 from behave._stepimport import use_step_import_modules
10 from behave.runner import Context, Runner
11 import pytest
12
13 from .testing_support import StopWatch, SimpleStepContainer
14 from .testing_support_async import AsyncStepTheory
15
16
17 # -----------------------------------------------------------------------------
18 # SUPPORT:
19 # -----------------------------------------------------------------------------
20
21
22 # -----------------------------------------------------------------------------
23 # ASYNC STEP EXAMPLES:
24 # -----------------------------------------------------------------------------
25 # if python_version >= 3.5:
26 # @step('an async coroutine step waits "{duration:f}" seconds')
27 # @async_run_until_complete
28 # async def step_async_step_waits_seconds(context, duration):
29 # print("async_step: Should sleep for %.3f seconds" % duration)
30 # await asyncio.sleep(duration)
31 #
32 # if python_version >= 3.4:
33 # @step('a tagged-coroutine async step waits "{duration:f}" seconds')
34 # @async_run_until_complete
35 # @asyncio.coroutine
36 # def step_async_step_waits_seconds2(context, duration):
37 # print("async_step2: Should sleep for %.3f seconds" % duration)
38 # yield from asyncio.sleep(duration)
39 #
40 # -----------------------------------------------------------------------------
41 # TEST MARKERS:
42 # -----------------------------------------------------------------------------
43 # xfail = pytest.mark.xfail
44 python_version = float("%s.%s" % sys.version_info[:2])
45 py35_or_newer = pytest.mark.skipif(python_version < 3.5, reason="Needs Python >= 3.5")
46
47 # -----------------------------------------------------------------------------
48 # TESTSUITE:
49 # -----------------------------------------------------------------------------
50 @py35_or_newer
51 class TestAsyncStepDecorator35(object):
52
53 def test_step_decorator_async_run_until_complete1(self):
54 step_container = SimpleStepContainer()
55 with use_step_import_modules(step_container):
56 # -- STEP-DEFINITIONS EXAMPLE (as MODULE SNIPPET):
57 # VARIANT 1: Use async def step_impl()
58 from behave import step
59 from behave.api.async_step import async_run_until_complete
60 import asyncio
61
62 @step('an async coroutine step waits "{duration:f}" seconds')
63 @async_run_until_complete
64 async def step_async_step_waits_seconds(context, duration):
65 await asyncio.sleep(duration)
66
67 # -- USES: async def step_impl(...) as async-step (coroutine)
68 AsyncStepTheory.validate(step_async_step_waits_seconds)
69
70 # -- RUN ASYNC-STEP: Verify that it is behaving correctly.
71 # ENSURE: Execution of async-step matches expected duration.
72 context = Context(runner=Runner(config={}))
73 with StopWatch() as stop_watch:
74 step_async_step_waits_seconds(context, 0.2)
75 assert abs(stop_watch.duration - 0.2) <= 0.05