{"id":20674644,"url":"https://github.com/petrkotek/phpunit-naughtytestdetector","last_synced_at":"2025-10-26T04:20:08.412Z","repository":{"id":47605247,"uuid":"70777075","full_name":"petrkotek/phpunit-naughtytestdetector","owner":"petrkotek","description":"\"Naughty test detector\" for PHPUnit. Identifies tests, which don't clean after themselves.","archived":false,"fork":false,"pushed_at":"2021-08-22T10:43:31.000Z","size":46,"stargazers_count":3,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-28T03:56:23.506Z","etag":null,"topics":["listener","php","phpunit","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrkotek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-13T06:44:13.000Z","updated_at":"2021-08-22T10:40:33.000Z","dependencies_parsed_at":"2022-09-12T08:22:09.314Z","dependency_job_id":null,"html_url":"https://github.com/petrkotek/phpunit-naughtytestdetector","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrkotek%2Fphpunit-naughtytestdetector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrkotek%2Fphpunit-naughtytestdetector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrkotek%2Fphpunit-naughtytestdetector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrkotek%2Fphpunit-naughtytestdetector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrkotek","download_url":"https://codeload.github.com/petrkotek/phpunit-naughtytestdetector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224854962,"owners_count":17380954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["listener","php","phpunit","testing"],"created_at":"2024-11-16T21:06:32.387Z","updated_at":"2025-10-26T04:20:08.362Z","avatar_url":"https://github.com/petrkotek.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# phpunit-naughtytestdetector\n[![Build Status](https://travis-ci.org/petrkotek/phpunit-naughtytestdetector.svg?branch=master)](https://travis-ci.org/petrkotek/phpunit-naughtytestdetector)\n[![Coverage Status](https://coveralls.io/repos/github/petrkotek/phpunit-naughtytestdetector/badge.svg?branch=master)](https://coveralls.io/github/petrkotek/phpunit-naughtytestdetector?branch=master)\n\n\"Naughty test detector\" for PHPUnit. Identifies tests leaving garbage after themselves.\n\nMany of us have been there - your integration test works in isolation, but breaks when running in a sequence of tests.\nThis can be very difficult to troubleshoot, but luckily `phpunit-naughtytestdetector` is here to help.  \n\n## Requirements\n\n- PHPUnit 6.0+\n- Supported PHP versions: 7.0 and 7.1\n\nNote: for older PHPUnit or PHP 5.6, use `v0.2.0`.\n\n## Installation\n\nNaughtyTestDetector is installable via [Composer](http://getcomposer.org) and should be added as a `require-dev` dependency:\n\n    composer require --dev petrkotek/phpunit-naughtytestdetector\n\n## Usage\n\n#### 1. Enable `NaughtyTestListener` by adding the following to your test suite's `phpunit.xml` file:\n\n```xml\n\u003cphpunit bootstrap=\"vendor/autoload.php\"\u003e\n    ...\n    \u003clisteners\u003e\n        \u003clistener class=\"PetrKotek\\NaughtyTestDetector\\PHPUnit\\Listeners\\NaughtyTestListener\"\u003e\n            \u003carguments\u003e\n                \u003c!-- Class name of your own MetricFetcher --\u003e\n                \u003cstring\u003eMyProject\\TestUtils\\MyMetricFetcher\u003c/string\u003e\n                \u003c!-- Optional constructor arguments for the metric fetcher --\u003e\n                \u003carray\u003e\n                    \u003celement\u003e\n                        \u003cstring\u003ehello world\u003c/string\u003e\n                    \u003c/element\u003e\n                \u003c/array\u003e\n                \u003c!-- Optionally specify levels on which MetricFetcher should be executed --\u003e\n                \u003c!-- Note: values below are the default ones --\u003e\n                \u003carray\u003e\n                    \u003celement key=\"executeOnTestLevel\"\u003e\n                        \u003cboolean\u003efalse\u003c/boolean\u003e\n                    \u003c/element\u003e\n                    \u003celement key=\"executeOnTestSuiteLevel\"\u003e\n                        \u003cboolean\u003etrue\u003c/boolean\u003e\n                    \u003c/element\u003e                \n                    \u003celement key=\"executeOnGlobalLevel\"\u003e\n                        \u003cboolean\u003efalse\u003c/boolean\u003e\n                    \u003c/element\u003e\n                \u003c/array\u003e\n            \u003c/arguments\u003e\n        \u003c/listener\u003e\n    \u003c/listeners\u003e\n\u003c/phpunit\u003e\n```\n\n#### 2. Implement the `MetricFetcher` interface, e.g.:\n```php\nnamespace MyProject\\TestUtils\\MyMetricFetcher;\n\nuse PetrKotek\\NaughtyTestDetector\\MetricFetcher;\n\nclass MyMetricFetcher implements MetricFetcher\n{\n    private $db;\n    \n    public function __construct()\n    {\n        $this-\u003edb = mysqli_connect(\"127.0.0.1\", \"my_user\", \"my_password\", \"my_db\");\n    }\n\n    /**\n     * @return array\n     */\n    public function fetchMetrics()\n    {\n        $result = mysqli_query($this-\u003edb, 'SELECT COUNT * FROM my_table');\n        $row = mysqli_fetch_row($result);\n        \n        return ['records' =\u003e $row[0];\n    }\n}\n\n```\n\nTip: You can also use built-in metric fetcher, e.g. `PetrKotek\\NaughtyTestDetector\\MetricFetchers\\GlobalsMetricFetcher`.\n\n#### 3. Run your test suite.\n\nE.g. `phpunit --configuration integration.xml`\n\n`NaughtyTestListener` will fetch metrics before \u0026 after each TestSuite (aka \"test class\") and if there is a difference\nbetween before \u0026 after, it prints out like message like this:\n```\nMyProject\\Integration\\MyNamespace\\BadTest is naughty!\n - my_table: 0 -\u003e 5 (+5)\n```\n\nThis means, that before the test, there was `0` records in the `my_table` and after executing all the tests, there were\n`5` records.\n\nNote: 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`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrkotek%2Fphpunit-naughtytestdetector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrkotek%2Fphpunit-naughtytestdetector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrkotek%2Fphpunit-naughtytestdetector/lists"}