Skip to content

Commit

Permalink
Merge pull request #9
Browse files Browse the repository at this point in the history
1.6
  • Loading branch information
Ypurek authored Feb 21, 2024
2 parents 5bb32f0 + 73bc20d commit cd47c01
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,24 @@ to configure test environment, you can use additional option:
pytest --analyzer sync --testRunEnv windows11,chrome,1920x1080
```

Eny environments used in test run should be placed in comma separated list, NO SPACES ALLOWED.


### Submitting Test Artifacts

Testomat.io does not store any screenshots,logs or other artifacts.
Testomat.io does not store any screenshots, logs or other artifacts.

In order to manage them it is advised to use S3 Buckets (GCP Storage).
https://docs.testomat.io/usage/test-artifacts/

In order for analyser to have access to your cloud bucket - enable **Share credentials with Testomat.io Reporter** option in testomat.io Settings ->
Artifacts.
Analyser needs to be aware of the cloud storage credentials.
There are two options:
1. Enable **Share credentials with Testomat.io Reporter** option in testomat.io Settings -> Artifacts.
2. Use environment variables `ACCESS_KEY_ID, SECRET_ACCESS_KEY, ENDPOINT, BUCKET`

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

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)
1) 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
Expand Down Expand Up @@ -122,25 +127,23 @@ def handle_artifacts(page: Page, request):
# file_bytes - required, bytes of the file to be uploaded
# key - required, file name in the s3 bucket
# bucket_name - optional,name of the bucket to upload file to. Default value is taken from Testomatio.io
artifact_url = pytest.s3_connector.upload_file(screenshot_path, filename)
artifact_url = pytest.testomatio.upload_file(screenshot_path, filename)
# or
# artifact_url = pytest.s3_connector.upload_file_object(file_bytes, key, bucket_name)
request.node.stash["artifact_urls"] = [artifact_url]
# artifact_url = pytest.testomatio.upload_file_object(file_bytes, key, bucket_name)
pytest.testomatio.add_artifacts([artifact_url])
page.close()
```

⚠️ Please take into account s3_connector available only after **pytest_collection_modifyitems()** hook is executed.

If you prefer to use pytest hooks - add `pytest_runtest_makereport` hook in your `conftest.py` file.
2) If you prefer to use pytest hooks - add `pytest_runtest_makereport` hook in your `conftest.py` file.

```python
def pytest_runtest_makereport(item, call):
artifact_url = pytest.s3_connector.upload_file(screenshot_path, filename)
item.stash["artifact_urls"] = [artifact_url]
artifact_url = pytest.testomatio.upload_file(screenshot_path, filename)
pytest.testomatio.add_artifacts([artifact_url])
```

Eny environments used in test run. Should be placed in comma separated list, NO SPACES ALLOWED.

### Clarifications

- tests can be synced even without `@patest.mark.testomatio('@T96c700e6')` decorator.
Expand Down Expand Up @@ -183,6 +186,12 @@ def test_example():

## Change log

### 1.6 - Fixes nested suites
- Testomaito not longer supports nested test suites. Suites could be only in a folder.
- Add helped to attach test artifacts
- Expose environment variables to provide access to cloud storage
- Update readme

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

Expand Down
21 changes: 13 additions & 8 deletions analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .testRunConfig import TestRunConfig
from .testItem import TestItem
from .s3_connector import S3Connector
from .testomatio import Testomatio
from .helper import add_and_enrich_tests, get_test_mapping, get_functions_source_by_name, collect_tests
import logging
import os
Expand Down Expand Up @@ -61,7 +62,9 @@ def pytest_configure(config: Config):
)

pytest.analyzer_test_run_config = TestRunConfig(group_title=os.environ.get('TESTOMATIO_RUNGROUP_TITLE'))
pytest.s3_connector = None

pytest.testomatio = Testomatio()
pytest.s3_connector = pytest.testomatio.s3_connector # backward compatibility

if config.getoption(analyzer_option) in ('add', 'remove', 'sync'):
url = config.getini('testomatio_url')
Expand Down Expand Up @@ -108,15 +111,17 @@ def pytest_collection_modifyitems(session: Session, config: Config, items: list[
pytest.analyzer_test_run_config.test_run_id = run_details['uid']

if run_details.get('artifacts'):
s3_access_key = run_details['artifacts'].get('ACCESS_KEY_ID')
s3_secret_key = run_details['artifacts'].get('SECRET_ACCESS_KEY')
s3_endpoint = run_details['artifacts'].get('ENDPOINT')
s3_bucket = run_details['artifacts'].get('BUCKET')
s3_access_key = os.environ.get('ACCESS_KEY_ID') or run_details['artifacts'].get('ACCESS_KEY_ID')
s3_secret_key = os.environ.get('SECRET_ACCESS_KEY') or run_details['artifacts'].get('SECRET_ACCESS_KEY')
s3_endpoint = os.environ.get('ENDPOINT') or run_details['artifacts'].get('ENDPOINT')
s3_bucket = os.environ.get('BUCKET') or run_details['artifacts'].get('BUCKET')
if all((s3_access_key, s3_secret_key, s3_endpoint, s3_bucket)):
pytest.s3_connector = S3Connector(s3_access_key, s3_secret_key, s3_endpoint, s3_bucket)
pytest.s3_connector.login()
pytest.testomatio.set_s3_connector(S3Connector(s3_access_key, s3_secret_key, s3_endpoint, s3_bucket))
pytest.testomatio.s3_connector.login()
pytest.s3_connector = pytest.testomatio.s3_connector # backward compatibility
else:
pytest.s3_connector = S3Connector('', '', '', '')
pytest.testomatio.set_s3_connector(S3Connector('', '', '', ''))
pytest.s3_connector = pytest.testomatio.s3_connector # backward compatibility
case 'debug':
with open(metadata_file, 'w') as file:
data = json.dumps([i.to_dict() for i in meta], indent=4)
Expand Down
5 changes: 2 additions & 3 deletions analyzer/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def load_tests(
create: bool = False
):
request = {
"framework": "",
"language": "",
"framework": "pytest",
"language": "python",
"noempty": no_empty,
"no-detach": no_detach,
"structure": structure if not no_empty else False,
Expand All @@ -37,7 +37,6 @@ def load_tests(
request['tests'].append({
"name": test.sync_title,
"suites": [
test.file_name,
test.class_name
],
"code": test.source_code,
Expand Down
21 changes: 21 additions & 0 deletions analyzer/testomatio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from _pytest.python import Function
from .s3_connector import S3Connector

class Testomatio:
def __init__(self) -> None:
self.s3_connector = None
pass

def set_s3_connector(self, s3_connector: S3Connector) -> None:
self.s3_connector = s3_connector

def upload_file(self, file_path: str, key: str = None, bucket_name: str = None) -> str:
return self.s3_connector.upload_file(file_path, key, bucket_name)

def upload_file_object(self, file_bytes: bytes, key: str, bucket_name: str = None) -> str:
return self.s3_connector.upload_file_object(file_bytes, key, bucket_name)

def add_artifacts(self, node: Function, urls: list[str]) -> None:
artifact_urls = node.stash.get("artifact_urls", [])
artifact_urls.extend(urls)
node.stash["artifact_urls"] = artifact_urls
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[build-system]
requires = ["setuptools>=65.5.1", "requests>=2.29.0", "pytest>=7.3.1", "boto3>=1.28.28", "libcst==1.1.0"]
requires = ["setuptools>=65.5.1", "requests>=2.29.0", "pytest<8.0.0,>=7.3.1", "boto3>=1.28.28", "libcst==1.1.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
exclude = [".github", "tests"]

[project]
name = "pytest-analyzer"
version = "1.5.0"
version = "1.6.0"

dependencies = [
"requests>=2.29.0",
"pytest>=7.3.1",
"pytest<8.0.0,>=7.3.1",
"boto3>=1.28.28",
"libcst==1.1.0"]

Expand Down

0 comments on commit cd47c01

Please sign in to comment.