Skip to content

Commit

Permalink
fixed issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Ypurek committed Dec 6, 2023
1 parent 238b558 commit 0be734b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
34 changes: 3 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def test_example():

## Change log

### 1.3.0 - added artifacts support connector
- [issue 5](https://github.com/Ypurek/pytest-analyzer/issues/5) - connection issues not blocking test execution anymore

### 1.2.8 - fixed issues
- [issue 4](https://github.com/Ypurek/pytest-analyzer/issues/4) - Analyzer's execution order

Expand Down Expand Up @@ -168,35 +171,4 @@ def test_example():
- test analyzer able to submit test results to testomat.io

## Roadmap

- handle REST API exceptions
- improve logging

## Delivery hints

1. Do not forget update version in pyproject.toml
2. Do not forget update version in README.md
3. Follow next steps:
To install locally (for testing purposes)

```bash
pip install --upgrade .
```

To build package

```bash
py -m build
```

Install twine to upload package to pypi

```bash
py -m pip install --upgrade twine
```

Upload package to pypi

```bash
py -m twine upload --repository pypi dist/* --verbose
```
3 changes: 3 additions & 0 deletions analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def pytest_collection_modifyitems(session: Session, config: Config, items: list[
connector: Connector = pytest.connector
test_config = pytest.analyzer_test_run_config
run_details = connector.create_test_run(**test_config.to_dict())
if run_details is None:
log.error('Test run failed to create. Reporting skipped')
return
pytest.analyzer_test_run_config.test_run_id = run_details['uid']

if run_details.get('artifacts'):
Expand Down
60 changes: 50 additions & 10 deletions analyzer/connector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from requests.exceptions import HTTPError, ConnectionError
import logging

from .testItem import TestItem
Expand Down Expand Up @@ -35,8 +36,17 @@ def load_tests(self, tests: list[TestItem], no_empty: bool = True, no_detach: bo
"file": test.file_name
})

# with safe_request('Failed to load tests to testomat.io'):
response = self.session.post(f'{self.base_url}/api/load?api_key={self.api_key}', json=request)
try:
response = self.session.post(f'{self.base_url}/api/load?api_key={self.api_key}', json=request)
except ConnectionError:
log.error(f'Failed to connect to {self.base_url}')
return
except HTTPError:
log.error(f'Failed to connect to {self.base_url}')
return
except Exception as e:
log.error(f'Generic exception happened. Please report an issue. {e}')
return

if response.status_code == 200:
log.info(f'Tests loaded to {self.base_url}')
Expand All @@ -48,16 +58,27 @@ def get_tests(self, test_metadata: list[TestItem]) -> dict:
response = self.session.get(f'{self.base_url}/api/test_data?api_key={self.api_key}')
return response.json()

def create_test_run(self, title: str, env: str, group_title, parallel) -> dict:
def create_test_run(self, title: str, env: str, group_title, parallel) -> dict | None:
request = {
"title": title,
"env": env,
"group_title": group_title,
"parallel": parallel
}
filtered_request = {k: v for k, v in request.items() if v is not None}
# with safe_request('Failed to create test run'):
response = self.session.post(f'{self.base_url}/api/reporter?api_key={self.api_key}', json=filtered_request)

try:
response = self.session.post(f'{self.base_url}/api/reporter?api_key={self.api_key}', json=filtered_request)
except ConnectionError:
log.error(f'Failed to connect to {self.base_url}')
return
except HTTPError:
log.error(f'Failed to connect to {self.base_url}')
return
except Exception as e:
log.error(f'Generic exception happened. Please report an issue. {e}')
return

if response.status_code == 200:
log.info(f'Test run created {response.json()["uid"]}')
return response.json()
Expand Down Expand Up @@ -91,15 +112,34 @@ def update_test_status(self, run_id: str,
"code": code
}
filtered_request = {k: v for k, v in request.items() if v is not None}
# with safe_request(f'Failed to update test status for test id {test_id}'):
response = self.session.post(f'{self.base_url}/api/reporter/{run_id}/testrun?api_key={self.api_key}',
json=filtered_request)
try:
response = self.session.post(f'{self.base_url}/api/reporter/{run_id}/testrun?api_key={self.api_key}',
json=filtered_request)
except ConnectionError:
log.error(f'Failed to connect to {self.base_url}')
return
except HTTPError:
log.error(f'Failed to connect to {self.base_url}')
return
except Exception as e:
log.error(f'Generic exception happened. Please report an issue. {e}')
return
if response.status_code == 200:
log.info('Test status updated')

def finish_test_run(self, run_id: str) -> None:
self.session.put(f'{self.base_url}/api/reporter/{run_id}?api_key={self.api_key}',
json={"status_event": "finish"})
try:
self.session.put(f'{self.base_url}/api/reporter/{run_id}?api_key={self.api_key}',
json={"status_event": "finish"})
except ConnectionError:
log.error(f'Failed to connect to {self.base_url}')
return
except HTTPError:
log.error(f'Failed to connect to {self.base_url}')
return
except Exception as e:
log.error(f'Generic exception happened. Please report an issue. {e}')
return

def disconnect(self):
self.session.close()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exclude = [".github", "tests"]

[project]
name = "pytest-analyzer"
version = "1.2.8"
version = "1.3.0"

dependencies = [
"requests>=2.29.0",
Expand Down

0 comments on commit 0be734b

Please sign in to comment.