# RCA Report Generator

A modern, interactive reporting dashboard for Pytest-based QA automation projects.  
Generates a clear Root Cause Analysis (RCA) report, supporting rapid investigation, team triage, and continuous improvement.

## Features

- **Visual summary** of all test results (pass/fail/skip), anomalies, and execution times.
- **Root Cause Analysis**: Highlights failing modules/scripts.
- **Interactive test table** with search, status filtering, and drilldown for each test case.
- **Classification of failures**: Distinguishes between likely real bugs and probable test/infrastructure issues.
- **Actionable recommendations** generated by LLM (e.g. GPT-4).
- **Downloadable results** in JSON/CSV.
- **Summary graphs** (pie/bar) and execution time distribution.
- **Responsive UI** – optimized for both desktop and mobile.
- **Works with any Pytest project**: Supports unit, integration, and end-to-end tests, as long as JUnit XML is generated.

---

## Demo

<p align="center">
  <img src="https://github.com/keinar/pytest-rca-report/raw/main/demo.gif" width="900" alt="RCA Dashboard Demo">
</p>


## Quick Start

You can also install via pip if available:
```bash
pip install pytest-rca-report
```

### 1. **Download and Set Up**

1. **Clone or download this repository**:
   ```bash
   git clone https://github.com/keinar/pytest-rca-report.git
   ```
   Or [download as ZIP](https://github.com/keinar/pytest-rca-report/archive/refs/heads/main.zip) and extract.

2. *(Optional)* Move or symlink the folder into your automation project repo.
   > You can keep this tool anywhere, but for convenience, place it inside or next to your main automation repo.

3. **Install dependencies**:
   ```bash
   cd pytest-rca-report
   pip install -r requirements.txt
   ```

---

### 2. **Generate pytest results and log**

After running your test suite, make sure to have:
- `pytest-results.xml` (Pytest JUnit XML output)
- `automation.log` (optional, for enhanced RCA)

Example:
```bash
pytest --junitxml=pytest-results.xml
```

---

### 3. **Run the RCA generator**

Navigate to the `pytest-rca-report` directory and run:

```bash
python parser.py
```
- Requires Python 3.9+ and OpenAI API key set as `OPENAI_API_KEY` in your environment.
- The script will **automatically create the `data/` folder** if it doesn't exist, and will generate `data/results.json`.
- At the end of the run, the interactive dashboard will be generated in the `./dashboard` folder.
- **Due to browser security (CORS) restrictions, you should view the dashboard using a local web server.**  
  To view the dashboard with full data, run:
  ```
  python -m http.server 8000 --directory dashboard
  ```
  Then open:
  ```
  http://localhost:8000/index.html
  ```
- On most systems, the dashboard will also open automatically after the script completes.

---

**Compatibility:**  
_The RCA Report Generator works with any Pytest project that produces a JUnit XML file – from unit tests to full end-to-end suites._

---

## Configuration

The main script (`parser.py`) uses the following paths and model by default:
```python
XML_PATH = './pytest-results.xml'
LOG_PATH = './automation.log'
OUT_JSON = './data/results.json'
MODEL = 'gpt-4o-mini'
```

- If your files are located elsewhere, or you wish to change the output location or LLM model, edit these variables at the top of `parser.py`.

- Supported OpenAI models: you can specify any available chat/completions model (e.g., `gpt-4o-mini`, `gpt-4-turbo`).

---

## Folder Structure

```
pytest-rca-report/
├── src/
│   └── rca_report/
│       ├── __init__.py
│       ├── parser.py
│       ├── assets/
│       │   ├── index.html
│       │   └── static/
│       │       ├── css/
│       │       │   └── style.css
│       │       └── js/
│       │           └── app.js
├── data/
│   └── results.json        # Processed summary/report file (generated)
├── dashboard/              # Generated interactive dashboard (after running parser.py)
│   └── index.html
│   └── static/
├── pytest-results.xml      # Input: JUnit XML from Pytest
├── automation.log          # Input: test logs (optional)
├── pyproject.toml
├── README.md
├── LICENSE
├── .env.example
├── .gitignore
```

> Note: When installed via pip, you can run the CLI from any directory. All assets are included in the package under `src/rca_report/assets/`.

---

## Usage & Navigation

- **Results Summary:** Top summary of all tests.
- **Summary Graphs:** Pie & bar charts of pass/fail/skip.
- **Log Anomalies:** Aggregated errors and warnings across all tests.
- **Root Cause Analysis:** Failure counts per module/script.
- **Execution Time:** Histogram & slowest tests listed.
- **Actionable Recommendations:** Next steps as suggested by the LLM.
- **All Testcases Table:**  
  - Search any test by name.
  - Filter by status (Passed/Failed/Skipped).
  - Click any row for a detailed modal with properties and failure reason.
  - Download all results as JSON or CSV.
- **Failure Classification:** Clear separation between likely real bugs and possible test automation issues.

---

## Customization

- To further enhance analysis, you can add more `record_property` info in your Pytest tests (these appear as custom properties in the report).
- All frontend code (JS/CSS/HTML) is modular and can be extended as needed.

---

## Requirements

- Python 3.9+  
- [OpenAI Python SDK](https://github.com/openai/openai-python)  
- Internet access for LLM processing  
- A modern browser (Chrome, Firefox, Edge, Safari)

You can install dependencies either by cloning the repo and running:
```bash
pip install -r requirements.txt
```
or by installing the package from PyPI (if available):
```bash
pip install pytest-rca-report
```

---

## Troubleshooting

- **results.json not found?** Run `parser.py` after you have a fresh `pytest-results.xml`.
- **Chart not showing?** Ensure you have execution time data in the XML.
- **CORS issues / Dashboard not loading data?**  
  Due to browser security restrictions, opening `index.html` directly via `file://` will not load data properly.  
  Instead, serve the `dashboard` folder with a local web server:  
  ```bash
  python -m http.server 8000 --directory dashboard
  ```
  Then open in your browser:  
  ```
  http://localhost:8000/index.html
  ```
- **LLM recommendations irrelevant?** Refine the prompt in `parser.py`.

---

## Roadmap / Suggestions

- Regression trend analysis between runs
- "Mark as Known Issue" UI
- Integration with defect tracking (Jira etc.)
- Team comments on test failures

---

## License

[MIT License](https://github.com/keinar/pytest-rca-report/blob/main/LICENSE)
