swift-testing icon indicating copy to clipboard operation
swift-testing copied to clipboard

[SWT-NNNN] Introduce API allowing traits to customize test execution

Open stmontgomery opened this issue 1 year ago • 2 comments

This includes an API proposal and code changes to introduce new API for custom traits to customize test execution.

View the API proposal for more details.

Motivation:

One of the primary motivations for the trait system in Swift Testing, as described in the vision document, is to provide a way to customize the behavior of tests which have things in common. If all the tests in a given suite type need the same custom behavior, init and/or deinit (if applicable) can be used today. But if only some of the tests in a suite need custom behavior, or tests across different levels of the suite hierarchy need it, traits would be a good place to encapsulate common logic since they can be applied granularly per-test or per-suite. This aspect of the vision for traits hasn't been realized yet, though: the Trait protocol does not offer a way for a trait to customize the execution of the tests or suites it's applied to.

Customizing a test's behavior typically means running code either before or after it runs, or both. Consolidating common set-up and tear-down logic allows each test function to be more succinct with less repetitive boilerplate so it can focus on what makes it unique.

Checklist:

  • [x] Code and documentation should follow the style of the Style Guide.
  • [x] If public symbols are renamed or modified, DocC references should be updated.

stmontgomery avatar Sep 25 '24 21:09 stmontgomery

@swift-ci please test Windows

stmontgomery avatar Sep 25 '24 21:09 stmontgomery

@stmontgomery Per our conversation offline you mentioned that order of execution of custom execution traits is well-defined to be outer-to-inner, left-to-right -- it would be great if we could explicitly add a note about that in the documentation so that folks are aware of the expected behavior!

jakepetroules avatar Oct 17 '24 04:10 jakepetroules

@stmontgomery Per our conversation offline you mentioned that order of execution of custom execution traits is well-defined to be outer-to-inner, left-to-right -- it would be great if we could explicitly add a note about that in the documentation so that folks are aware of the expected behavior!

I've added language specifying this ordering to the updated proposal and PR

stmontgomery avatar Nov 20 '24 19:11 stmontgomery

@swift-ci please test

stmontgomery avatar Nov 20 '24 20:11 stmontgomery

@swift-ci please test

stmontgomery avatar Jan 07 '25 20:01 stmontgomery

Resolves #36

stmontgomery avatar Mar 05 '25 13:03 stmontgomery

@stmontgomery the proposal document no longer exists, but I would love to use this! How do I?

Mcrich23 avatar Oct 23 '25 18:10 Mcrich23

It moved here.

grynspan avatar Oct 23 '25 18:10 grynspan