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

Added TestData attribute with named data sets and variadic parameter support #5167

Closed

Conversation

davidbyoung
Copy link
Sponsor

@davidbyoung davidbyoung commented Feb 4, 2023

This is meant to simplify how inline test data is passed into tests. Rather than requiring everything to be wrapped in an array like

#[TestWith(['foo', 'bar'])]
public function testSomething(string $a, string $b): void
{
    // ...
}

You can now just pass in the parameters directly using the new #[TestData] attribute:

#[TestData('foo', 'bar')]
public function testSomething(string $a, string $b): void
{
    // ...
}

which looks more like an invocation of the test method. Additionally, this PR introduces support for naming data sets using a name parameter, eg

#[TestData('foo', 'bar', name: 'Tests something with "foo" and "bar"')]
public function testSomething(string $a, string $b): void
{
    // ...
}

I have purposely not created an annotation version of this because PHPDoc is not a good place to define PHP values to pass into test methods.

@davidbyoung davidbyoung marked this pull request as ready for review February 4, 2023 23:33
@davidbyoung
Copy link
Sponsor Author

I have a few open questions:

  • Should TestWith and TestWithJson be deprecated in favor of the new TestData attribute to unify how to pass inline data to a test?
  • I built my implementation sort of off the TestWith functionality, eg marking the TestData metadata's isTestWith() as true. Should I completely separate this implementation from TestWith?
  • This solution uses an optional named argument to name the data set. Typically, PHPUnit has avoided supporting named arguments. Is my approach acceptable, or should I consider an alternative?

@sebastianbergmann
Copy link
Owner

have purposely not created an annotation version

Good, as no new annotations will be added to PHPUnit.

@sebastianbergmann
Copy link
Owner

This solution uses an optional named argument to name the data set. Typically, PHPUnit has avoided supporting named arguments. Is my approach acceptable, or should I consider an alternative?

Please consider an alternative.

@sebastianbergmann
Copy link
Owner

I built my implementation sort of off the TestWith functionality, eg marking the TestData metadata's isTestWith() as true. Should I completely separate this implementation from TestWith?

I think we do not need a new Metadata object, only a new attribute. The attribute parser should create the same Metadata object from the two attributes.

@sebastianbergmann
Copy link
Owner

Should TestWith and TestWithJson be deprecated in favor of the new TestData attribute to unify how to pass inline data to a test?

I do not think so, at least not for now.

@sebastianbergmann sebastianbergmann added feature/data-provider Data Providers feature/metadata Issues related to attributes and annotations feature/metadata/attributes type/enhancement A new idea that should be implemented labels Feb 5, 2023
@codecov
Copy link

codecov bot commented Feb 5, 2023

Codecov Report

Merging #5167 (eb4cc7f) into main (9c1d355) will decrease coverage by 0.06%.
The diff coverage is 40.47%.

@@             Coverage Diff              @@
##               main    #5167      +/-   ##
============================================
- Coverage     81.24%   81.18%   -0.06%     
- Complexity     5827     5844      +17     
============================================
  Files           629      631       +2     
  Lines         18459    18499      +40     
============================================
+ Hits          14997    15019      +22     
- Misses         3462     3480      +18     
Impacted Files Coverage Δ
src/Framework/Attributes/TestData.php 0.00% <0.00%> (ø)
src/Metadata/Metadata.php 98.09% <0.00%> (-1.91%) ⬇️
src/Metadata/TestData.php 0.00% <0.00%> (ø)
src/Metadata/Parser/AttributeParser.php 99.61% <80.00%> (-0.39%) ⬇️
src/Metadata/Api/DataProvider.php 54.24% <92.85%> (+8.14%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@davidbyoung
Copy link
Sponsor Author

Thanks for the feedback! Looking to xUnit and NUnit for inspiration, it looks like xUnit does not support named data sets in their [InlineData] attribute, but NUnit does via named parameters, similar to my original approach. As far as I can tell, we have the following options:

  • Use named parameters, although this would be a break with how PHPUnit has typically worked.
  • Have a separate attribute #[NamedTestData('This is the name', 'foo', 'bar')] whose first parameter is the name and subsequent parameters are passed to the test method. We could still introduce the #[TestData('foo', 'bar')] attribute to prevent us from having to pass in the parameters in an array, just without supporting named data sets.
  • Require data sets to always be named as the first parameter in #[TestData('This is the name', 'foo', 'bar')].
  • Continue to wrap parameters in an array, similar to this approach but with the negative of having to wrap all parameters in an array, which, at least to me, feels unintuitive.
  • Scrap the concept of named data sets altogether.

I can implement a PR for any of these approaches, but wanted to get your input first.

@davidbyoung
Copy link
Sponsor Author

@sebastianbergmann Did you have any preference on my list of proposed ways forward above?

@davidbyoung
Copy link
Sponsor Author

Hi @sebastianbergmann, have you had an opportunity to review the different options I enumerated above? Do any sound preferable to you? Or should I abandon this feature altogether? Thanks!

@sebastianbergmann
Copy link
Owner

The more I think about this, the more I think that things should stay as they are. Sorry.

@davidbyoung davidbyoung closed this Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/data-provider Data Providers feature/metadata/attributes feature/metadata Issues related to attributes and annotations type/enhancement A new idea that should be implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants