单元和集成测试包

v1.1 2022-09-11 22:57 UTC

This package is auto-updated.

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


README

一个轻量级的单元和集成(?)测试工具

Source Code Download Package PHP Programming Language Read License

UITesting

UITesting是一个用于单元和(?)集成测试的极轻量级工具。

安装

.env 文件

将以下环境变量复制粘贴到您的 .env 文件中。为了避免命名空间冲突,请自定义您的命名空间

PATH_TESTS=tests
TEST_CASE_TEMPLATE=vendor/rafael.moran/uitest/src/UITestCaseTemplate.tpl
TEST_NAMESPACE=<YouAppName>\UITesting\Tests
BASE_DIRNAME=
TEST_CASE_PREFIX=TestCase_

在您的根目录中创建一个新的 PHP 文件,命名任意,例如: run-tests.php,然后在 composer.json 中的 scripts 部分使用新的 PHP 文件名添加一个新的脚本

...
"scripts": {
        "uitest" : "php ./run-tests.php",
        "uimaker" : "php vendor/rafael.moran/uitest/uimaker"
	}
...

您可以通过以下方式执行您的新命令

$ composer uitest

或者您可以使用 REPL

$ composer uimaker -- -n=ClassNameX

创建 UITester 实例

就像使用其他 PHP 包一样使用此包

// file: run-tests.php (you can pick whatever filename you want)
require_once 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();

// Entities to test
include __DIR__ . '/examples/functions.php';
include __DIR__ . '/examples/classes/Car.php';
include __DIR__ . '/examples/classes/AirPlane.php';


// Use case
$tester = new \RafaelMoran\UITest\UITester(['verbose' => true]); // set to false to not display details

// Run all tests
$tester->all();

用法:如何创建 UITester

use RafaelMoran\UITest\UITester;

// Use case
$tester = new UITester(); // It runs all test from $_ENV['PATH_TESTS'], results are not displayed.

// Or

// It's run all tests from `/another/real/weird/path/`
$tester = new UITester([
						"path" => "/another/real/weird/path/",
						"verbose" => true,
						// Or...
						"v" => true,
					]); 

运行所有测试

/*
 * It runs all test cases from the default test folder `/tests/` 
 * and display in detail all assertions statuses.
 */
$tester->all(); 
	
// ...

/**
 * It runs all test cases from the default test folder `/another/test/folder/` 
 * and display in detail all assertions statuses.
 */
$tester->all("/another/test/folder/"); 

仅运行特定测试

/**
 * It runs only `CarTest_518135355` from the default test folder `/tests/` 
 * and display in detail all assertions statuses.
 */
$tester->only('CarTest_518135355')
		->outputTestResults(); 

// ...

/**
 * It runs only `CarTest_518135355` and `GetRandomStrTest_1218383454` test cases 
 * from the default test folder `/tests/` and display in detail 
 * all assertions statuses.
 */
$tester->only([
		'CarTest_518135355',
		'GetRandomStrTest_1218383454'
	])
	->outputTestResults(); 

// ...

/**
 * It runs only `CarTest_518135355` from the default test folder 
 * `/another/real/weird/path/` and display in detail all assertions statuses.
 */
$tester->setPath('/another/real/weird/path/')
	->only('CarTest_518135355')
	->outputTestResults(); 

用法:如何创建 UITestCase

所有测试用例都必须扩展自抽象类 UITestCase,并且所有测试/方法都必须以 test_ 开头并返回 void。

有一个名为 /tests/ 的文件夹,当您通过 REPL uimaker 创建测试用例时,默认情况下所有测试用例都会保存在此文件夹中。您可以将测试保存在任何其他文件夹中。为了执行它们,您需要指定新的路径,如上所示。

namespace <YourAppName>\UITesting\Tests;

use RafaelMoran\UITest\UITestCase;

// New test case needs to extend from abstract class UITestCase.
class CarTestCase extends UITestCase
{
	/**
	 * Tests if (new Car)->getType() returns a non-empty string.
	 *
	 * @return void
	 */
	public function test_cartype_is_string_and_not_empty() : void
	{
		$car_type = (new Car)->getType();

		$this->assertNotEmpty($car_type)
			->assertIsString($car_type);
	}

	/**
	 * Tests if values are float type.
	 *
	 * @return void
	 */
	public function test_if_value_is_float() : void
	{
		$this->assertIsFloat(27.25);
		$this->assertIsFloat('abc');
		$this->assertIsFloat(23);
		$this->assertIsFloat(23.5);
		$this->assertIsFloat(1e7);  // Scientific Notation. This is true.
		$this->assertIsFloat(true);
	}
	
}

有一个小的 REPL 可以帮助您创建新的测试用例,这个 REPL 是 uimaker

用法:如何使用 REPL uitest

$ cd uitesting // Or make an alias that point to `php uitest` globally

此命令将创建一个新的测试用例,命名为 ClassNameX

$ composer uimaker -n=ClassNameX
namespace <YourAppName>\UITesting\Tests;

use RafaelMoran\UITest\UITestCase;

class ClassNameXTestCase_518135355 extends UITestCase
{
	/**
	 * Tests if 'ClassNameXTestCase_518135355'...
	 *
	 * All tests MUST START WITH "test_".
	 *
	 * @return void
	 */
	public function test_() : void
	{
		/**
		 * Read ./lib/UITestCase.php file regarding assertions.
	 	 * 
	 	 * Examples:
	 	 * 
	 	 * $this->assertLength( 'abc', 3 ); # true
	 	 * $this->assertArrayHasKey('key3', array('key3'=>null, 'key4'=>1)); # true
		 */
	}
	
}
$ composer uimaker -n=FunctionNameX
OR 
$ composer uimaker --name=FunctionNameX
namespace <YourAppName>\UITesting\Tests;

use RafaelMoran\UITest\UITestCase;

class FunctionNameXTestCase_518135355 extends UITestCase
{
	/**
	 * Tests if 'FunctionNameXTestCase_518135355'...
	 *
	 * All tests MUST START WITH "test_".
	 *
	 * @return void
	 */
	public function test_() : void
	{
		/**
		 * Read ./lib/UITestCase.php file regarding assertions.
	 	 * 
	 	 * Examples:
	 	 * 
	 	 * $this->assertLength( 'abc', 3 ); # true
	 	 * $this->assertArrayHasKey('key3', array('key3'=>null, 'key4'=>1)); # true
		 */
	}
	
}

用法:一体化

require_once 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();

// Examples
include __DIR__ . '/examples/functions.php';
include __DIR__ . '/examples/classes/Car.php';
include __DIR__ . '/examples/classes/AirPlane.php';

// Use case
$tester = new \RafaelMoran\UITest\UITester(['verbose' => true]); // set to false to not display details

// Run all tests
$tester->all();

// Use case
$tester = new UITester(['verbose' => true]); // It displays all assertions in detail

// Or...

$tester = new UITester(['v' => true]); // It displays all assertions in detail

// ...

// Run all tests
$tester->all();
	
//...

// Only specific tests (string)
$tester->only('CarTest_518135355')
		->outputTestResults();

Detailed results and final report

// ...

// Only specific tests (array)
$tester->only([
		'CarTest_518135355',
		'GetRandomStrTest_1218383454'
	])
	->outputTestResults();

Final report

贡献

欢迎贡献。对于重大更改,请首先打开一个问题以讨论您想要更改的内容。

请确保适当更新测试。

版权和许可证

ralphmoran/uitest 工具版权 © Rafael Moran,并按照 MIT 许可协议(MIT)的使用条款使用。请参阅 LICENSE 了解更多信息。