Skip to content

[Documentation] MSTest documentation lacks information about test lifecycle #5637

Closed

Description

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.

Activity

  1. added
    area/documentation
    on May 23, 2025
  2. marcelwgn commented on May 23, 2025

    @marcelwgn

    I would like to work on this.

  3. hawkerm commented on May 28, 2025

    @hawkerm

    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?

  4. marcelwgn commented on Jun 3, 2025

    @marcelwgn

    Ive created the PR for this page here: dotnet/docs#46540

  5. Youssef1313 commented on Jun 7, 2025

    @Youssef1313
    MemberAuthor

    Docs PR is merged.

  6. hawkerm commented on Jul 11, 2025

    @hawkerm

    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:

    1. Include the assembly initialize step (i.e. have a Run AssemblyInitialize (if implemented))
    2. 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...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/documentation

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions