Coder Social home page Coder Social logo

silverstripeltd / phpunit-immediate-exception-printer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scriptfusion/phpunit-immediate-exception-printer

0.0 1.0 0.0 206 KB

:fax: Immediately prints exception and assertion failures during testing.

License: GNU Lesser General Public License v3.0

PHP 100.00%

phpunit-immediate-exception-printer's Introduction

PHPUnit Immediate Exception Printer

Latest version Total downloads Build status Test coverage Code style

Immediate Exception Printer is a PHPUnit plug-in that prints out exceptions and assertion failures immediately during a test run. Normally PHPUnit keeps error details secret until the end of the test run, but sometimes we don't want to wait that long. With Immediate Exception Printer, all secrets are immediately revealed, with a few extra benefits, too.

Benefits

  • Immediately print out exceptions and assertion failures as they occur.
  • Flawless test suite indicator: success dot turns to red exclamation mark if any prior tests failed.
  • Display the execution time of each test in tiered colour bands.
  • Display the name of each test case as it is executed.

Preview

The following preview is somewhat atypical but shows all supported output cases.

Preview image

This printer makes no attempt to modify the test summary; only runtime output is changed.

Usage

  1. Add the dependency to your Composer file's require-dev section.

    "scriptfusion/phpunit-immediate-exception-printer": "^1"
  2. Declare the printer class in your phpunit.xml configuration file.

    <phpunit
        printerClass="ScriptFUSION\PHPUnitImmediateExceptionPrinter\ImmediateExceptionPrinter"
        colors="true"
    >
  3. Run the tests! If you didn't update phpunit.xml the same options can be specified on the command-line instead.

    vendor/bin/phpunit --printer 'ScriptFUSION\PHPUnitImmediateExceptionPrinter\ImmediateExceptionPrinter' --color
  4. Enjoy immediate test execution feedback.

Requirements

  • PHP 5.6 or newer.
  • PHPUnit 5.5 or newer.

Testing

The printer's capabilities are exploited via CapabilitiesTest. However, this test file isn't run directly because many of these tests are designed to fail. Instead, we write tests that run PHPUnit internally, each of which invokes one of the capability test cases and verifies its output.

The real tests, also known as functional tests, are located in test/functional, written in PHPT format. PHPT is a scarcely documented format designed to support testing PHP itself. An undocumented feature of PHPUnit is its limited support for a subset of the PHPT test specification, which we exploit to test PHPUnit itself with our printer implementation loaded.

To run the tests, simply specify vendor/bin/phpunit -c test on the command line from the project directory. By default, we run all the functional PHPT tests. To run CapabilitiesTest instead, specify vendor/bin/phpunit -c test test/CapabilitiesTest.

Writing a functional test

To test the output of a particular capability we run CapabilitiesTest with the --filter option to target a specific test case. Each functional test contains the arguments passed to PHPUnit in the --ARGS-- section of the file. These arguments can be pasted directly after the PHPUnit command to see the resulting output from that test case. We verify the output in the --EXPECTF-- section of the file.

One challenge we must overcome is verifying coloured output including ANSI escape sequences. To see these escape sequences we can pipe the output of a specific capability test to cat -v as shown in the following example.

vendor/bin/phpunit -c test --colors=always test/CapabilitiesTest --filter ::testSuccess$ | cat -v

The output from cat will print the "escape" character as ^[. We must replace each occurrence of this character sequence with the literal escape character (ASCII character 27). The easiest way to obtain the real escape character is to just copy it from an existing functional test.

Create a new functional test by copying an existing test as a template, then modify the PHPUnit arguments and the expected output to match what we expect using the techniques just described. Don't forget to give the test a clear description in the --TEST-- section!

Inspiration

Thanks to the following open source projects that inspired this project. Keep being awesome ๐Ÿ‘.

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.