Instance method data-source diagnostic depends on constructor declaration order

Closed Beginner friendly
#7,456 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

A pull request for this has already been merged.

  • #7502 by @thomhurst — merged

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
testing

Research direction

The bug is in canBeInstanceMethod in src/TUnit.Analyzers/TestDataAnalyzer.cs near line 512, which checks only InstanceConstructors.First(). Compare it with the preceding instance-method check, which searches all constructors, and see how the diagnostic is reported. Done means a parameterless constructor is found whichever order the constructors are declared, with a regression test covering both orders.

Written by the indexing model from the issue text.

Description

bug P3

An instance method data-source diagnostic depends on constructor declaration order. TestDataAnalyzer checks only the first constructor when deciding whether a test class has a parameterless constructor, so it can report that a data-source method must be static even when a parameterless constructor exists.

Reproduction

Declare a test class with a parameterized constructor first and a public parameterless constructor second. On a test method, use the explicit-type overload [MethodDataSource(typeof(Tests), nameof(Data))] with a public instance Data method returning IEnumerable<int>. Reverse the constructor order and compare diagnostics.

Expected and actual

Parameterless-constructor availability should not depend on declaration order. canBeInstanceMethod in src/TUnit.Analyzers/TestDataAnalyzer.cs:512 uses InstanceConstructors.First(), whereas the preceding instance-method check searches all constructors. Check all applicable constructors and add a regression for both declaration orders.

Environment

Confirmed by source inspection on main commit a412ddd5a5 and PR #7449. This is separate from selecting [TestConstructor] for class-level data rows: instance-source creation can use a parameterless constructor. Runtime reproduction has not yet been run. Related historical issue #1821 concerns the absence of a parameterless constructor rather than declaration order.

Dominant language
C#
Stars
4k
Forks
139
Avg merge
7h 54m
Merged PRs (30d)
359

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from thomhurst/TUnit

All issues in thomhurst/TUnit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.