You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Documentation] MSTest documentation lacks information about test lifecycle #5637
Information about lifecycle doesn't seem to be documented. I think it can be summarized as follows.
AssemblyInitialize is called, if present
For every class:
ClassInitialize is called, if present.
For every test case in the class (a parameterized test method has its individual inputs as separate test cases):
Create class instance (so your constructor is called, optionally, it can have TestContext parameter)
Set TestContext property if present
Run TestInitialize, including from base classes (base classes are called first in reverse order, then TestInitialize from the current class)
Run the test method itself. If it returns a Task/ValueTask, we await it
Update the test outcome in TestContext
Run TestCleanup methods. First we run TestCleanup from the current class, then from any base classes in order (so it's reverse order of test initialize)
If you implement IAsyncDisposable, we call DisposeAsync
If you implement IDisposable, we call Dispose
ClassCleanup is called, if present.
AssemblyCleanup is called, if present.
Reacted by Amaury Levé, Marcel W. and Michael A. Hawker
Yeah, this has definitely been confusing in the current docs. Clarification in the notes on what "The properties related to the test run are not available" (or what that includes) would be useful too.
I followed the new 3.6+ example for TestContext setting in the constructor, but either way, I'm adding a property to the context in the Properties bag, in the static ClassInitialize method. However, only one of my two test methods can access it, randomly. It looks like two class instances for each or my tests is created, as described above, but one of them gets passed a general TestContext, instead of the one passed in and modified by ClassInitialize. To me it seems like the same root TestContext passed to ClassInitialize should be used for all tests?
Does TestContext itself need more clarification above too? Am I misunderstanding how this works, or is this a bug where the context isn't propagating properly?
Alright, had some time and was able to go through the new doc, my project, and setting up a separate repro for MSTest.Sdk/3.6.4.
I definitely think more clarification is needed and/or there's some issues with how TestContext and classes are being initialized and passed instances for both ClassInitialize and AssemblyInitialize.
For instance the complete order doesn't:
Include the assembly initialize step (i.e. have a Run AssemblyInitialize (if implemented))
Similarly, separate out the ordering of the call to ClassInitialize (which from my tests seems to occur first) before the Constructor to the class is called (the Create instance of test class step).
I'm seeing the same oddness with the AssemblyInitialize. If I run each test individually, it'll work and I see AssemblyInitialize called for each one. When I run them both together though, one will see the context set and the other one won't. Shouldn't the AssemblyInitialize run and be able to store a property before either class is constructed?
Though, this is a similar behavior to what I'm seeing with my ClassInitialize in my project, I just noticed I have both my tests in the same class there compared to creating different classes in the repro. i.e. if I run either test within the same class individually it'll pass with the proper stored context provided in the ClassInitialize. However, if I run the whole thing together, I see two different TestContext objects being passed. One has been initialized by ClassInitialize and the other is some other object (I also see the constructor called twice instead of once).
I'll open a new bug with my repro as an example, I have it where each test will pass individually with a full lifecycle run, but non-deterministically fail when run all together as they're all getting passed different contexts instead of sharing one (or two - I'm not quite sure how the TestContext property bag is supposed to be handled in the assembly case with two classes...).
Activity
marcelwgn commentedon May 23, 2025
I would like to work on this.
Evangelink commentedon May 23, 2025
Awesome thanks for willing to help Marcel W. (@marcelwgn)! I think we can create a new page under Writing test and we probably want to link this attribute section https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-writing-tests-attributes#attributes-used-to-provide-initialization-and-cleanups
hawkerm commentedon May 28, 2025
Yeah, this has definitely been confusing in the current docs. Clarification in the notes on what "The properties related to the test run are not available" (or what that includes) would be useful too.
I followed the new 3.6+ example for TestContext setting in the constructor, but either way, I'm adding a property to the context in the Properties bag, in the static ClassInitialize method. However, only one of my two test methods can access it, randomly. It looks like two class instances for each or my tests is created, as described above, but one of them gets passed a general TestContext, instead of the one passed in and modified by ClassInitialize. To me it seems like the same root TestContext passed to ClassInitialize should be used for all tests?
Does TestContext itself need more clarification above too? Am I misunderstanding how this works, or is this a bug where the context isn't propagating properly?
marcelwgn commentedon Jun 3, 2025
Ive created the PR for this page here: dotnet/docs#46540
Youssef1313 commentedon Jun 7, 2025
Docs PR is merged.
hawkerm commentedon Jul 11, 2025
Alright, had some time and was able to go through the new doc, my project, and setting up a separate repro for MSTest.Sdk/3.6.4.
I definitely think more clarification is needed and/or there's some issues with how TestContext and classes are being initialized and passed instances for both ClassInitialize and AssemblyInitialize.
For instance the complete order doesn't:
Run AssemblyInitialize (if implemented))Create instance of test classstep).I'm seeing the same oddness with the AssemblyInitialize. If I run each test individually, it'll work and I see AssemblyInitialize called for each one. When I run them both together though, one will see the context set and the other one won't. Shouldn't the AssemblyInitialize run and be able to store a property before either class is constructed?
Though, this is a similar behavior to what I'm seeing with my ClassInitialize in my project, I just noticed I have both my tests in the same class there compared to creating different classes in the repro. i.e. if I run either test within the same class individually it'll pass with the proper stored context provided in the ClassInitialize. However, if I run the whole thing together, I see two different TestContext objects being passed. One has been initialized by ClassInitialize and the other is some other object (I also see the constructor called twice instead of once).
I'll open a new bug with my repro as an example, I have it where each test will pass individually with a full lifecycle run, but non-deterministically fail when run all together as they're all getting passed different contexts instead of sharing one (or two - I'm not quite sure how the TestContext property bag is supposed to be handled in the assembly case with two classes...).