xp-framework / unittest

Unittests for the XP Framework
0 stars 0 forks source link

Add metrics to TestResult #34

Closed thekid closed 6 years ago

thekid commented 6 years ago

Implements #33

Usage

Inside a listener's testRunFinished() method, call the metric() method. Here's an example from the coverage library:

class CoveredLines extends Metric {
  private $coverage, $executed, $executable;

  public function __construct($coverage) {
    $this->coverage= $coverage;
  }

  protected function calculate() {
    $report= $this->coverage->getReport();
    $this->executed= $report->getNumExecutedLines();
    $this->executable= $report->getNumExecutableLines();
  }

  protected function format() {
    $percent= $this->executed / $this->executable * 100;
    return sprintf(
      "%s%.2f%%\033[0m lines covered (%d/%d)",
      $percent < 50.0 ? "\033[31;1m" : ($percent < 90.0 ? "\033[33;1m" : "\033[32;1m"),
      $percent,
      $this->executed,
      $this->executable
    );
  }

  protected function value() {
    return $this->executed / $this->executable * 100;
  }
}

$result->metric('Coverage', new CoveredLines($this->coverage));

Example

image