2017 © Pedro Peláez
 

library phpunit-naughtytestdetector

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

image

petrkotek/phpunit-naughtytestdetector

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  • Monday, July 31, 2017
  • by petrkotek
  • Repository
  • 1 Watchers
  • 3 Stars
  • 72,866 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 11 Versions
  • 27 % Grown

The README.md

phpunit-naughtytestdetector

Build Status Coverage Status, (*1)

"Naughty test detector" for PHPUnit. Identifies tests leaving garbage after themselves., (*2)

Many of us have been there - your integration test works in isolation, but breaks when running in a sequence of tests. This can be very difficult to troubleshoot, but luckily phpunit-naughtytestdetector is here to help., (*3)

Requirements

  • PHPUnit 6.0+
  • Supported PHP versions: 7.0 and 7.1

Note: for older PHPUnit or PHP 5.6, use v0.2.0., (*4)

Installation

NaughtyTestDetector is installable via Composer and should be added as a require-dev dependency:, (*5)

composer require --dev petrkotek/phpunit-naughtytestdetector

Usage

1. Enable NaughtyTestListener by adding the following to your test suite's phpunit.xml file:

<phpunit bootstrap="vendor/autoload.php">
    ...
    <listeners>
        <listener class="PetrKotek\NaughtyTestDetector\PHPUnit\Listeners\NaughtyTestListener">
            <arguments>
                <!-- Class name of your own MetricFetcher -->
                <string>MyProject\TestUtils\MyMetricFetcher</string>
                <!-- Optional constructor arguments for the metric fetcher -->
                <array>
                    <element>
                        <string>hello world</string>
                    </element>
                </array>
                <!-- Optionally specify levels on which MetricFetcher should be executed -->
                <!-- Note: values below are the default ones -->
                <array>
                    <element key="executeOnTestLevel">
                        <boolean>false</boolean>
                    </element>
                    <element key="executeOnTestSuiteLevel">
                        <boolean>true</boolean>
                    </element>                
                    <element key="executeOnGlobalLevel">
                        <boolean>false</boolean>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
</phpunit>

2. Implement the MetricFetcher interface, e.g.:

namespace MyProject\TestUtils\MyMetricFetcher;

use PetrKotek\NaughtyTestDetector\MetricFetcher;

class MyMetricFetcher implements MetricFetcher
{
    private $db;

    public function __construct()
    {
        $this->db = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    }

    /**
     * @return array
     */
    public function fetchMetrics()
    {
        $result = mysqli_query($this->db, 'SELECT COUNT * FROM my_table');
        $row = mysqli_fetch_row($result);

        return ['records' => $row[0];
    }
}

Tip: You can also use built-in metric fetcher, e.g. PetrKotek\NaughtyTestDetector\MetricFetchers\GlobalsMetricFetcher., (*6)

3. Run your test suite.

E.g. phpunit --configuration integration.xml, (*7)

NaughtyTestListener will fetch metrics before & after each TestSuite (aka "test class") and if there is a difference between before & after, it prints out like message like this:, (*8)

MyProject\Integration\MyNamespace\BadTest is naughty!
 - my_table: 0 -> 5 (+5)

This means, that before the test, there was 0 records in the my_table and after executing all the tests, there were 5 records., (*9)

Note: If you want to temporarily disable Naughty Test Detector, use DISABLE_NAUGHTY_TEST_DETECTOR enviromental variable, e.g. DISABLE_NAUGHTY_TEST_DETECTOR=1 phpunit --configuration integration.xml., (*10)

The Versions

31/07 2017

dev-master

9999999-dev

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

31/07 2017

v1.0.1

1.0.1.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

31/07 2017

v1.1.0

1.1.0.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

08/06 2017

v1.0.0

1.0.0.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

21/12 2016

v0.2.0

0.2.0.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

16/11 2016

v0.1.4

0.1.4.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

16/11 2016

dev-improve-formatting

dev-improve-formatting

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

17/10 2016

v0.1.3

0.1.3.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

14/10 2016

v0.1.2

0.1.2.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

13/10 2016

v0.1.1

0.1.1.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

  • php ~5.3 || ^7.0

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky

13/10 2016

v0.1.0

0.1.0.0

"Naughty test detector" for PHPUnit. Identifies tests, which don't clean after themselves.

  Sources   Download

Apache License 2.0

The Requires

 

The Development Requires

by Petr Kotek

phpunit detector cleanup cleaning messy naughty erratic flaky