ozzie/pest-plugin-nest

Nest Pest PHP 测试,以实现更好的组织和可读性

v1.0.0 2023-04-12 02:08 UTC

This package is auto-updated.

Last update: 2024-09-06 01:47:30 UTC


README

警告

已弃用:Pest 现在已官方支持 describe 块

https://pest.php.ac.cn/docs/pest-spicy-summer-release#content-describe-blocks

Nest - 可视化分组你的 Pest 测试

Nest 是一个 PestPHP 的插件,允许你在测试文件和测试输出中进行可视化分离

安装

对于 pest 2.0,请确保你已经升级到 ^1.0

composer require ozzie/pest-plugin-nest --dev

对于 pest 1.0,请确保你已经升级到 ^0.1

composer require ozzie/pest-plugin-nest "^0.1" --dev

用法

注意 虽然这些函数将它们的 api 转发到本机 Pest 的 test/it 函数,但必须使用 Nest 命名空间函数,以便正确显示报告。

use function Ozzie\Nest\describe;
use function Ozzie\Nest\it;

describe('sub()', function() {
  it('subtracts the second number from the first')
    ->expect(Calc::sub(10, 5))
    ->toEqual(5);
});

describe('add()', function() {
  it('adds two numbers together')
    ->expect(Calc::add(1, 2))
    ->toEqual(3);
});

输出

basic usage test output

您还可以嵌套多个 describe 块,并可选地使用 when() 块作为 describe() 的替代

use function Ozzie\Nest\describe;
use function Ozzie\Nest\when;
use function Ozzie\Nest\it;

describe('abs()', function () {
  when('positive value', function() {
    it('returns the number back', function () {
      expect(Calc::abs(1))->toEqual(1);
    });
  });
  
  when('negative value', function() {
    it('returns the number back as positive', function () {
      expect(Calc::abs(-1))->toEqual(1);
    });
    it('returns a float number back as positive', function () {
      expect(Calc::abs(-1.5))->toEqual(1.5);
    });
  });
});

describe('sub()', function() {
  it('subtracts the second number from the first')
    ->expect(Calc::sub(10, 5))
    ->toEqual(5);
});

输出

basic usage test output

您也可以直接调用类,如果您喜欢的话

use Ozzie\Nest\Nest;

Nest::describe('sub()', function() {
  Nest::it('subtracts the second number from the first')
    ->expect(Calc::sub(10, 5))
    ->toEqual(5);
});

Nest::describe('add()', function() {
  Nest::it('adds two numbers together')
    ->expect(Calc::add(1, 2))
    ->toEqual(3);
});

API

test / it

这些函数与 Pest 提供的函数完全相同(因为它们在底层转发调用)。尽管如此,我们需要使用这些函数,以便正确修改输出中的测试名称

use function Ozzie\Nest\test;
use function Ozzie\Nest\it;

test('true is true')->expect(true)->toEqual(true);

it('is true')->expect(true)->toEqual(true);

// normal callbacks work as well
it('can do math', function() {
  expect(1 + 1)->toEqual(2);
});

describe / when

这些函数仅用于将你的 test/it 调用分组在一起

describe() 将其描述作为前缀添加到所有嵌套测试中

when()when + 其描述作为前缀添加到所有嵌套测试中

这些函数可以嵌套多次,直到满足需求

use function Ozzie\Nest\describe;
use function Ozzie\Nest\when;
use function Ozzie\Nest\it;

describe('abs()', function () {
  when('positive value', function() {
    it('returns the number back', function () {
      expect(Calc::abs(1))->toEqual(1);
    });
  });
  
  when('negative value', function() {
    it('returns the number back as positive', function () {
      expect(Calc::abs(-1))->toEqual(1);
    });
    it('returns a float number back as positive', function () {
      expect(Calc::abs(-1.5))->toEqual(1.5);
    });
  });
});

限制

遗憾的是,由于设置/清理函数的工作方式,你无法在 describe/when 块内嵌套它们,并且只让它们应用于该块。你仍然必须在整个文件中只有一个例如 beforeEach 调用,它适用于每个测试。

你还必须显式使用 Ozze\Nest\testOzze\Nest\it 函数来使用 describewhen,这样我们才能正确更新测试输出。