From e5c4bb527d320d5154c30c3d82ddae71fd42a554 Mon Sep 17 00:00:00 2001 From: Juan Madurga Date: Fri, 10 Mar 2023 10:22:52 +0100 Subject: [PATCH] don't send report from xdist workers --- pytest_tinybird/tinybird.py | 5 ++++- requirements-dev.txt | 1 + tests/test_tinybird.py | 7 ++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pytest_tinybird/tinybird.py b/pytest_tinybird/tinybird.py index cc1e7af..1c14ce7 100644 --- a/pytest_tinybird/tinybird.py +++ b/pytest_tinybird/tinybird.py @@ -35,6 +35,9 @@ def report(self, session: Session): terminalreporter: TerminalReporter = session.config.pluginmanager.get_plugin( "terminalreporter" ) + # special check for pytest-xdist plugin, we do not want to send report for each worker. + if hasattr(terminalreporter.config, 'workerinput'): + return report = [] for k in terminalreporter.stats: for test in terminalreporter.stats[k]: @@ -60,7 +63,7 @@ def report(self, session: Session): data=data, timeout=REQUEST_TIMEOUT) if response.status_code != 202: - log.error("Error while uploading to tinybird") + log.error("Error while uploading to tinybird %s", response.status_code) @pytest.hookimpl(trylast=True) def pytest_sessionfinish(self, session: Session, exitstatus: Union[int, ExitCode]): diff --git a/requirements-dev.txt b/requirements-dev.txt index 8e403f8..59e98c2 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,5 +2,6 @@ flake8==6.0.0 pylint==2.16.2 pytest==7.2.1 requests>=2.28.1 +pytest-xdist==2.5.0 tox==4.2.8 diff --git a/tests/test_tinybird.py b/tests/test_tinybird.py index 3a44e23..380b106 100644 --- a/tests/test_tinybird.py +++ b/tests/test_tinybird.py @@ -7,16 +7,21 @@ def test_report_to_tinybird(testdir): # create a temporary pytest test module testdir.makepyfile(""" import pytest + from random import randint def test_passed(): assert True - """) + def test_flaky(): + assert randint(1,10) >=5 + """) tinybird_url = os.environ["TINYBIRD_URL"] = 'https://fake-api.tinybird.co' datasource_name = os.environ["TINYBIRD_DATASOURCE"] = "test_datasource" tinybird_token = os.environ["TINYBIRD_TOKEN"] = 'test_token' with mock.patch('requests.post') as mock_post: + mock_post.return_value.status_code = 202 testdir.runpytest( + "-n 2", '--report-to-tinybird', '-vvv' )