Inline tests for PHP

    Inline tests are tests built directly into the DOC comments of the test script. Such a chip, as far as I know, is in Python, although I could not find a clear description. In any case, I liked the idea, since inline tests have a number of advantages compared to conventional ones, which I will outline below. Therefore, I want to bring to your attention a tool for running such tests for PHP.

    So, what are the advantages of inline tests compared to conventional unit tests (PHPUnit, etc.)?

    Benefits


    1. Simplicity and speed of adding tests : needless to say, sometimes you want to write a test, but when you remember that for this you will have to create the entire hierarchy of folders for this class (e.g. App_Module_Class), then create a file and a test class, write the same boring code checks, then look for where this PHPUnit is located and how to run it correctly - hands drop. And it’s not a pity to do this if the test is complex and important, but when it’s simple ...
    2. Inline tests can supplement / replace the documentation of the method : indeed, it is sometimes easier for the programmer to understand the essence of the code by reading what and under what parameters it returns than reading the description.
    3. Inline tests can even be written for private methods of the class : since the startup script actually extracts functions and methods from the context, it does not matter if they are private or public.
    4. They cannot be lost : tests will always be with the code.


    Of course, the scope of such tests is rather narrow, there are 2 limitations:

    Limitations


    1. Only suitable for simple tests : firstly, the test needs to be placed on one line of the comment, and secondly, you can’t write a complex test using the interconnection of several functions.
    2. Only suitable for isolated functions / methods : a function should communicate with the outside world only through arguments and return value.


    You can download the test run script here: github.com/ptrofimov/phpinlinetest

    P.S. Since PHPUnit uses assert tags to auto-generate its tests, it was decided to keep the same name for compatibility.

    Also popular now: