inventor96/fatfree-test-manager

一个轻量级类,用于运行并报告使用 Fat-Free 框架 Test 类的单元测试结果。

v0.3.0 2021-03-12 02:15 UTC

This package is auto-updated.

Last update: 2024-09-12 10:37:39 UTC


README

使用 Fat-Free 框架 Test 运行并报告单元测试结果的一个轻量级类。

安装

composer require --dev inventor96/fatfree-test-manager

用法

该工具的目标是尽可能简单和轻量。

概述

我们的想法是扫描一个文件夹(非递归)中所有以 Test.php 结尾的文件(例如 MyAwesomeTest.php)。对于找到的每个文件,我们找到第一个类定义,实例化该类,然后调用该类中所有以 test 开头的公共方法(例如 public function testIfThisWorks() { ... })。

有了这个结构,只需调用 TestManager::runAndReportTests('directory/with/test/files'); 即可。

扩展 TestBase

测试类必须直接或间接扩展 TestBase。如果间接扩展(测试类扩展了另一个扩展 TestBase 的类),中间的类(的构造函数)将希望将包含其自身名称的数组作为第二个参数传递给父类的构造函数。这将允许报告包括测试方法的正确类和方法名称。

例如

class MyTestBase extends TestBase {
	public function __construct(\Test &$test_instance) {
		parent::__construct($test_instance, [get_class()]);
	}
}

在测试类和方法之前/之后运行代码

如果测试类(或它扩展的类)有一个名为 preClass()preTest()postTest()postClass() 的方法;每个方法将在相应的时间被调用。

多个文件夹

如果您有多个包含测试的文件夹,您可以创建一个 Fat-Free 框架 Test 的实例,并针对每个目录调用 TestManager::runTests('a/directory/with/tests', $your_instance_of_Test);,然后在最后调用 TestManager::reportTests($your_instance_of_Test);

退出代码

默认情况下,如果存在失败的测试,runAndReportTests()reportTests() 将以退出代码 1 结束 PHP 进程;如果所有测试都成功,则退出代码为 0。要禁用此行为并允许脚本继续,将最后一个参数设置为 false

通用示例

example_dir/ExampleTest.php:

<?php

use inventor96\F3TestManager\TestBase;

class ExampleTest extends TestBase {
	private $pre_class_called = 0;
	private $post_class_called = 0;
	private $pre_test_called = 0;
	private $post_test_called = 0;

	public function preClass() {
		$this->pre_class_called++;
	}

	public function postClass() {
		$this->post_class_called++;

		echo "preClass() called: {$this->pre_class_called}".PHP_EOL;
		echo "postClass() called: {$this->post_class_called}".PHP_EOL;
		echo "preTest() called: {$this->pre_test_called}".PHP_EOL;
		echo "postTest() called: {$this->post_test_called}".PHP_EOL;
	}

	public function preTest() {
		$this->pre_test_called++;
	}

	public function postTest() {
		$this->post_test_called++;
	}

	private function testThisShouldNeverGetCalled() {
		// private methods should never get called by the TestManager
		$this->expect(false, 'This is not the method you are looking for...');
	}

	public function testPassExample() {
		$this->expect(true, 'the message for a passing test');
	}

	public function testFailExample() {
		$this->expect(false, 'the message for a failed test');
	}

	public function testPassAndFailExample() {
		$this->expect(true);
		$this->expect(false);
	}

	public function testWithException() {
		throw new Exception("This is the message for an exception");
	}
}

example_dir/test_runner.php:

<?php

use inventor96\F3TestManager\TestManager;

require_once('../vendor/autoload.php');

echo PHP_EOL."Running tests using separate calls.".PHP_EOL;
$test1 = new Test();
TestManager::runTests(__DIR__, $test1);
TestManager::reportTests($test1, false);

echo PHP_EOL."Running tests using one call.".PHP_EOL;
TestManager::runAndReportTests(__DIR__);

运行测试的示例如下

$ php test_runner.php 

Running tests using separate calls.
preClass() called: 1
postClass() called: 1
preTest() called: 4
postTest() called: 4
PASS: ExampleTest::testPassExample() // ExampleTest.php:38 - the message for a passing test
FAIL: ExampleTest::testFailExample() // ExampleTest.php:42 - the message for a failed test
PASS: ExampleTest::testPassAndFailExample() // ExampleTest.php:46
FAIL: ExampleTest::testPassAndFailExample() // ExampleTest.php:47
FAIL: ExampleTest::testWithException() // ExampleTest.php:51 - Exception: This is the message for an exception

Running tests using one call.
preClass() called: 1
postClass() called: 1
preTest() called: 4
postTest() called: 4
PASS: ExampleTest::testPassExample() // ExampleTest.php:38 - the message for a passing test
FAIL: ExampleTest::testFailExample() // ExampleTest.php:42 - the message for a failed test
PASS: ExampleTest::testPassAndFailExample() // ExampleTest.php:46
FAIL: ExampleTest::testPassAndFailExample() // ExampleTest.php:47
FAIL: ExampleTest::testWithException() // ExampleTest.php:51 - Exception: This is the message for an exception