Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hbrecht opened this issue Nov 14, 2023 · 1 comment
Closed
Assignees
Labels
feature/logging Issues related to logging test results feature/test-runner CLI test runner type/bug Something is broken version/10 Something affects PHPUnit 10

Comments

@hbrecht
Copy link

hbrecht commented Nov 14, 2023

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.

@hbrecht hbrecht added type/bug Something is broken version/10 Something affects PHPUnit 10 labels Nov 14, 2023
@sebastianbergmann
Copy link
Owner

This is "just" a bug in the JUnit XML logger:

Issue5561Test.php

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

final class Issue5561Test extends TestCase
{
    protected function setUp(): void
    {
        $this->assertTrue(false);
    }

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

Without --log-junit

PHPUnit 10.4.2-20-gefd8372395 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12

F                                                                   1 / 1 (100%)

Time: 00:00.016, Memory: 4.00 MB

There was 1 failure:

1) Issue5561Test::testOne
Failed asserting that false is true.

/home/sb/Issue5561Test.php:8

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

With --log-junit

PHPUnit 10.4.2-20-gefd8372395 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12

F                                                                   1 / 1 (100%)

An error occurred inside PHPUnit.

Message:  assert($this->currentTestCase !== null)
Location: /usr/local/src/phpunit/src/TextUI/TestRunner.php:68

#0 /usr/local/src/phpunit/src/TextUI/Application.php(189): PHPUnit\TextUI\TestRunner->run()
#1 /usr/local/src/phpunit/phpunit(99): PHPUnit\TextUI\Application->run()
#2 {main}

Caused by: /usr/local/src/phpunit/src/Logging/JUnit/JunitXmlLogger.php:238

#0 /usr/local/src/phpunit/src/Logging/JUnit/JunitXmlLogger.php(238): assert()
#1 /usr/local/src/phpunit/src/Logging/JUnit/JunitXmlLogger.php(190): PHPUnit\Logging\JUnit\JunitXmlLogger->handleFinish()
#2 /usr/local/src/phpunit/src/Logging/JUnit/Subscriber/TestFinishedSubscriber.php(28): PHPUnit\Logging\JUnit\JunitXmlLogger->testFinished()
#3 /usr/local/src/phpunit/src/Event/Dispatcher/DirectDispatcher.php(100): PHPUnit\Logging\JUnit\TestFinishedSubscriber->notify()
#4 /usr/local/src/phpunit/src/Event/Dispatcher/DeferringDispatcher.php(45): PHPUnit\Event\DirectDispatcher->dispatch()
#5 /usr/local/src/phpunit/src/Event/Emitter/DispatchingEmitter.php(952): PHPUnit\Event\DeferringDispatcher->dispatch()
#6 /usr/local/src/phpunit/src/Framework/TestRunner.php(238): PHPUnit\Event\DispatchingEmitter->testFinished()
#7 /usr/local/src/phpunit/src/Framework/TestCase.php(489): PHPUnit\Framework\TestRunner->run()
#8 /usr/local/src/phpunit/src/Framework/TestSuite.php(340): PHPUnit\Framework\TestCase->run()
#9 /usr/local/src/phpunit/src/TextUI/TestRunner.php(63): PHPUnit\Framework\TestSuite->run()
#10 /usr/local/src/phpunit/src/TextUI/Application.php(189): PHPUnit\TextUI\TestRunner->run()
#11 /usr/local/src/phpunit/phpunit(99): PHPUnit\TextUI\Application->run()
#12 {main}

@sebastianbergmann sebastianbergmann self-assigned this Nov 14, 2023
@sebastianbergmann sebastianbergmann added feature/test-runner CLI test runner feature/logging Issues related to logging test results labels Nov 14, 2023
@sebastianbergmann sebastianbergmann changed the title PHPunit internal error when assertion fails in setUp JUnit XML logger does not handle assertion failures in before-test methods Nov 14, 2023
sebastianbergmann added a commit that referenced this issue Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/logging Issues related to logging test results feature/test-runner CLI test runner type/bug Something is broken version/10 Something affects PHPUnit 10
Projects
None yet
Development

No branches or pull requests

2 participants