microsoft / testfx

MSTest framework and adapter
MIT License
732 stars 254 forks source link

Allow before/after TestMethod code with registering global callback #2673

Open starosta33 opened 6 months ago

starosta33 commented 6 months ago

the semantic should be something similar as below:

Note: this is allowed in NUnit already (BeforeTest & AfterTest hooks)


public class BeforeAfterTestMethodGlobalHook
{
    [GlobalBeforeTestMethod]
    public async Task BeforeTestMethodAsync(TestContext testContext)
    {
        // ...
    }

    [GlobalAfterTestMethod]
    public async Task AfterTestMethodAsync(TestContext testContext)
    {
        // ...
    }
}
MarcoRossignoli commented 6 months ago

@Evangelink I think we have already other request for this one.

Evangelink commented 6 months ago

Yes mine :)

Evangelink commented 6 months ago

We need to be discussing the design of this feature as there are a couple of questions that come to my mind:

  1. Shall we allow a local test init/cleanup to opt-out from the global call?
  2. Do we want to detect the global init/cleanup through a new set of attributes? A new set of assembly attributes? Do we prefer a custom registration (e.g. on MSTest framework on the runner) of lambdas?
  3. Shall we support the various attributes that are supported on regular init/cleanup?
nohwnd commented 6 months ago

What are the cases where you found it useful @evangelink?

If I am not mistaken this can be done with a base class, which has the flexibility that point 1. above seeks, and does not require duplicating of all the options (point 3.)

Evangelink commented 6 months ago

Good question, the base class is indeed the most forward solution.

I was mainly thinking about scenarios where you have already many tests and want to introduce a common behavior or when your tests already have base classes and you want an easier solution than creating X intermediate base class that would add this init/cleanup behavior and inherit from the previous base class.

@starosta33 Would the base class be enough for your use case?

MarcoRossignoli commented 5 months ago

I think that this feature is "useful" to move from other adapters to MSTest, this functionality is available there and looks like the update of big tests suite to achieve the same semantic is "too much".

nohwnd commented 5 months ago

creating X intermediate base class that would add this init/cleanup behavior and inherit from the previous base class.

Another option here is to compose rather than inherit. So a helper that implements the setup would be added, and the existing base class would call it.

Evangelink commented 5 months ago

Another option here is to compose rather than inherit. So a helper that implements the setup would be added, and the existing base class would call it.

Yes definitely! I was thiking about the cases where the base class is not something you own.

nohwnd commented 5 months ago

I did not think about that 👍