PHPUnit仅识别我的四个测试中的三个

时间:2018-09-26 14:50:02

标签: php unit-testing phpunit

我有一个包含四个单元测试的类。该类如下所示:

class TestWorkflowService extends TestCase
{
    private $containerMock;

    private $workflowEntityMock;

    private $workflowService;

    public function setup()
    {
        $this->containerMock = $this->createMock(ContainerInterface::class);
        $this->workflowService = new WorkflowService($this->containerMock);
        $this->workflowEntityMock = $this->createMock(WorkflowInterface::class);
    }

    public function testGetWorkflowProcessByEntityNullResult()
    {
        self::assertNull($this->workflowService->getWorkflowProcessByEntity($this->workflowEntityMock));
    }

    public function testGetProcessHandlerByEntityNullResult()
    {
        self::assertNull($this->workflowService->getProcessHandlerByEntity($this->workflowEntityMock));
    }

    public function testRestartWorkflow()
    {
        $modelStateMock = $this->createMock(ModelState::class);
        $processHandlerMock = $this->createMock(ProcessHandler::class);
        $processHandlerMock->method('start')->willReturn($modelStateMock);
        $this->containerMock->method('get')->willReturn($processHandlerMock);
        self::assertNull($this->workflowService->restartWorkflow($this->workflowEntityMock));
    }

    public function setEntityToNextWorkflowState()
    {
        $modelStateMock = $this->createMock(ModelState::class);
        $processHandlerMock = $this->createMock(ProcessHandler::class);
        $processHandlerMock->method('start')->willReturn($modelStateMock);
        $this->containerMock->method('get')->willReturn($processHandlerMock);
        self::assertNull($this->workflowService->setEntityToNextWorkflowState($this->workflowEntityMock));
    }
}

...但是当我运行PHPUnit时,会得到以下结果:

  

... 3   / 3(100%)

     

时间:2.17秒,内存:5.75MB

     

好的(3个测试,3个断言)

为什么我的第四项考试未被认可?

1 个答案:

答案 0 :(得分:2)

PHPUnit使用following rules标识测试方法:

  

测试是名为test *的公共方法。

     

或者,您可以在方法的文档块中使用@test批注将其标记为测试方法。

这样一来,您就可以在测试类中拥有其他公共方法,而不必将它们解释为测试(尽管我不确定您为什么会真正这样做)。

将您的方法名称更改为testSetEntityToNextWorkflowState,或使用@test批注对其进行标记。