-
Notifications
You must be signed in to change notification settings - Fork 102
struct's inits are executed before each test, but class's deinits only after a suite is complete. #605
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
Comments
Tests in Swift Testing run in parallel by default, meaning that they will be accessing the shared global state
|
Because tests run in parallel by default, the exact moment at which If you run your tests with the |
@grynspan Thanks, that makes sense. Right now I'm just running a quick test to try out Swift Testing. That global variable won't be part of my "production" code. |
Verified at desk that the logging occurs at the expected time when I'm closing this issue on that basis, but please do feel free to reopen if I've misunderstood the problem or if I've missed something here! |
@grynspan I tried running the suite serialized, but I got this issue that I don't know how to fix: I tried cleaning everything and nothing worked. The only way to fix it is creating a brand new project from Xcode and selecting Swift Testing as Testing Framework, but if it's added as a dependency, this issue happens all the time. (Is this already reported?) Regarding your comment, that makes sense, however, this documentation is misleading about the use of Thanks for your help! |
We haven't yet tagged a release of the Swift Testing package that includes the
Respectfully, the difference in behaviour here is entirely due to the parallelization of tests when using Swift Testing. If we implemented |
I didn't know that. Thanks for sharing!
And thank you for the explanation. I completely understand this behavior, and I apologize if my previous comment was not clear enough. My point is that I believe it would be beneficial if the documentation included a note indicating that due to the nature of parallelization, it's not simply a matter of replacing tearDown with deinit, but rather refactoring your code to function in a parallel environment or making it serialized in the meantime. At the end, it's merely a suggestion that could assist other developers. Thank you for taking the time to reply to my issue! 😃 |
We may be talking past each other, so I'm sorry if I'm just being boneheaded here and not understanding. This isn't specific to Developers using Swift 6 are generally going to be guided by the compiler to write concurrency-safe code, and so long as your code is concurrency-safe, the fact that |
Description
Hi, I'm learning how to use Swift Testing and preparing a series of tutorials for my Youtube Channel.
In my next video I want to present how to migrate from
setUp
andtearDown
intoinit
/deinit
. According to this documentation struct'sinit
acts as XCTest'ssetUp
, and I created a small demo to see what is the time wheninit
is called:As you can see, init is called before each test runs, which is expected. However, since deinit is not available on structs, I have to convert my suite into a final class. I was expecting that deinit will be executed after each test run, but that's not happening:
So, my question is: Is this expected?
From what I'm seeing, deinit is running after all tests in the suite are done. If that's the intention, I feel this is confusing in terms of marking
deinit
as equivalent totearDown
.I will really appreciate your guidance. Thank you!
Expected behavior
Class's deinit work as teardown running after each test is complete.
Actual behavior
Class's deinit are executed after all tests in suite are done.
Steps to reproduce
swift-testing version/commit hash
0.11.0
Swift & OS version (output of
swift --version ; uname -a
)The text was updated successfully, but these errors were encountered: