testfx icon indicating copy to clipboard operation
testfx copied to clipboard

AssemblyInitialize/Cleanup should not be executed when all tests are ignored

Open gao-artur opened this issue 1 year ago • 7 comments

Describe the bug

When all tests in the project are ignored, the AssemblyInitialize method is not executed. The AssemblyCleanup is always executed. I'd expect both methods to always execute or not execute. The answer to this issue suggests the AssemblyCleanup will always execute, so I expect the same from AssemblyInitialize (otherwise, what the cleanup method supposed to clean?)

Steps To Reproduce

Set the breakpoint in AssemblyInitialize and AssemblyCleanup and debug

[TestClass]
public class UnitTest1
{
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext context)
    {
    }

    [AssemblyCleanup]
    public static void AssemblyCleanup()
    {
    }

    [TestMethod, Ignore]
    public void TestMethod1()
    {
    }
}

Expected behavior

Both AssemblyInitialize and AssemblyCleanup are executed

Actual behavior

Only AssemblyCleanup is executed

AB#1850400

gao-artur avatar Mar 22 '23 14:03 gao-artur

Hi @gao-artur, I confirm that I'm able to reproduce the issue.

engyebrahim avatar Mar 23 '23 12:03 engyebrahim

Hey @engyebrahim,

Could you check if that something that never worked (you can test with a few previous versions, e.g. 2.2.10, 2.2.8, 2.2.4) or if it's a regression we introduced in v3? If it's a regression we need to work on it soon.

Evangelink avatar May 12 '23 09:05 Evangelink

@Evangelink I can repro in these versions 2.2.10, 2.2.8, 2.2.4.

engyebrahim avatar May 19 '23 09:05 engyebrahim

@gao-artur if both methods were skipped, would it be causing any issue for you? Looking at the issue and at the code, it seems more logical to me that they are both skipped if there is no test to run (no test or all tests are skipped). Because the cleanup is currently called, I would need to postpone the fix to v4 so I am wondering if there is any bad impact on your side.

Evangelink avatar Jul 11 '23 16:07 Evangelink

Skipping both is even better. I see no point in initializing something that won't be used, especially if it's a heavy operation like creating and initializing DB, for example.

gao-artur avatar Jul 11 '23 16:07 gao-artur

Postponing to v4 then as it is a breaking change (I agree that current behavior is not right).

Evangelink avatar Jul 11 '23 16:07 Evangelink

I agree that setups and teardowns should run only when at least 1 test will run from the assembly, otherwise we are just wasting the effort.

...AssemblyCleanup will always execute, so I expect the same from AssemblyInitialize (otherwise, what the cleanup method supposed to clean?)

In my opinion AssemblyCleanup should not rely on AssemblyInitialize. Assembly initialize can throw an exception, be cancelled, or aborted, or be empty. Or the things that need cleaning up were created by tests, and not by assembly initialize.

So ideally Assembly Cleanup waits for AssemblyInitialize to finish (either successfully or unsuccessfully), but otherwise is not tied to it.

nohwnd avatar Jan 10 '24 10:01 nohwnd