GithubHelp home page GithubHelp logo

nrawe / june-tdd Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 0.0 1.91 MB

A small, simple TDD framework for PHP

License: MIT License

PHP 100.00%
php7 php tdd unit-testing tdd-framework testing-framework

june-tdd's Introduction

June

A clean, small and modern unit testing framework for PHP

I wrote this framework as a bit of a whim based on my experiences on writing tapped, but actually... I quite like it!

The goal of this framework is to provide a syntatically clean way of structuring tests, leveraging language improvements in PHP. Its founding principle is that TDD is about finding designs, not bugs. We still need to provide cases for bugs, but they're not the day one most important thing.

My experience with the current array of testing frameworks is that they don't really provide a clear separation of these concepts in their Domain Specific Languages and they can sometimes be a bit cumbersome to work with. I've tried to make this a doddle to learn and use.

Running tests

php /path/to/june.phar [file]

By default, the framework assumes that the tests should be run from the tests/ directory in the current working directory. If an argument is passed, it is assumed to be the file path to the test that needs to be run.

A basic test

<?php
// tests/CalculatorTest.php

use Calculator;
use function June\{unit, test};

unit(Calculator::class, function () {

    test('It can perform addition', function ($equals) {

        $calculator = new Calculator();

        $equals($calculator->add(1, 1), 2);
    });

});

This is as simple as it gets. Using the use function syntax, you can import in the testing framework functions. The unit() function groups the steps which need to be performed in order verify correct function, and the test() function defines what needs to be tested.

The callback provided to test() can optionally request assertions to be injected, in this case $equals. These assertions are invoked as a function and always take the form of assertion($actual, $expected).

The reason that assertions are exposed in this fashion is to provide a bit of a steer on code quality. If you write a test with a lot of assertions, it's might be an indication that there's something wrong with the design. To achieve this, we provide a visual cue via the length of the callback signature that this may not be optimal.

The list of available assertions is here.

Bugs

<?php
// tests/CalculatorTest.php

use Calculator;
use function June\{bug, unit, test};

unit(Calculator::class, function () {

    test('It can perform addition', function ($equals) {

        $calculator = new Calculator();

        $equals($calculator->add(1, 1), 2);
    });

    bug('Some weird edge case with additions', function ($equals) {
        $calculator = new Calculator();

        $equals($calculator->add(1, 4), 5);
    });

});

We've all been there: code done broke. We have to fix it. Really, bugs shouldn't effect the overall design of a unit and so the framework provides a semantic differential of these types of cases. When you're looking through, you can get a feel for the kinds of bugs that have been discovered and this might give you an indication about the quality of the units' design.

Disabling tests

<?php
// tests/CalculatorTest.php

use Calculator;
use function June\{unit, xbug, xtest};

unit(Calculator::class, function () {

    xtest('It can perform addition', function ($equals) {

        $calculator = new Calculator();

        $equals($calculator->add(1, 1), 2);
    });

    xbug('Some weird edge case with additions', function ($equals) {
        $calculator = new Calculator();

        $equals($calculator->add(1, 4), 5);
    });

});

Sometimes we need to disable a failing test. The framework provides an xbug() and xtest() function, similar to other frameworks, to allow you to quickly disable a test without having to get your comment on.

Debugging

<?php
// tests/CalculatorTest.php

use Calculator;
use function June\{unit, test, tinker};

unit(Calculator::class, function () {

    test('It can perform addition', function ($equals) {

        $calculator = new Calculator();

        tinker();

        $equals($calculator->add(1, 1), 2);
    });

});

Sometimes, you just need to get into the runtime to understand your test. The framework provides the tinker() helper function to do just that. When you have psy/psysh installed and invoke this function, a debug shell will be brought up so you can interact with the currently running step.

Available Assertions

  • $equals($actual, $expected) returns whether the two arguments are strictly equal
  • $unequal($actual, $expected) returns whether the two are strictly unequal

License

MIT Licensed, go bat-nuts crazy.

june-tdd's People

Contributors

nrawe avatar

Stargazers

 avatar  avatar  avatar

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.