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

Skipping a test in a before-class method crashes JUnit XML logger #6109

Closed
IonBazan opened this issue Jan 24, 2025 · 3 comments
Closed

Skipping a test in a before-class method crashes JUnit XML logger #6109

IonBazan opened this issue Jan 24, 2025 · 3 comments
Assignees
Labels
feature/logging/junit Issues related to logging test results in JUnit XML format feature/logging Issues related to logging test results type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11

Comments

@IonBazan
Copy link
Contributor

Q A
PHPUnit version 11.5.3
PHP version 8.3.16
Installation Method Composer

Summary

Test marked as skipped throws exception when using --log-junit since v11.5.3.

Current behavior

Some of my tests are marked as skipped using $this->markTestSkipped('This test is still broken');. When running PHPUnit with --log-junit, it throws an exception due to changes made in d099069 (fixing #6098)

How to reproduce

Create a test with following body:

    public function setUp(): void
    {
        parent::setUp();
        $this->markTestSkipped('This test is still broken');
    }

Executing the test causes following error:

vendor/bin/phpunit MyTest.php --log-junit=log.xml
PHPUnit 11.5.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.16
Configuration: /Users/ionbazan/work/project1/phpunit.xml

S                                                                   1 / 1 (100%)


An error occurred inside PHPUnit.

Message:  assert($this->currentTestCase !== null)
Location: /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Logging/JUnit/JunitXmlLogger.php:200

#0 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Logging/JUnit/JunitXmlLogger.php(200): assert(false, 'assert($this->c...')
#1 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Logging/JUnit/Subscriber/TestPrintedUnexpectedOutputSubscriber.php(24): PHPUnit\Logging\JUnit\JunitXmlLogger->testPrintedUnexpectedOutput(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#2 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Event/Dispatcher/DirectDispatcher.php(106): PHPUnit\Logging\JUnit\TestPrintedUnexpectedOutputSubscriber->notify(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#3 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Event/Dispatcher/DeferringDispatcher.php(47): PHPUnit\Event\DirectDispatcher->dispatch(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#4 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Event/Emitter/DispatchingEmitter.php(1009): PHPUnit\Event\DeferringDispatcher->dispatch(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#5 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php(210): PHPUnit\Event\DispatchingEmitter->testPrintedUnexpectedOutput('\n')
#6 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Framework/TestCase.php(361): PHPUnit\Framework\TestRunner->run(Object(MyTest))
#7 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/Framework/TestSuite.php(369): PHPUnit\Framework\TestCase->run()
#8 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(64): PHPUnit\Framework\TestSuite->run()
#9 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/src/TextUI/Application.php(210): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\TextUI\Configuration\Configuration), Object(PHPUnit\Runner\ResultCache\DefaultResultCache), Object(PHPUnit\Framework\TestSuite))
#10 /Users/ionbazan/work/project1/vendor/phpunit/phpunit/phpunit(104): PHPUnit\TextUI\Application->run(Array)
#11 /Users/ionbazan/work/project1/vendor/bin/phpunit(122): include('/Users/ionbazan...')
#12 {main}

Expected behavior

No error should be thrown.

@IonBazan IonBazan added the type/bug Something is broken label Jan 24, 2025
@sebastianbergmann sebastianbergmann added version/11 Something affects PHPUnit 11 feature/logging Issues related to logging test results version/10 Something affects PHPUnit 10 labels Jan 24, 2025
@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Jan 24, 2025

I cannot reproduce this:

tests/end-to-end/regression/6109/Issue6109Test.php

<?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\TestFixture\Issue6109;

use PHPUnit\Framework\TestCase;

final class Issue6109Test extends TestCase
{
    public function testOne(): void
    {
        $this->markTestSkipped('message');
    }
}
❯ ./phpunit --no-configuration --log-junit junit.xml tests/end-to-end/regression/6109/Issue6109Test.php
PHPUnit 11.5.3-16-ge108ac8ab8 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.3

S                                                                   1 / 1 (100%)

Time: 00:00.023, Memory: 8.00 MB

OK, but some tests were skipped!
Tests: 1, Assertions: 0, Skipped: 1.

junit.xml

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="PHPUnit\TestFixture\Issue6109\Issue6109Test" file="/usr/local/src/phpunit/tests/end-to-end/regression/6109/Issue6109Test.php" tests="1" assertions="0" errors="0" failures="0" skipped="1" time="0.001880">
    <testcase name="testOne" file="/usr/local/src/phpunit/tests/end-to-end/regression/6109/Issue6109Test.php" line="16" class="PHPUnit\TestFixture\Issue6109\Issue6109Test" classname="PHPUnit.TestFixture.Issue6109.Issue6109Test" assertions="0" time="0.001880">
      <skipped/>
    </testcase>
  </testsuite>
</testsuites>

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

@sebastianbergmann sebastianbergmann added status/waiting-for-feedback Waiting for feedback from original reporter feature/logging/junit Issues related to logging test results in JUnit XML format feature/logging Issues related to logging test results and removed feature/logging Issues related to logging test results labels Jan 24, 2025
@IonBazan
Copy link
Contributor Author

Apologies for incomplete example. Looks like somewhere along the way there is an unexpected output created in my app test suite which causes the issue. I will get that checked but in the meantime, I created following reproducer test which demonstrates the issue correctly:
https://github.com/IonBazan/phpunit/blob/issue-6109/tests/end-to-end/regression/6109/Issue6109Test.php

namespace PHPUnit\TestFixture\Issue6109;

use PHPUnit\Framework\TestCase;

final class Issue6109Test extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        print '*';

        $this->markTestSkipped('This test is failing when running with JUnit Logger');
    }

    public function testOne(): void
    {
        $this->assertTrue(true);
    }
}
php phpunit tests/end-to-end/regression/6109/Issue6109Test.php --log-junit=test.xml
PHPUnit 11.5.3-18-gc60d4ba3b by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.16
Configuration: /Users/ionbazan/work/phpunit/phpunit.xml

S                                                                   1 / 1 (100%)*

An error occurred inside PHPUnit.

Message:  assert($this->currentTestCase !== null)
Location: /Users/ionbazan/work/phpunit/src/Logging/JUnit/JunitXmlLogger.php:200

#0 /Users/ionbazan/work/phpunit/src/Logging/JUnit/JunitXmlLogger.php(200): assert(false, 'assert($this->c...')
#1 /Users/ionbazan/work/phpunit/src/Logging/JUnit/Subscriber/TestPrintedUnexpectedOutputSubscriber.php(24): PHPUnit\Logging\JUnit\JunitXmlLogger->testPrintedUnexpectedOutput(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#2 /Users/ionbazan/work/phpunit/src/Event/Dispatcher/DirectDispatcher.php(106): PHPUnit\Logging\JUnit\TestPrintedUnexpectedOutputSubscriber->notify(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#3 /Users/ionbazan/work/phpunit/src/Event/Dispatcher/DeferringDispatcher.php(47): PHPUnit\Event\DirectDispatcher->dispatch(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#4 /Users/ionbazan/work/phpunit/src/Event/Emitter/DispatchingEmitter.php(1009): PHPUnit\Event\DeferringDispatcher->dispatch(Object(PHPUnit\Event\Test\PrintedUnexpectedOutput))
#5 /Users/ionbazan/work/phpunit/src/Framework/TestRunner/TestRunner.php(210): PHPUnit\Event\DispatchingEmitter->testPrintedUnexpectedOutput('*')
#6 /Users/ionbazan/work/phpunit/src/Framework/TestCase.php(361): PHPUnit\Framework\TestRunner->run(Object(PHPUnit\TestFixture\Issue6109\Issue6109Test))
#7 /Users/ionbazan/work/phpunit/src/Framework/TestSuite.php(369): PHPUnit\Framework\TestCase->run()
#8 /Users/ionbazan/work/phpunit/src/TextUI/TestRunner.php(64): PHPUnit\Framework\TestSuite->run()
#9 /Users/ionbazan/work/phpunit/src/TextUI/Application.php(210): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\TextUI\Configuration\Configuration), Object(PHPUnit\Runner\ResultCache\DefaultResultCache), Object(PHPUnit\Framework\TestSuite))
#10 /Users/ionbazan/work/phpunit/phpunit(104): PHPUnit\TextUI\Application->run(Array)
#11 {main}

@sebastianbergmann sebastianbergmann removed the status/waiting-for-feedback Waiting for feedback from original reporter label Jan 28, 2025
@sebastianbergmann
Copy link
Owner

Thank you, I will look into this as soon as I can.

@sebastianbergmann sebastianbergmann changed the title Skipped tests cause PHPUnit exception with JUnitXmlLogger Skipping a test in a before-class method crashes JUnit XML logger Jan 28, 2025
@sebastianbergmann sebastianbergmann self-assigned this Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/logging/junit Issues related to logging test results in JUnit XML format feature/logging Issues related to logging test results type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11
Projects
None yet
Development

No branches or pull requests

2 participants