当我们使用 PHPUnit 时,控制台中的 --uses 参数到底有什么作用以及如何使用?

问题描述 投票:0回答:1

我在使用控制台时遇到了 PHPUnit 的参数。

如何使用它以及使用名为 --uses 的参数做什么?

为了具体说明我的意思,文档的链接:

https://docs.phpunit.de/en/11.1/textui.html#:~:text=to%20document%20this.-,%2D%2Duses%20%3Cname%3E,-仅%20run%20tests

如果您能帮助揭示其中的要点,我将非常感谢您。

php phpunit
1个回答
0
投票

我在https://manpages.debian.org/testing/phpunit/phpunit.1.en.html

找到了它

它说:

Only runs tests annotated with "@uses <name>"

所以,假设你的名字是Monkey。在这种情况下,您可以使用

注释测试
/**
 * @covers \BankAccount
 * @uses   \Money
 */
public function testMoneyCanBeDepositedInAccount(): void
{
    // ...
}

@uses 注释指定将由测试执行的代码,但不打算被测试覆盖。一个很好的例子是测试代码单元所必需的值对象。

这里还有另一个示例:https://docs.phpunit.de/en/10.5/code-coverage.html#code-coverage-targeting-units-of-code-examples-invoicetest-php

<?php declare(strict_types=1);
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Invoice::class)]
#[UsesClass(Money::class)]
final class InvoiceTest extends TestCase
{
    public function testAmountInitiallyIsEmpty(): void
    {
        $this->assertEquals(new Money, (new Invoice)->amount());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.