Git Product home page Git Product logo

easy-coverage's Introduction

Introduction

A simple package to encourage/force developers to write tests.

Why I built this package?

I'm working with multiple junior developers in multiple projects.

I always want to find a way to encourage/force them to write tests.

And I want this process to be integrated into the CI/CD and pre-push workflow.

Installation

composer require "howtomakeaturn/easy-coverage:^0.1"

Usage

Let's say you think App\Services namespace is very important, and you want to force all developers to write tests for every public method under this namespace.

Here is the basic example:

<?php

namespace Tests\Coverage;

use PHPUnit\Framework\TestCase;
use Howtomakeaturn\EasyCoverage\EasyCoverage;

class CoverageTest extends TestCase
{
    public function testCoverage()
    {
        $coverage = new EasyCoverage();

        $coverage->includeNamespaces([
            'App\Services',
        ]);

        $coverage->scan();

        if ($coverage->result()) {
            $this->assertTrue(true);
        } else {
            $num = count($coverage->missingMethods());

            $str = implode(', ', $coverage->missingMethods());

            $msg = "You need to write tests for these $num methods: $str";

            $this->fail($msg);
        }
    }
}

Anytime you create a new test for the related methods, use PHP 8 Attribute Syntax to label them:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use Howtomakeaturn\EasyCoverage\Target;

class SimpleTaskOneTest extends TestCase
{
    #[Target('App\Services\SimpleTaskOne@doSomething')]
    public function testBasicTest()
    {
        $task = new \App\Services\SimpleTaskOne();

        $task->doSomething();

        $this->assertTrue(true);
    }
}

And that's it!

Anyone who creates new public methods in any classes under App\Services have to write tests!


If you want to ignore some methods, you can use

    $coverage->alwaysIgnoreMethods([
        '__construct',
    ]);

If you want to ignore some classes, you can use

    $coverage->ignoreClasses([
        'App\Services\LargeLegacyTask',
    ]);

If you want to ignore certain methods in classes, you can use

    $coverage->ignoreClassMethods([
        'App\Services\SimpleTaskTwo@doSomething',
    ]);

Recommended workflow

  1. You specify multiple namespaces that you think writing tests are necessary.

  2. Run EasyCoverage in PHPUnit & CI/CD & pre-push workflow.

  3. Anyone who writes new public methods under these namespaces will be forced to write tests!

How does this work behind the scenes?

  1. EasyCoverage finds out all the public & non-static methods under the namesapces you specify.

  2. EasyCoverage finds out all the class methods you labeled with Howtomakeaturn\EasyCoverage\Target attribute under the Tests namespace.

  3. EasyCoverage compares two array values from above and then return the result.

License

The EasyCoverage is open-sourced software licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.