-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[8.x] Adds first-level test traits setUp/tearDown (opt-in) #39883
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
[8.x] Adds first-level test traits setUp/tearDown (opt-in) #39883
Conversation
I think this would be a nice addition. I did want to mention that you can accomplish this right now with the following: trait MyTrait
{
/** @before */
protected function setUpMyTrait()
{
$this->afterApplicationCreated(function() {
// Perform setup (must happen in afterApplicationCreated because
// everything with the @before annotation runs BEFORE setUp() is called
});
}
/** @after */
protected function tearDownMyTrait()
{
// Perform tear down (runs after tearDown())
}
} But I think the confusion about how the Edit: Any |
I also do it in some of my testing traits but feels less straightforward using annotations. |
Ah I didn't immediately remember that you could do this using the annotations. In that case I wouldn't add this to the core framework. There's also a slight chance of breakages with this PR if people have already defined such methods. |
If there is a minimal collision risk, I can push this to 9.x then, and remove the interface. |
I still feel like the I suppose another option would be to add a new |
I actually just dug around some more and I was wrong, the |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
What?
Allows the developer to include and reuse traits that automatically set-up and tear-down in each test, when declared at first-level.
Given a reusable test
WithExamplePost
:The test class implementing
InitializeTraits
, will automatically callsetUpWithExamplePost
after the application is created, andtearDownWithExamplePost
before is destroyed.Since this is an additive opt-in feature, no test will break unless the interface is added on tests classes.
Why?
There are three keys for this implementation:
BC?
Nope, opt-in additive.
Notes
I decided to use an interface-check in the
TestCase
rather than adding a trait because:Didn't I see this before?
Kinda