Skip to content

Conversation

@klausi
Copy link

@klausi klausi commented Feb 21, 2025

Junit support for the file path is missing, adding it here.

@klausi
Copy link
Author

klausi commented Feb 21, 2025

Uploading stable diff file for composer patches.

behat-file-path-1601.diff.txt

Copy link
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @klausi

I've checked at https://github.com/testmoapp/junitxml?tab=readme-ov-file#example (closest thing there is to a JUnit spec) and can see the file attribute is supported on <testcase> so we'd be happy to add this.

I think it does need a slightly different implementation, and the example outputs in https://github.com/Behat/Behat/blob/master/features/junit_format.feature also need to be updated with the new attribute to pass the tests.

If you have time to work on this, that would be great :)

'failures' => $stats[TestResult::FAILED],
'errors' => $stats[TestResult::PENDING] + $stats[TestResult::UNDEFINED],
'time' => $this->durationListener ? $this->durationListener->getFeatureDuration($this->currentFeature) : '',
'file' => $this->currentFeature->getFile(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for consistency this would need to be a relative path the same as the file attribute on the <testcase node.

Currently that is calculated internal to the JUnitScenarioPrinter

if ($file) {
$cwd = realpath(getcwd());
$testCaseAttributes['file'] =
substr($file, 0, strlen($cwd)) === $cwd ?
ltrim(substr($file, strlen($cwd)), DIRECTORY_SEPARATOR) : $file;
}
but it would probably be better to extract that rather than duplicating it...

Note also that it appears Gherkin does not guarantee the file is defined - ->getFile() can be null|string. I have to say I'm not sure why/when it could be missing... The scenario printer therefore only adds a file attribute if there is a file to report.

@klausi
Copy link
Author

klausi commented Feb 21, 2025

You are absolutely correct, thank you for the pointers!

I would probably just copy the file handling code. You know the golden rule of software engineering: duplicating simple code once is fine and avoids abstraction complexity. The third time you want to copy - then you do the refactor :-)

I won't have time to work on this right now as I'm busy with other things. Anyone feel free to pick this up!

@acoulton
Copy link
Contributor

You're absolutely right about early abstractions, I was just conscious that we also "relativise" paths for the pretty and progress formatters and suspected that might also be duplicated (or abstracted).

Looking at them now, I see they actually have slightly different logic. They show paths relative to the behat %paths.base% configuration - this is the path to the behat.yml / behat.php config file, which is usually (but not always) the same as the cwd() that the JunitFeaturePrinter uses.

I think it would probably be better if the JUnit file attributes were consistent with the other formatters, and guaranteed to be relative to the base directory regardless of cwd()? But I'm not sure if that would pose a BC issue (we don't use JUnit much)?

@carlos-granados
Copy link
Contributor

Thanks @klausi for your contribution. We have generated a new PR from your suggestion. It will replace this PR

New PR #1680

carlos-granados added a commit that referenced this pull request Oct 26, 2025
#1680)

Adds the "file" attribute in feature nodes in the JSON and JUnit
formatters

Replaces #1601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants