Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

humbug

> 测试质量
Open source

Humbug is a Mutation Testing framework for PHP to measure the real effectiveness of your test suites and assist in their improvement. It eats Code Coverage for

1.1K stars0 likes3 views
WebsiteGitHub

About

Humbug is a Mutation Testing framework for PHP to measure the real effectiveness of your test suites and assist in their improvement. It eats Code Coverage for

Humbug: Mutation Testing for PHP

This package is deprecated, check out Infection instead.

Humbug is a Mutation Testing framework for PHP. It is currently in development and so, while it does actually work quite well, it will have rough edges that a team of minions are working hard to hammer out. If it falls out of the gate, you have been warned ;). ⚠️️ Update your remotes! Humbug has transferred to a new location. While your existing repositories will redirect transparently for any operations, take some time to transition to the new URL.

bash
$ git remote set-url upstream https://github.com/humbug/humbug.git

Replace upstream with the name of the remote you use locally; upstream is commonly used but you may be using something else. You may also using a different URL (e.g. [email protected]:mockery/mockery.git). Run git remote -v to see what you're actually using.

Table of Contents

  • Introduction
  • Contributing
  • Installation
    • Git
    • Phar
    • Composer
  • Usage
    • Configuration
      • Configure command
      • Manual Configuration
    • Running Humbug
      • The Metrics
  • Command Line Options
    • Overriding The Configured Timeout
    • Restricting Files To Mutate
    • Mutate specific files
    • Incremental Analysis
  • Performance
  • Mutators

Introduction

Mutation Testing is, in a nutshell, giving your unit tests a run for their money. It involves injecting small defects into source code and then checking if the unit tests noticed. If they do, then your unit tests have "killed" the mutation. If not, the mutation has escaped detection. As unit tests are intended to prevent regressions, having a real regression pass unnoticed would be a bad thing!

Whereas Code Coverage can tell you what code your tests are executing, Mutation Testing is intended to help you judge how well your unit tests actually perform and where they could be improved.

I've written in more detail about why Mutation Testing is worth having: Lies, Damned Lies and Code Coverage: Towards Mutation Testing

Contributing

Humbug is an open source project that welcomes pull requests and issues from anyone. Before opening pull requests, please read our short Contribution Guide.

Installation

Git

You can clone and install Humbug's dependencies using Composer:

bash
git clone https://github.com/humbug/humbug.git
cd humbug
/path/to/composer.phar install

The humbug command is now at bin/humbug.

Phar

If you don't want to track the master branch directly, you can install the Humbug phar as follows:

bash
wget https://padraic.github.io/humbug/downloads/humbug.phar
wget https://padraic.github.io/humbug/downloads/humbug.phar.pubkey
# If you wish to make humbug.phar directly executable
chmod +x humbug.phar

On Windows, you can just download using a browser or from Powershell v3 using the following commands where wget is an alias for Invoke-WebRequest:

bash
wget https://padraic.github.io/humbug/downloads/humbug.phar -OutFile humbug.phar
wget https://padraic.github.io/humbug/downloads/humbug.phar.pubkey -OutFile humbug.phar.pubkey

If you're stuck with Powershell v2:

bash
$client = new-object System.Net.WebClient
$client.DownloadFile("https://padraic.github.io/humbug/downloads/humbug.phar", "humbug.phar")
$client.DownloadFile("https://padraic.github.io/humbug/downloads/humbug.phar.pubkey", "humbug.phar.pubkey")
PHAR Updates

The phar is signed with an openssl private key. You will need the pubkey file to be stored beside the phar file at all times in order to use it. If you rename humbug.phar to humbug, for example, then also rename the key from humbug.phar.pubkey to humbug.pubkey.

The phar releases are currently done manually so they will not be updated with the same frequency as git master. To update your current phar, just run:

bash
./humbug.phar self-update

Note: Using a phar means that fixes may take longer to reach your version, but there's more assurance of having a stable development version. The public key is downloaded only once. It is re-used by self-update to verify future phar releases.

Once releases commence towards stable, there will be an alpha, beta, RC and a final release. Your development track phar file will self-update automatically until it reaches a stable release. If you wish to continue tracking the development level phars, you will need to indicate this using one of the stability flags:

bash
./humbug.phar self-update --dev
Self-Update Request Debugging

If you experience any issues self-updating with unexpected openssl or SSL errors, please ensure that you have enabled the openssl extension. On Windows, you can do this by adding or uncommenting the following line in the php.ini file for PHP on the command line (if different than the file for your http server):

extension=php_openssl.dll

Certain other SSL errors may arise due missing certificates. You can rectify this by finding their location on your system (e.g. C:/xampp/php/ext/cacert.pem), or alternatively downloading a copy from http://curl.haxx.se/ca/cacert.pem. Then ensure the following option is correctly pointing to this file:

openssl.cafile=C:/path/to/cacert.pem

Composer

Due to Humbug's dependencies being pegged to recent versions, adding Humbug to composer.json may give rise to conflicts. The above two methods of installation are preferred where this occurs. You can however install it globally as any other general purpose tool:

bash
composer global require 'humbug/humbug=~1.0@dev'

And if you haven't done so previously...add this to ~/.bash_profile (or ~/.bashrc):

bash
export PATH=~/.composer/vendor/bin:$PATH

Humbug currently works on PHP 5.4 or greater.

Usage

Configuration

Humbug is still under development so, to repeat, beware of rough edges.

Configure command

To configure humbug in your project you may run:

bash
humbug configure

This tool will ask some questions required to create the Humbug configuration file (humbug.json.dist).

Manual Configuration

In the base directory of your project create a humbug.json.dist file:

javascript
{
    "timeout": 10,
    "source": {
        "directories": [
            "src"
        ]
    },
    "logs": {
        "text": "humbuglog.txt",
        "json": "humbuglog.json"
    }
}

You can commit the humbug.json.dist to your VCS and override it locally with a humbug.json file.

Edit as appropriate. If you do not define at least one log, detailed information about escaped mutants will not be available. The Text log is human readable. If source files exist in the base directory, or files in the source directories must be excluded, you can add exclude patterns (here's one for files in base directory where composer vendor and Tests directories are excluded):

javascript
{
    "timeout": 10,
    "source": {
        "directories": [
            "."
        ],
        "excludes": [
            "vendor",
            "Tests"
        ]
    },
    "logs": {
        "text": "humbuglog.txt"
    }
}

If, from your project's base directory, you must run tests from another directory then you can signal this also. You should not need to run Humbug from any directory other than your project's base directory.

javascript
{
    "chdir": "tests",
    "timeout": 10,
    "source": {
        "directories": [
            "src"
        ],
    }
}

Running Humbug

Ensure that your tests are all in a passing state (incomplete and skipped tests are allowed). Humbug will quit if any of your tests are failing.

The magic command, while in your project's base directory (using the PHAR download) is:

bash
./humbug.phar

or if you just cloned Humbug:

bash
../humbug/bin/humbug

or if you added Humbug as a composer dependency to your project:

bash
./vendor/bin/humbug

Instead of php with the xdebug extension you may also run Humbug via phpdbg:

bash
phpdbg -qrr humbug.phar

If all went well, you will get something similar to:

…

To explain the perhaps cryptic progress output:

  • Killed Mutation (.): A mutation that caused unit tests to fail which is a positive outcome.
  • Escaped Mutation (M): A mutation where the unit tests still passed which is not what we want! Our unit tests should detect any behaviour changes.
  • Uncovered Mutation (S): A mutation which occurs on a line not covered by any unit test. Since there are no unit tests, this is another undesireable result.
  • Fatal Error (E): A mutation created a fatal error. Usually a positive result since it's obviously going to be noticed. In some cases, however, it might be a Humbug problem that needs fixing.
  • Timeout (T): This is where unit tests exceed the allowed timeout configured for Humbug. Likely a positive result if your timeout is appropriate, and often occurs when a mutation ends up creating an infinite loop.

Kills, errors and timeouts are all counted as detected mutations. We report errors in the logs on the off chance that Humbug itself encountered an internal error, i.e. a bug to be reported as an issue here!

The Metrics

The example summary results reported a number of metric scores:

  • A Mutation Score Indicator (MSI) of 47%. This means that 47% of all generated mutations were detected (i.e. kills, timeouts or fatal errors). The MSI is the primary Mutation Testing metric. Given the code coverage of 65%, there is a 18% discrepancy so Code Coverage was a terrible quality measurement in this example.
  • Mutation Code Coverage is 67%. On average it should be within the same ballpark as your normal code coverage, but code coverage ignores mutation frequency.
  • The Mutation Score Indicator for code that is actually covered by tests was 70% (i.e. ignoring code not even tested). This gives you some idea of how effective the tests that do exist really are.

If you examine these metrics, the standout issue is that the MSI of 47% is 18 points lower than the reported Code Coverage at 65%. These unit tests are far less effective than Code Coverage alone could detect.

Interpreting these results requires some context. The logs will list all undetected mutations as diffs against the original source code. Examining these will provide further insight as to what specific mutations went undetected.

Command Line Options

Humbug has a few command line options of note, other than those normally associated with any Symfony Console application.

Overriding The Configured Timeout

You can manually set the timeout threshold for any single test:

bash
humbug --timeout=10

Restricting Files To Mutate

If you're only interested in mutating a subset of your files, you can pass any number of --file options containing simple file names, globs or regular expressions. Basically, these are all passed to the Symfony Finder's name() method.

bash
humbug --file=NewClass.php --file=*Driver.php

This in no way restricts the initial Humbug check on the overall test suite which is still executed in full to ensure all tests are passing correctly before proceeding.

Mutate specific files

If you want to mutate only a few specific files, you can pass any number of --path options containing f

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPcoveragemutation-analysismutation-testingphp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.