Skip to content
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

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

Open
starosta33 opened this issue Apr 8, 2024 · 9 comments
Open

Comments

@starosta33
Copy link

starosta33 commented Apr 8, 2024

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
Copy link
Contributor

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

@Evangelink
Copy link
Member

Yes mine :)

@Evangelink Evangelink added this to the 3.5.0 milestone Apr 8, 2024
@Evangelink
Copy link
Member

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
Copy link
Member

nohwnd commented Apr 10, 2024

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
Copy link
Member

Evangelink commented Apr 10, 2024

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
Copy link
Contributor

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
Copy link
Member

nohwnd commented Apr 11, 2024

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
Copy link
Member

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
Copy link
Member

nohwnd commented Apr 11, 2024

I did not think about that 👍

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

No branches or pull requests

4 participants