# Score Formula

This page will be polished some time in the future, but for now it's being used in production as of May 2024.

## General Goals & Ideas

There are 2 goals the OyenCov needs to achieve with the score formula:

- Preserving the legacy test coverage goal: Ensuring every line of codebase in use, is tested at least once.
- Incorporate usage weights into scoring, to help the engineers in prioritizing efforts into frequently used, thus commercially important parts of the codebase.

In the current formula, **Methods** are the first class citizens we measure usages & weightings of, including controller actions and background jobs’ perform method.

We currently don't look at conditional branches, but it's on the table, we will revisit it in the next iterations.

## Usage-weighting

The most used Methods in any given codebase running in production will have significantly higher orders of magnitude hits than most of the other Methods.

We turn usage into a more usable and tame weighting by sorting it and assign them with weights from 1.0 to 10.0. This ensures the most used Method is at most 10x as valuable as the least used ones.

$\text{normalized\_weight} = \left\lceil 1 + \frac{\log\left(1 - \frac{position}{total\_Methods\_count}\right)}{\log(1 - \frac{1}{3})} \right\rceil$## Test/prod deviation for each Method

- We calculate the mean and standard deviation log(raw_test_hits/raw_prod_hits) of the Methods that are both actively in use & tested.
- For Methods below the mean of test/prod coverage, calculate z-score = how many standard deviations below the mean.
- For Methods that are used but not tested, we set the z-score to be -10.

## General Score = sum(Usage-weight * Deviation)

Going back to our general goals, we want to ensure your engineering & testing efforts closely reflect how the codebase is being used in real life, while also not neglect any parts that are being used, however minor it is.

$\text{general\_score} = 10^2 \left( 10 + \frac{\sum_{i=1}^{n} w_i d_i}{\sum_{i=1}^{n} w_i} \right)$The general_score will be between 0 and 1000. Ideally, you should be going for 950, that should give your team sufficient confidence that most use cases of your application are covered with test suites.