GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Add support for coroutine-based concurrency · Issue #720 · allure-framework/allure-python Open Labels task:new featureRequesting new capability or software featureRequesting new capability or software featuretheme:coretheme:pytest DescriptionCurrently, we don`t support running allure-python in an async function (e.g. through asyncio event loop or a similar coroutine execution engine). This is important, because
Coroutines and contextsA coroutine in python is created with a coroutine declaration, i.e. a function with the Without proper means a coroutine paused inside a context manager may leak unintended global state into another coroutine. Basic illustrationSuppose we have the following abstract code: async def f(): with change_global_state(): await long_io_operation() async def g(): use_global_state() If we create two coroutines (one with Below is an example, how both coroutines could be run with import asyncio async def execute_all(): await asyncio.gather(f(), g()) asyncio.run(execute_all()) There are two general scenarios we can face while generating allure report. They are described below. Scenario 1: running multiple tests concurrentlyThis scenario requires a pytest plugin capable of executing async tests concurrently, like pytest-asyncio-cooperative. Unfortunately, this particular plugin currently doesn`t work with allure-pytest, so the remaining content in this section describes more of a theoretical case, as if the plugin actually works. If we assume the plugin works, the test may looks like this (also, see here): import allure import asyncio import pytest @pytest.fixture(scope="session") async def fence(): yield asyncio.Event() @pytest.mark.asyncio_cooperative async def test_with_async_operation(fence): with allure.step("Wait for a fence"): await fence.wait() @pytest.mark.asyncio_cooperative async def test_without_async_operation(fence): with allure.step("Setting a fence"): fence.set() After Scenario 2: running multiple steps concurrently inside one testThis one is easily achieved either with a self-written fixture, or with pytest-asyncio. Given we have pytest-asyncio installed and the following code: import pytest import allure import asyncio @pytest.mark.asyncio async def test_with_concurrent_steps(): fence = asyncio.Event() async def run_with_step(name): with allure.step(name): await fence.wait() async def release_fence(): fence.set() await asyncio.gather( run_with_step("Step 1"), run_with_step("Step 2"), release_fence() ) Note: the steps could've been executed with The following {
"name": "test_with_concurrent_steps",
"status": "passed",
"steps": [{
"name": "Step 1",
"status": "passed",
"steps": [{
"name": "Step 2",
"status": "passed"
}]
}]
}And it is shown in the report like this: The steps are nested one into another instead of being independent as was intended. WorkaroundTo fix the report, change you code so the functions that create steps are executed synchronously: import pytest import allure import asyncio @pytest.mark.asyncio async def test_with_concurrent_steps(): fence = asyncio.Event() async def run_with_step(name): with allure.step(name): await fence.wait() async def release_fence(): fence.set() await release_fence() await run_with_step("Step 1") await run_with_step("Step 2") The downside is slower execution and the need to rewrite the logic in your code. Also, see this test. Thought on possible implementationWe probably should utilize ContextVars, they were introduced for the very this issue (see PEP567). Cons: they require python 3.7. If we decide to move this way, we may either declare async concurrency supported in python 3.7+ only or drop python 3.6 support completely (our latest decision was to focus on 3.7+ while trying to stay 3.6-compatible, if possible). Can`t think of another possible way for now. Reacted by 雷菊 and Max Kravchuk Activity
MetadataMetadataAssigneesLabelstask:new featureRequesting new capability or software featureRequesting new capability or software featuretheme:coretheme:pytest TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |