2017 © Pedro Peláez
 

library unittest

Unittests for the XP Framework

image

xp-framework/unittest

Unittests for the XP Framework

  • Monday, July 30, 2018
  • by thekid
  • Repository
  • 3 Watchers
  • 0 Stars
  • 37,000 Installations
  • PHP
  • 66 Dependents
  • 1 Suggesters
  • 0 Forks
  • 3 Open issues
  • 38 Versions
  • 11 % Grown

The README.md

Unittests

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version, (*1)

Unittests for the XP Framework, (*2)

Writing a test

Tests reside inside a class and are annotated with the @test attribute., (*3)

use unittest\{Assert, Test};

class CalculatorTest {

  #[Test]
  public function addition() {
    Assert::equals(2, 1 + 1);
  }
}

To run the test, use the test subcommand:, (*4)

$ xp test CalculatorTest
[.]

♥: 1/1 run (0 skipped), 1 succeeded, 0 failed
Memory used: 1672.58 kB (1719.17 kB peak)
Time taken: 0.000 seconds

Assertion methods

The unittest package provides the following six assertion methods:, (*5)

public abstract class unittest.Assert {
  public static void equals(var $expected, var $actual, string $error)
  public static void notEquals(var $expected, var $actual, string $error)
  public static void true(var $actual, string $error)
  public static void false(var $actual, string $error)
  public static void null(var $actual, string $error)
  public static void instance(string|lang.Type $type, var $actual, string $error)
  public static void throws(string|lang.Type $type, callable $block)
}

If you need more than that, you can use xp-forge/assert on top of this library., (*6)

Setup and teardown

In order to run a method before and after the tests are run, annotate methods with the @before and @after attributes:, (*7)

use unittest\{Assert, Before, After, Test};

class CalculatorTest {
  private $fixture;

  #[Before]
  public function newFixture() {
    $this->fixture= new Calculator();
  }

  #[After]
  public function cleanUp() {
    unset($this->fixture);
  }

  #[Test]
  public function addition() {
    Assert::equals(2, $this->fixture->add(1, 1));
  }
}

Note: All test methods are run with the same instance of CalculatorTest!, (*8)

Expected exceptions

The Expect attribute is a shorthand for catching exceptions and verifying their type manually., (*9)

use lang\IllegalArgumentException;
use unittest\{Test, Expect};

class CalculatorTest {

  #[Test, Expect(IllegalArgumentException::class)]
  public function cannot_divide_by_zero() {
    (new Calculator())->divide(1, 0);
  }
}

Ignoring tests

The Ignore attribute can be used to ignore tests. This can be necessary as a temporary measure or when overriding a test base class and not wanting to run one of its methods., (*10)

use unittest\{Test, Ignore};

class EncodingTest {

  #[Test, Ignore('Does not work with all iconv implementations')]
  public function transliteration() {
    /* ... */
  }
}

Parameterization

The Values attribute can be used to run a test with a variety of values which are passed as parameters., (*11)

use lang\IllegalArgumentException;
use unittest\{Test, Expect, Values};

class CalculatorTest {

  #[Test, Expect(IllegalArgumentException::class), Values([1, 0, -1])]
  public function cannot_divide_by_zero($dividend) {
    (new Calculator())->divide($dividend, 0);
  }
}

Actions

To execute code before and after tests, test actions can be used. The unittest library comes with the following built-in actions:, (*12)

  • unittest.actions.ExtensionAvailable(string $extension) - Verifies a given PHP extension is loaded.
  • unittest.actions.IsPlatform(string $platform) - Verifies tests are running on a given platform via case-insensitive match on PHP_OS. Prefix with ! to invert.
  • unittest.actions.RuntimeVersion(string $version) - Verifies tests are running on a given PHP runtime. See version_compare for valid syntax.
  • unittest.actions.VerifyThat(function(): var|string $callable) - Runs the given function, verifying it neither raises an exception nor return a false value.
use unittest\actions\{IsPlatform, VerifyThat};
use unittest\{Test, Action};

class FileSystemTest {

  #[Test, Action(eval: 'new IsPlatform("!WIN")')]
  public function not_run_on_windows() {
    // ...
  }

  #[Test, Action(eval: 'new VerifyThat(fn() => file_exists("/\$Recycle.Bin");')]
  public function run_when_recycle_bin_exists() {
    // ...
  }
}

Multiple actions can be run around a test by passing an array to the @action attribute., (*13)

Further reading

The Versions

30/07 2018

dev-master

9999999-dev http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

30/07 2018

v9.5.1

9.5.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

24/06 2018

v9.5.0

9.5.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

23/06 2018

v9.4.2

9.4.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

02/04 2018

v9.4.1

9.4.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

02/04 2018

dev-refactor/remove-xp-stringof

dev-refactor/remove-xp-stringof http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

31/10 2017

v9.4.0

9.4.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

12/10 2017

v9.3.0

9.3.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

27/06 2017

v9.2.0

9.2.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

17/06 2017

dev-fix/issue-23

dev-fix/issue-23 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

16/06 2017

v9.1.1

9.1.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

04/06 2017

v9.1.0

9.1.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

28/05 2017

v9.0.1

9.0.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

28/05 2017

v9.0.0

9.0.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

25/05 2017

v8.0.0

8.0.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

20/05 2017

v7.2.0

7.2.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

20/09 2016

v7.1.1

7.1.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

28/08 2016

v7.1.0

7.1.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

18/06 2016

v7.0.2

7.0.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

17/03 2016

v7.0.1

7.0.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

21/02 2016

v7.0.0

7.0.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

23/01 2016

v6.10.1

6.10.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

17/01 2016

dev-refactor/use-mirrors

dev-refactor/use-mirrors http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

11/01 2016

v6.10.0

6.10.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

10/01 2016

v6.9.0

6.9.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

09/01 2016

v6.8.3

6.8.3.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

05/01 2016

v6.8.2

6.8.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

05/01 2016

v6.8.1

6.8.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

03/01 2016

v6.8.0

6.8.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

29/12 2015

v6.7.2

6.7.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

29/12 2015

v6.7.1

6.7.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

13/12 2015

v6.7.0

6.7.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

13/12 2015

v6.6.2

6.6.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

11/12 2015

v6.6.1

6.6.1.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

08/11 2015

v6.6.0

6.6.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

27/09 2015

v6.5.0

6.5.0.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

06/08 2015

dev-refactor/test-running

dev-refactor/test-running http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp

06/08 2015

v6.4.2

6.4.2.0 http://xp-framework.net/

Unittests for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

module xp