<p><strong>Meta Description:</strong> Aprenda a definir e acompanhar métricas de QA: cobertura, defeitos, velocity, DORA metrics, como criar dashboards efetivos.</p>

<hr>
<h2>Por Que Métricas?</h2>

<ul><li><strong>Visibilidade</strong> - Entender estado do projeto</li><ol><li><strong>Tendências</strong> - Evolução ao longo do tempo</li>
<li><strong>Decisões</strong> - Base para ações</li>
<li><strong>Accountability</strong> - Responsabilidade clara</li></ol></ul>

<hr>
<h2>Métricas de Teste</h2>

<h3>Cobertura</h3>

<pre><code>def test_coverage_metrics():
    return {
        "code_coverage": {
            "line": 85,  # {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}
            "branch": 78,
            "function": 90
        },
        "requirement_coverage": 95,  # {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}
        "risk_coverage": 80,  # {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}
        "test_efficiency": {
            "bugs_found": 15,
            "tests_executed": 500,
            "ratio": 0.03  # 3 bugs por 100 testes
        }
    }
</code></pre>

<h3>Execution Metrics</h3>

<pre><code>def execution_metrics():
    return {
        "total_tests": 1000,
        "passed": 950,
        "failed": 30,
        "blocked": 10,
        "skipped": 10,
        "pass_rate": 95,  # {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}
        "flaky_rate": 2,  # {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}
        "execution_time": {
            "total_minutes": 45,
            "average_per_test_seconds": 2.7
        }
    }
</code></pre>

<hr>
<h2>Métricas de Defeitos</h2>

<pre><code>class DefectMetrics:
    def __init__(self, data):
        self.data = data

<p>def defect_leakage(self):<br>        """{6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442} de defeitos encontrados em produção"""<br>        prod = self.data['bugs_in_prod']<br>        total = self.data['total_bugs']<br>        return (prod / total) * 100 if total else 0</p>

<p>def defect_density(self):<br>        """Defeitos por 1000 linhas"""<br>        bugs = self.data['total_bugs']<br>        loc = self.data['lines_of_code']<br>        return (bugs / loc) * 1000</p>

<p>def escape_rate(self):<br>        """{6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442} de defeitos escapados para prod"""<br>        return self.defect_leakage()</p>

<p>def mtbf(self):<br>        """Mean Time Between Failures"""<br>        return self.data['uptime_hours'] / self.data['incidents']</p>

<p>def mttr(self):<br>        """Mean Time To Repair"""<br>        return sum(r['time_to_fix'] for r in self.data['incidents']) / len(self.data['incidents'])<br></code></pre></p>

<hr>
<h2>DORA Metrics</h2>

<h3>Deployment Frequency</h3>

<pre><code>def deployment_frequency(days=30):
    """
    Elite: On-demand ou múltiplas vezes ao dia
    High: Entre diária e semanal
    Medium: Entre semanal e mensal
    Low: Mensal ou menos
    """
    deploys = get_deployments(days)
    return {
        "count": len(deploys),
        "frequency": len(deploys) / days,  # por dia
        "rating": "elite" if len(deploys) &gt;= days * 2 else "high" if len(deploys) &gt;= days else "medium"
    }
</code></pre>

<h3>Lead Time for Changes</h3>

<pre><code>def lead_time(days=30):
    """
    Elite:  1 mês
    """
    changes = get_changes(days)
    avg_hours = sum(c['lead_time_hours'] for c in changes) / len(changes)
    return {
        "average_hours": avg_hours,
        "p95_hours": calculate_percentile(changes, 95),
        "rating": "elite" if avg_hours &lt; 1 else &quot;high&quot; if avg_hours &lt; 24 else &quot;medium&quot;
    }
</code></pre>

<hr>
<h2>Dashboard</h2>

<pre><code># Grafana Dashboard - QA Metrics

<p>panels:<br>  - title: "Test Pass Rate"<br>    type: gauge<br>    targets:<br>      - expr: "sum(test_passed) / sum(test_total) * 100"<br>    thresholds:<br>      - value: 90<br>        color: yellow<br>      - value: 95<br>        color: green</p>

<p>- title: "Code Coverage Trend"<br>    type: timeseries<br>    targets:<br>      - expr: "code_coverage_percent"<br>        legendFormat: "Coverage {6727d158e4474b0847515bd23a8ee4bebd5f1aac7a18bc968e51d8985dccb442}"</p>

<p>- title: "Defect Leakage"<br>    type: stat<br>    targets:<br>      - expr: "defects_in_prod / total_defects * 100"<br>    unit: percent</p>

<p>- title: "DORA Metrics"<br>    type: table<br>    targets:<br>      - expr: "deployment_frequency"<br>      - expr: "lead_time_hours"<br>      - expr: "mttr_minutes"<br>      - expr: "change_failure_rate"<br></code></pre></p>

<hr>
<h2>Conclusão</h2>

<p>Métricas guiam melhoria. As chaves são:<br><ol><li><strong>Definir KPIs</strong> - Alinhados com objetivos</li><br><li><strong>Automatizar coleta</strong> - CI/CD integration</li><br><li><strong>Visualizar</strong> - Dashboards claros</li><br><li><strong>Analisar tendências</strong> - Não só snapshots</li><br><li><strong>Agir</strong> - Métricas sem ação não valem nada</li></ol></p>