Skip to content

JUnit XML logger does not handle assertion failures in before-test methods #5561

Closed
@hbrecht

Description

@hbrecht
Q A
PHPUnit version 10.4.1 / 10.4.2
PHP version PHP 8.2.7 with PCOV 1.0.11
Installation Method PHAR

Summary

On a failed assertion in setUp, phpunit fails with an internal error.
I am unsure, if assertions are allowed in setUp, the documentation only disallows them in setUpBeforeClass.
Issue was previously reported as #5549, but at the time due to an incorrect analysis on my side I was not able to narrow the root cause down. I updated #5549 with the minimal test case as well.

Current behavior

When running the test with the failed assertion, following happens:

An error occurred inside PHPUnit.

Message: PHPUnit\Event\Telemetry\HRTime::duration(): Argument #1 ($start) must be of type PHPUnit\Event\Telemetry\HRTime, null given, called in phar:///usr/local/bin/phpunit/phpunit/Logging/JUnit/JunitXmlLogger.php on line 191

Location: phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:48

#0 phar:///usr/local/bin/phpunit/phpunit/TextUI/Application.php(144): PHPUnit\TextUI\TestRunner->run()
#1 /usr/local/bin/phpunit(2976): PHPUnit\TextUI\Application->run()
#2 {main}

Caused by: phar:///usr/local/bin/phpunit/phpunit/Event/Value/Telemetry/HRTime.php:54

#0 phar:///usr/local/bin/phpunit/phpunit/Logging/JUnit/JunitXmlLogger.php(191): PHPUnit\Event\Telemetry\HRTime->duration()
#1 phar:///usr/local/bin/phpunit/phpunit/Logging/JUnit/JunitXmlLogger.php(148): PHPUnit\Logging\JUnit\JunitXmlLogger->handleFinish()
#2 phar:///usr/local/bin/phpunit/phpunit/Logging/JUnit/Subscriber/TestFinishedSubscriber.php(29): PHPUnit\Logging\JUnit\JunitXmlLogger->testFinished()
#3 phar:///usr/local/bin/phpunit/phpunit/Event/Dispatcher/DirectDispatcher.php(78): PHPUnit\Logging\JUnit\TestFinishedSubscriber->notify()
#4 phar:///usr/local/bin/phpunit/phpunit/Event/Dispatcher/DeferringDispatcher.php(41): PHPUnit\Event\DirectDispatcher->dispatch()
#5 phar:///usr/local/bin/phpunit/phpunit/Event/Emitter/DispatchingEmitter.php(548): PHPUnit\Event\DeferringDispatcher->dispatch()
#6 phar:///usr/local/bin/phpunit/phpunit/Framework/TestRunner.php(152): PHPUnit\Event\DispatchingEmitter->testFinished()
#7 phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php(436): PHPUnit\Framework\TestRunner->run()
#8 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(266): PHPUnit\Framework\TestCase->run()
#9 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(266): PHPUnit\Framework\TestSuite->run()
#10 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(266): PHPUnit\Framework\TestSuite->run()
#11 phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php(44): PHPUnit\Framework\TestSuite->run()
#12 phar:///usr/local/bin/phpunit/phpunit/TextUI/Application.php(144): PHPUnit\TextUI\TestRunner->run()
#13 /usr/local/bin/phpunit(2976): PHPUnit\TextUI\Application->run()
#14 {main}

When I run with assertions being active, I get

Message: assert($this->currentTestCase !== null)

How to reproduce

<?php
class DummyTest extends PHPUnit\Framework\TestCase {

        protected function setUp(): void {
            $this->assertTrue(false);
        }

        public function testDummy(): void {
            $this->assertTrue(true);
        }
}

Expected behavior

PHPUnit runs without internal error or documentation disallows assertions in setUp.

Metadata

Metadata

Labels

feature/loggingIssues related to logging test resultsfeature/logging/junitIssues related to logging test results in JUnit XML formatfeature/test-runnerCLI test runnertype/bugSomething is brokenversion/10Something affects PHPUnit 10

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions