Skip to content

Conversation

@fredrikeriksson
Copy link

@fredrikeriksson fredrikeriksson commented Oct 8, 2025

🤔 What's changed?

Enhanced the SetTestMethod method in TUnitTestGeneratorProvider to include SourceFilePath and linenumber as additional parameters when adding the TEST_ATTR attribute. This makes TUnit override the usual behaviour.

Updated UnitTestMethodGenerator to populate generationContext.CustomData["linenumber"] with the scenario definition's line number for both row tests and standard tests.

⚡️ What's your motivation?

Fixes #872

🏷️ What kind of change is this?

  • 🐛 Bug fix (non-breaking change which fixes a defect)

♻️ Anything particular you want feedback on?

📋 Checklist:

  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "[vNext]" section of the CHANGELOG, linking to this pull request & included my GitHub handle to the release contributors list.

This text was originally taken from the template of the Cucumber project, then edited by hand. You can modify the template here.

Enhanced the `SetTestMethod` method in `TUnitTestGeneratorProvider` to include `SourceFilePath` and `linenumber` as additional parameters when adding the `TEST_ATTR` attribute.
This makes TUnit override the usual behaviour.

Updated `UnitTestMethodGenerator` to populate `generationContext.CustomData["linenumber"]` with the scenario definition's line number for both row tests and standard tests.

These changes improve test traceability by embedding source file path and line number metadata into generated test methods. reqnroll#872
@fredrikeriksson
Copy link
Author

fredrikeriksson commented Oct 8, 2025

A couple of notes:

  • It's hard to automatically test this so no updated tests as of now, is this fine?
  • This fix relies on a recent fix in TUnit, should we also update deps or just make a note in issue too update if needed?

@gasparnagy
Copy link
Contributor

@fredrikeriksson yes it is fine to do only exploratory tests for this.

Yes, feel free to update the dependency. We will release this in v3.2 then.

@fredrikeriksson
Copy link
Author

fredrikeriksson commented Oct 9, 2025

@gasparnagy Heh... out of curiosity I tried to upgrade TUnit in our project to latest version including the needed fix, and it now works!
When I sifted through TUnit code I recalled that TestAttribute had some attributes for CallerFilePath and CallerLineNumber and I had a hunch that it might honor pragma lines.
It seems I was correct but due to thomhurst/TUnit/issues/3330 which got fixed by thomhurst/TUnit/pull/3331 (v0.67.10) both approaches now work.
I verified that v0.67.9 does not work but v0.67.10 does.
So I propose we omit my code change and just keep the bump, any objections?

This is by the way the code for TestAttribute for educational purposes.

[AttributeUsage(AttributeTargets.Method)]
public sealed class TestAttribute(
    [CallerFilePath] string file = "",
    [CallerLineNumber] int line = 0)
    : BaseTestAttribute(file, line);

@thomhurst for info!

@gasparnagy
Copy link
Contributor

@fredrikeriksson @thomhurst Wow. Thx. I also checked and indeed it works.

I close this PR then. We can recover it if we will ever need it.

@gasparnagy gasparnagy closed this Oct 20, 2025
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.

TUnit - Wrong source reference in Test Explorer

2 participants