Skip to content

Commit

Permalink
Merge pull request #8
Browse files Browse the repository at this point in the history
1.5
  • Loading branch information
Ypurek authored Feb 12, 2024
2 parents a721a61 + 3c7bbde commit 058447a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,29 @@ Artifacts.

You would need to decide when and where you want to upload your test artifacts to cloud storage

Using pytest fixtures might be a good choice, ex.:
Upload page screenshot when test fails, using fixtures [reference](https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures)

```python
# content of conftest.py
import pytest
from typing import Dict
from pytest import StashKey, CollectReport
from playwright.sync_api import Page

phase_report_key = StashKey[Dict[str, CollectReport]]()

@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
rep = yield
item.stash.setdefault(phase_report_key, {})[rep.when] = rep
return rep


@pytest.fixture(scope="function")
def page(context, request):
page = context.new_page()
def handle_artifacts(page: Page, request):
yield
if request.node.rep_call.failed:
report = request.node.stash[phase_report_key]
if ("call" not in report) or report["setup"].failed or report["call"].failed:
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=8))

filename = f"{random_string}.png"
Expand All @@ -110,7 +125,7 @@ def page(context, request):
artifact_url = pytest.s3_connector.upload_file(screenshot_path, filename)
# or
# artifact_url = pytest.s3_connector.upload_file_object(file_bytes, key, bucket_name)
request.node.testomatio = {"artifacts": [artifact_url]}
request.node.stash["artifact_urls"] = [artifact_url]
page.close()
```

Expand All @@ -121,7 +136,7 @@ If you prefer to use pytest hooks - add `pytest_runtest_makereport` hook in your
```python
def pytest_runtest_makereport(item, call):
artifact_url = pytest.s3_connector.upload_file(screenshot_path, filename)
item.testomatio = {"artifacts": [artifact_url]}
item.stash["artifact_urls"] = [artifact_url]
```

Eny environments used in test run. Should be placed in comma separated list, NO SPACES ALLOWED.
Expand Down Expand Up @@ -168,6 +183,9 @@ def test_example():

## Change log

### 1.5.0 - Fixes artifacts in fixtures lifecycle
- Earlier, artifacts added in pytest fixtures where scipped by analyser

### 1.4.0 - Fixes artifacts and test sync with Testomatio
- Fixes artifacts uploads
- Fixes test id resolution when syncing local test with Testomatio
Expand Down
14 changes: 9 additions & 5 deletions analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def pytest_runtest_makereport(item: Item, call: CallInfo):
'message': None,
'stack': None,
'example': None,
'artifacts': None,
'artifacts': test_item.artifacts,
'steps': None,
'code': None,
}
Expand Down Expand Up @@ -179,8 +179,12 @@ def pytest_runtest_makereport(item: Item, call: CallInfo):
else:
request['example'] = 'object' # to avoid json serialization error

if request['status']:
pytest.analyzer_test_run_config.status_request.append(request)
if item.nodeid not in pytest.analyzer_test_run_config.status_request:
pytest.analyzer_test_run_config.status_request[item.nodeid] = request
else:
for key, value in request.items():
if value is not None:
pytest.analyzer_test_run_config.status_request[item.nodeid][key] = value


def pytest_runtest_logfinish(nodeid, location):
Expand All @@ -189,11 +193,11 @@ def pytest_runtest_logfinish(nodeid, location):
elif not pytest.analyzer_test_run_config.test_run_id:
return

for request in pytest.analyzer_test_run_config.status_request:
for nodeid, request in pytest.analyzer_test_run_config.status_request.items():
if request['status']:
connector = pytest.connector
connector.update_test_status(run_id=pytest.analyzer_test_run_config.test_run_id, **request)
pytest.analyzer_test_run_config.status_request = []
pytest.analyzer_test_run_config.status_request = {}


def pytest_sessionfinish(session: Session, exitstatus: int):
Expand Down
2 changes: 1 addition & 1 deletion analyzer/testItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, item: Item):
# straitforward way, does not work with test packages
# self.source_code = getsource(item.function)
self.class_name = item.cls.__name__ if item.cls else None
self.artifacts = item.testomatio.get('artifacts', []) if hasattr(item, 'testomatio') else []
self.artifacts = item.stash.get("artifact_urls", [])

def to_dict(self) -> dict:
result = dict()
Expand Down
2 changes: 1 addition & 1 deletion analyzer/testRunConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, title: str = None,
self.environment = environment
self.group_title = group_title
self.parallel = parallel
self.status_request = []
self.status_request = {}

def to_dict(self) -> dict:
result = dict()
Expand Down

0 comments on commit 058447a

Please sign in to comment.