annexus/testsuite

简单的单元测试和模拟框架

1.0.3 2015-07-15 07:04 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:43:52 UTC


README

PHP的单元测试和模拟框架。

使用模拟框架动态创建模拟和存根,并用单元测试框架测试您的代码。所有这些都在一个包中,易于使用!

快速指南

一个简单的单元测试和模拟框架。

通过composer简单地安装testsuite

/> composer require annexus/testsuite

现在在您的项目中创建一个文件夹,该文件夹将包含所有您的测试。在那个文件夹中创建一个简单的测试类(下面有简单示例的说明)。请注意,文件名必须与类名相同。示例

文件:SomeTest.php 您应该将您的类命名为: class SomeTest { }

现在您可以运行测试了。控制台命令应该如下所示

/> php [path to TestSuite.phar] [path to folder containing tests]

或者,如果您想加载启动文件,例如composer的"autoload.php"或您的自定义文件

/> php --bootstrap [path to bootstrap file] [path to TestSuite.phar] [path to folder containing tests]

示例

/> php /var/www/vendor/annexus/testsuite/TestSuite.phar /var/www/tests

或者带有启动文件

/> php --bootstrap /var/www/vendor/autoload.php /var/www/vendor/annexus/testsuite/TestSuite.phar /var/www/tests

创建单元测试

单元测试类(或测试用例)可以有任何名称,但必须始终从 CFUnitTestCase 继承,因此必须包含此类。类名与文件名相同很重要。

测试方法必须以 Test_ 作为前缀。Test Suite不会运行其他方法。

class ExampleTestCase extends CFUnitTestCase
{
    public function Test_A_simple_assertion()
    {
        $this->assert(5)->should()->be(5)->and()->beGreaterThan(2);
    }
}

此外,测试用例还可以有以下方法之一

public function setUp() - This is run before each test method is executed
public functin tearDown() - This is run at the end of each test method
public function setUpBeforeClass() - This is run before any test method is executed
public function tearDownAfterClass() - This is run after all test methods are executed

流畅断言

断言可以以流畅的方式做出。以下断言受支持。

// Mixed assertion
$this->assert($string)->Should()->be('something');
$this->assert($array)->Should()->be($someOtherArray);
$this->assert($bool)->Should()->notBe(true);

// Type checking
$this->assert($string)->Should()->beAString();
$this->assert($object)->Should()->beAnObject();
$this->assert($array)->Should()->beAnArray();
$this->assert($int)->Should()->beAnInt();
$this->assert($float)->Should()->beAFloat();
$this->assert($bool)->Should()->beTrue();
$this->assert($bool)->Should()->beFalse();
$this->assert($mixed)->Should()->NotBeNull();
$this->assert($mixed)->Should()->beNull()
$this->assert($string)->Should()->beEmpty();
$this->assert($string)->Should()->NotBeEmpty();

// String specific assertions
$this->assert($string)->should()->haveLength(5);
$this->assert($string)->should()->beEquivalentTo($someString); // Case insensitive compare
$this->assert($string)->should()->startsWith($someString); // Case sensitive compare
$this->assert($string)->should()->startsWithEquivalent($someString); // Case insensitive compare
$this->assert($string)->should()->endWith($someString); // Case sensitive compare
$this->assert($string)->should()->endWithEquivalent($someString); // Case insensitive compare
$this->assert($string)->should()->contain($someText);
$this->assert($string)->should()->notContain($someText);
$this->assert($string)->should()->containEquivalentOf($someString); // Case insensitive compare (also on array values)
$this->assert($string)->should()->notContainEquivalentOf($someString); // Case insensitive compare (also on array values)

// Array specific assertions
$this->assert($array)->should()->contain($someOtherArray); // On intersect = success
$this->assert($array)->should()->notContain($someOtherArray); // When not intersects = success
$this->assert($array)->should()->notContainNull();

// Number assertions
$this->assert($int)->should()->beGreaterOrEqualTo($number);
$this->assert($int)->should()->beGreaterThan($number);
$this->assert($int)->should()->beLessOrEqualTo($number);
$this->assert($int)->should()->beLessThan($number);
$this->assert($int)->should()->bePositive();
$this->assert($int)->should()->beNegative();
$this->assert($int)->should()->beInRange($min, $max); //min=1, max=2 and given=2 will result in success

// Date assertions
$this->assert($datetime)->should()->beAfter($someDatetime);
$this->assert($datetime)->should()->beBefore($someDatetime);
$this->assert($datetime)->should()->beOnOrAfter($someDatetime);
$this->assert($datetime)->should()->beOnOrBefore($someDatetime);

// Throwable assertions
$this->assert()->should()->shouldThrow($func); Give the method that should be executed as an anonymous function to this method.
$this->assert()->should()->shouldThrow($func)->WithMessage('Exact exception message');
$this->assert()->should()->shouldThrow($func)->WithMessage('* psrtial message'); // The asterisk acts as a wild card. Can be used at the beginning, end or both sides of the string
$this->assert()->should()->shouldNotThrow($func);


// Extending assertions with "And()"
$this->assert($string)->should()->beAString()->and()->HaveLength(5);

模拟

当您想模拟一个对象时,必须首先包含类 CFMock/CFMock.php。现在模拟将变得简单。

您可以创建对象的模拟版本如下

$mock = new Mock::create( 'DummyClass' );
$mock->aCallTo('SomeMethod')->returns('some value');

// Or even namespaced classes can be loaded:
$mock = new Mock::create( '\Some\Namespace\DummyClass' );

可以在 $mock 对象上调用方法。

模拟框架还附带了一些断言。

expectCallCount(2); // Expects that many calls to a certain method
expectMinimumCallCount(2); // Expects at least that many calls to a certain method
expectMaximumCallCount(2); // Expects a maximum of 2 calls to a method, less is fine as well
expectNever(); // A certain method should never be called
expectOnce(); // Only a single call is expected to be made to a certain method

模拟测试的典型设置可能是这样的

$mock = new Mock::create( 'DummyClass' );
$mock->aCallTo('SomeMethod')->returns('some value')->expectOnce();

$mock->someMethod('message'); // Will return the string: 'some value'

这显然不是一个真实世界的例子,但它应该说明这个想法。

运行单元测试

WebRunner

当您创建了单元测试后,可以轻松地从浏览器中运行这些测试。只需浏览到 "vendor/testsuite/annexus/Unit/Runners/WebWebRunner.php"

在那里,您可以简单地点击“运行测试”按钮或选择要运行的测试。

命令行

您还可以通过命令行运行测试

有两种方法可以运行测试

  1. TestSuite.phar "Path/to/testFolder/"
  2. TestSuite.phar --bootstrap "location/to/your/bootstrap/file.php" "Path/to/testFolder/"

注意,“--bootstrap” 必须 作为第一个参数。此命令可能很有用,例如,用于加载随composer一起提供的“autoload.php”文件。这样您就可以在单元测试中访问所有类。

屏幕截图

Example test with selected tests