Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing for dbt build result #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ can better report on tests in their UI.

## About this fork

This is the fork repository based on https://github.com/chasleslr/dbt-junitxml/ version 0.1.5
On top of that here were added:
This is the fork repository based on https://github.com/chasleslr/dbt-junitxml/ version 0.1.5
On top of that here were added:
1. Support of DBT Core 1.3+ (originally it supported only up to 1.2). Versions 0.2.x Tested on DBT 1.5
2. In case of test failures Junit XML contains additional information regarding Stored Results and original test SQL. Details can be found below.
3. Test name in the resulted xml is more specific rather than in original version .
Expand All @@ -20,24 +20,23 @@ Publishing as a regular pip module is considered
pip install "git+https://github.com/SOVALINUX/[email protected]#egg=dbt-junitxml"
```

We recommend you to stick to some specific version, since newer versions might contain changes that may impact your operations (not being backward incompatible at all, but rather change some visualizations you might be used to).
We recommend you to stick to some specific version, since newer versions might contain changes that may impact your operations (not being backward incompatible at all, but rather change some visualizations you might be used to).

## Usage

When you run your dbt test suite, the output is saved under `target/run_results.json`. Run the following command
to parse your run results and output a jUnit XML formatted report named `report.xml`.
When you run your dbt test suite, the output is saved under `target/run_results.json` and `target/manifest.json`. Run the following command to parse your run results and output a jUnit XML formatted report named `report.xml`.

```shell
dbt-junitxml parse target/run_results.json report.xml
dbt-junitxml parse target/run_results.json target/manifest.json report.xml
```

## Features description

### Rich XML output in case of test failure
In order to help you handle test failures right where you see it we're adding supporting information into Junit XML in case of test failure
It's even more than you see in the DBT CLI console output!
For example:

In order to help you handle test failures right where you see it we're adding supporting information into Junit XML in case of test failure
It's even more than you see in the DBT CLI console output!
For example:

```
Got 19 results, configured to fail if != 0
Expand All @@ -53,9 +52,9 @@ where reporter_employee_id is null

### Saving test SQL files for further analysis

Sometimes it's handy to see the exact SQL that was executed and tested by DBT without repeating compilation steps.
To achieve it we suggest you to save compiled tests SQL during your test run.
Below you can find a reference script:
Sometimes it's handy to see the exact SQL that was executed and tested by DBT without repeating compilation steps.
To achieve it we suggest you to save compiled tests SQL during your test run.
Below you can find a reference script:
```shell
dbt test --store-failures
mkdir -p target/compiled_all_sql && find target/compiled/ -name *.sql -print0 | xargs -0 cp -t target/compiled_all_sql/
Expand All @@ -64,7 +63,7 @@ zip -r -q compiled_all_sql.zip target/compiled_all_sql

### Integration with Report Portal

https://reportportal.io/ helps you to manage your test launches. Here at EPAM we're using this tool to manage over 4,000 DBT tests
https://reportportal.io/ helps you to manage your test launches. Here at EPAM we're using this tool to manage over 4,000 DBT tests

In order to upload your test run to reportportal you can use the following script:
```shell
Expand All @@ -81,4 +80,4 @@ Currently, only v4 of the [Run Results](https://docs.getdbt.com/reference/artifa

## Contribution

Development of this fork was partially sponsored by EPAM Systems Inc. https://www.epam.com/
Development of this fork was partially sponsored by EPAM Systems Inc. https://www.epam.com/
10 changes: 7 additions & 3 deletions src/dbt_junitxml/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class InvalidRunResult(Exception):
pass


def is_dbt_test(dict):
return dict["unique_id"].split(".")[0] == "test"


def convert_timestamp_to_isoformat(timestamp: str) -> str:
return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').strftime(
'%Y-%m-%dT%H:%M:%S')
Expand Down Expand Up @@ -48,14 +52,14 @@ def parse(run_result, manifest, output):
if not schema_version == "https://schemas.getdbt.com/dbt/run-results/v4.json":
raise InvalidRunResult("run_result.json other than v4 are not supported.")

if not executed_command == "test":
if not executed_command in ["test", "build"]:
raise InvalidRunResult(
f"run_result.json must be from the output of `dbt test`. Got dbt {executed_command}.")
f"run_result.json must be from the output of `dbt test` or `dbt build`. Got dbt {executed_command}.")

except KeyError as e:
raise InvalidRunResult(e)

tests = run_result["results"]
tests = [d for d in run_result["results"] if is_dbt_test(d)]
total_elapsed_time = run_result["elapsed_time"]
test_suite_timestamp = convert_timestamp_to_isoformat(run_result["metadata"]["generated_at"])

Expand Down