2017 © Pedro Peláez
 

library codecept-unittest-generator

Unit test generator from annotations.

image

hyperia/codecept-unittest-generator

Unit test generator from annotations.

  • Thursday, April 5, 2018
  • by hyperia-sk
  • Repository
  • 1 Watchers
  • 6 Stars
  • 589 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 2 % Grown

The README.md

Codecept UnitTest generator

This package allows you to generate PHPUnit tests from annotations, which you can write in your methods documentation., (*1)

  • Basic usage of this package is to generate your tests skeleton and basic tests.
  • You must check and complete all tests you generate, including the most basic methods.
  • Files to parse must declare one class, abstract class, trait or interface to be accepted.

screenshot_2017-12-08_17-12-32-github, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

composer require hyperia/codecept-unittest-generator:"^1.0"

or add, (*5)

"hyperia/codecept-unittest-generator": "^1.0"

to the require section of your composer.json., (*6)

Configuration

enable UnitGenerator extension in base /codeception.yml config file:, (*7)

extensions:
    enabled:
        - Codeception\Extension\RunFailed
    commands:
        - Codeception\Command\UnitGenerator
unitgenerator:
    config: 
        # erase old target files with new one
        overwrite: true 
        # if you want to generate tests for Interface too
        interface: false
        # automaticaly generate tests for getter / setter method
        auto: true
        # ignore errors that can be ignored
        ignore: true
        # regex (/.*config.php/ for example) that files must not match to have a tests generation
        exclude: '/.*Trait.*/'
        # regex (/.*.php/ for example) that files should match to have a tests generation
        include: '/.*.php/'
    dirs:
        # source directory => target directory
        - common/models: tests/unit/unitgen/common/models/
        - console/models: tests/unit/unitgen/console/models/

Usage

./vendor/bin/codecept generate:unit, (*8)

Annotations

/**
 * @PHPUnitGen\<phpunit_assertion_method>(<expectation>:{<parameters...>}) 
 */

This annotation use some parameters:, (*9)

  • phpunit_assertion_method: It is the PHPUnit assertion method you want ot use (like assertEquals, assertInstanceOf, assertTrue ...)., (*10)

  • expectation: The method return expected value. Some PHPUnit methods does not need it (like assertTrue), so in those cases, it can be null., (*11)

  • parameters: The method parameters., (*12)

See this example, we want to auto generate some simple test for this method:, (*13)

<?php
// The class to test content

/** @PHPUnitGen\AssertEquals('expected string':{1, 2, 'a string'}) */
/** @PHPUnitGen\AssertTrue(:{4, 5, 'another'}) */
/** @PHPUnitGen\AssertEquals(null) */
/** @PHPUnitGen\AssertNull() */
public function method(int $arg1 = 0, int $arg2 = 0, string $arg3 = 'default') {}

Those annotations will create basic PHPUnit tests:, (*14)

<?php
// The generated test for "method" in tests class

$this->assertEquals('expectation string', $this->method(1, 2, 'a string'));
$this->assertTrue($this->method(4, 5, 'another'));
$this->AssertEquals(null, $this->method());
$this->assertNull($this->method());

Getter and setter annotation

<?php
// The class to test content

/** @PHPUnitGen\Get() */
/** @PHPUnitGen\Set() */

Those two annotations will allow you to auto-generate tests for simple getter / setter. Your getter / setter needs to be named like the following:, (*15)

get<MyProperty>() {}
set<MyProperty>() {}

PHPUnit Generator has an "auto" option: If you activate it, it will search for the property when it find a method beginning with "get" or "set", and if the property exists, it will generate tests., (*16)

Private or protected method

If the method to test is private or protected, PHPUnit Generator will access the method with PHP ReflectionClass., (*17)

Requirements

UnitGenerator needs the following components to run:, (*18)

  • Codeception UnitGenerator is a module for Codeception. It will need a running version of this tool.
  • Phpunit-generator package.

The Versions

05/04 2018